How do I know what the theme is selected in the settings (light or dark)?
I want to use a conditional statement such as
if (darkTheme) {..}
else {..}
You want to find your response in the official MSDN page for Theme on Windows Phone.
In the part "Determining Theme Background" that indicate :
// Determine the visibility of the dark background.
Visibility darkBackgroundVisibility =
(Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
// Write the theme background value.
if (darkBackgroundVisibility == Visibility.Visible)
{
textBlock1.Text = "background = dark";
}
else
{
textBlock1.Text = "background = light";
}
Also, in this page, you've a part on the "theme accent color". To recover the two main colors defined by the user ( background and accent color).
if( (Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] )
...
else
...
I find the easiest approach to determine the theme is to use:
public bool darkTheme = ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible);
is darkTheme is true then the selected theme is dark, and false for light.
Then in whatever procedure just use a simple if statement, such as:
if (darkTheme == true)
{
//Do some stuff related to dark theme
}
else
{
//Do some stuff related to light theme
}
// Detecting the current theme.
private static Color lightThemeBackground = Color.FromArgb(255, 255, 255, 255);
private static Color darkThemeBackground = Color.FromArgb(255, 0, 0, 0);
rivate static SolidColorBrush backgroundBrush;
internal static AppTheme CurrentTheme
{
get
{
if ( backgroundBrush == null )
backgroundBrush = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;
if (backgroundBrush.Color == lightThemeBackground)
return AppTheme.Light;
else if (backgroundBrush.Color == darkThemeBackground)
return AppTheme.Dark;
return AppTheme.Dark;
}
}
BONUS: Install ThemeManager by Jeff Wilcox and switch between light and dark theme in your app with just one line of code!
http://www.jeff.wilcox.name/2012/01/phonethememanager/
Related
I'm using Template 10 and in Windows 10 Mobile when I choose the light mode, the notifications bar appears all white
and can not see the notifications, the hours, etc.
In the dark mode everything seems fine image:
How do I solve this?
I do this in my hamburger's override for UIElement CreateRootElement() after my database setup/migrations are done.
if(Template10.Utils.DeviceUtils.Current().IsPhone()){
var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
if(statusBar != null)
{
if(Application.Current.RequestedTheme == ApplicationTheme.Light)
//background && foreground or combination, and dependent on color choices
statusBar.ForegroundColor = Windows.UI.Colors.Black;
else if(Application.Current.RequestedTheme == ApplicationTheme.Dark
statusBar.ForegroundColor = Windows.UI.Colors.White;
}
}
Template10 already has a lot of the logic built in just have to know where it is. As #Jay Zuo said you have to also include the Mobile reference as well..
As #mvermef said, to solve this problem, we can set the color used in status bar according to application's theme. We can get application's theme by using Application.RequestedTheme property and set status bar's color by using properties in Status​Bar Class. For a simple example:
public MainPage()
{
InitializeComponent();
NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
var statusBar = StatusBar.GetForCurrentView();
if (statusBar != null)
{
if (Application.Current.RequestedTheme == ApplicationTheme.Light)
{
statusBar.ForegroundColor = Windows.UI.Colors.Black;
}
else if (Application.Current.RequestedTheme == ApplicationTheme.Dark)
{
statusBar.ForegroundColor = Windows.UI.Colors.White;
}
}
}
}
Please note to use Status​Bar Class, we need reference Windows Mobile Extensions for the UWP in the project.
I have created a simple Calendar application and I would like to change the color of names of the days that are displayed. I created a simple condition:
if (nameDay.Text.Equals("Sunday"))
{
daytxt.Foreground = Brushes.Red;
}
But in this case the color is changing permanently. When the name of day changes to "Monday" then color of the text is still red but it should be black. How can I fix my issue?
An else condition is missing from your if statement in order to achieve what you need.
You can do it 1 of 2 ways:
if (nameDay.Text.Equals("Sunday"))
{
daytxt.Foreground = Brushes.Red;
}
else
{
daytxt.Foreground = Brushes.Black;
}
Or
daytxt.Foreground = nameDay.Text.Equals("Sunday") ? Brushes.Red : Brushes.Black;
//This is the code in App.Xaml.cs
private void DetermineAppTheme()
{
bool value = true;
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("IsLightTheme"))
{
value = ((bool)ApplicationData.Current.LocalSettings.Values["IsLightTheme"]);
}
if (value == true)
{
this.RequestedTheme = (ApplicationTheme)ElementTheme.Light;
}
else
{
this.RequestedTheme = (ApplicationTheme)ElementTheme.Dark;
}
}
This is the code that I was previously using to change color in a Windows 8/8.1 Store app but unfortunately this is not working on windows 10 app.
In my settings page I am changing the state of theme on click using these lines of code
ApplicationData.Current.LocalSettings.Values["IsLightTheme"] = false;
MessageDialog messageDialog = new MessageDialog("Please restart the Application so that Theme change can take place");
await messageDialog.ShowAsync();
Why is this not changing the color from white to black, I don't understand. This works fine if implemented on a Windows 8 app.
My guess is that in your App.xaml.cs file, you're setting value = true; and then if(value) { // set light theme }
You need to set light theme if value is false.
I myself found the solution. First of all
this.RequestedTheme = (ApplicationTheme)ElementTheme.Light;
has to be changed to
this.RequestedTheme = (ApplicationTheme)ElementTheme.Default;
and in app.xaml Requested Theme has to be removed. and then this code will change the theme/
Updated:
How can I compare or verify Button.Background to a hexadecimal color such as {#FF008F06} etc.. in Silverlight application using C# code programmatically?
The Background property is of type Brush, so it can be a SolidColorBrush or a GradientBrush and so on.
I think you need to distinguish between the various types and perform the correct comparison.
For example:
if(Button.Background is SolidColorBrush)
{
bool isRed = ((SolidColorBrush)Button.Background).Color == Colors.Red;
}
else if (Button.Background is GradientBrush)
{
...
}
To compare a to a specific hexadecimal color:
bool flag = ((SolidColorBrush)Button.Background).Color ==
(Color)ColorConverter.ConvertFromString("#FF008F06");
EDIT Test Assert
var Greenish = new SolidColorBrush(Colors.Green);
Assert.AreEqual(Greenish.Color, ((SolidColorBrush)Button.backGround)).Color;
How to change the background color of a Listview Control in C# when the Control is disabled??
The color of a textbox can be changed when it is disabled but when a listview is disabled it goes grey and we can't apply any color to it.So is there a way to change background color of Listview control when disabled??
I've tried with overriding OnPaint, OnPaintBackground but the BackColor is still no change. Even WM_PAINT can change it but the Item backcolor is not identical to the listview BackColor. I had thought of this solution before although it is just some kind of hack but it seems to be the only working solution, the whole idea is to use a Background Image instead:
Bitmap bm = new Bitmap(listView1.ClientSize.Width, listView1.ClientSize.Height);
Graphics.FromImage(bm).Clear(listView1.BackColor);
listView1.BackgroundImage = bm;
If you want to create your own ListView which supports BackColor in disabled state, here is the class:
public class MyListView : ListView {
public override Color BackColor {
get { return base.BackColor;}
set {
base.BackColor = value;
if(BackgroundImage == null){
Bitmap bm = new Bitmap(1,1);
bm.SetPixel(0,0,value);
BackgroundImage = bm;
BackgroundImageTiled = true;
}
}
}
public override Image BackgroundImage {
get { return base.BackgroundImage; }
set {
base.BackgroundImage = value;
if(value == null){
Bitmap bm = new Bitmap(1,1);
bm.SetPixel(0,0,BackColor);
BackgroundImage = bm;
BackgroundImageTiled = true;
}
}
}
}
If someone has another solution, I would like to know also.