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.
Related
I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.
Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.
This is the error :
Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.
It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.
Can someone help me out here with this.
Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.
The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.
public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>
{
string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (Control == null && isRegist != true)
{
CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;
}
}
private void CreateNativeAdControl()
{
if (adView != null)
return;
var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")
{
width = 728;
height = 90;
}
// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl
{
ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
};
}
}
//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/
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/
I'm making an app for Windows Phone, I've been trying for ages to get the InputScope of the main text box to change when the orientation is changed to landscape (so that the keyboard takes up less space in landscape without the autocorrect bar), and back again.
I experimented with a second text box and hiding the others upon an orientation change, but this did not work neatly. :P
Try as I might I can't get this to work and can find no way to change the InputScope value after the OrientationChangedEvent argument, which has worked nicely in changing the position of the elements of the page around orientations.
I'm still fairly new to developing apps with C# and XAML, and I hope there's a nice easy way to set the InputScope of my text box that one of you awesome people could show me!
-EDIT : Here's the event handler, everything inside there works absolutely fine, but any way I try to add anything to do with InputScope does not work :(
private void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
{
//Portrait
PlaceholderText.FontSize = 29.333;
PlaceholderText.Padding = new Thickness (0,0,0,0);
MainTweet.FontSize = 29.333;
MainTweet.Padding = new Thickness (12,8,12,8);
Counter.Margin = new Thickness (0,212,28,0);
}
else
{
//Landscape
PlaceholderText.FontSize = 23;
PlaceholderText.Padding = new Thickness (8,0,0,0);
MainTweet.FontSize = 22;
MainTweet.Padding = new Thickness (16,8,180,0);
Counter.Margin = new Thickness (0,-18,28,0);
}
}
MainTweet.Text is the textbox that the keyboard is focusing on by default, when the page is changed to landscape I'd love to be able to change this from the "Search" InputScope to another, probably "URL". The stuff currently in there rearranges elements on the page nicely when the orientation is changed, I appreciate it might not look that neat...
There are multiple "orientation" states in the enumeration - not just Portrait and Landscape. The following worked for me to change the scope (on Windows Phone 7.5 emulator):
if (e.Orientation == PageOrientation.Landscape
|| e.Orientation == PageOrientation.LandscapeRight
|| e.Orientation == PageOrientation.LandscapeLeft)
{
InputScope inputScope = new InputScope();
InputScopeName inputScopeName = new InputScopeName();
inputScopeName.NameValue= InputScopeNameValue.Url;
inputScope.Names.Add(inputScopeName);
textBox1.InputScope = inputScope;
}
So you'd drop that into your MainPage_OrientationChanged event handler.
I've figured out how to set the tint for the title bar and have set a background image for all my views, but for the life of me I cannot figure out how to set the default UILabel color for section headers and such. I don't want to riddle my code setting all my colors to UIColor.Black by hand. Is there any way to get a list of different UIElements and the way to set defaults (colors, fonts, sizes) for each? I'm specifically interested in the color of labels, but any other information would be extremely helpful for the future.
Starting with iOS 5 there's an UIAppearance class to handle global appearance of many UI elements.
The way it works with MonoTouch is that an inner type, called Appearance, exists on those types. You can directly set the properties from it (easy with MonoDevelop's code completion). E.g.
UILabel.Appearance.BackgroundColor = UIColor.Blue;
Sadly it does not cover everything you might want for every control (nor will it work before iOS 5).
This is kind of hacky, but this does the trick, and it works with all kinds of other UI controls that subclass UIView. This assumes you have your own subclassed DialogViewController (mine has lots of handy time-saving things in it, and all my views subclass my DVC to do work).
protected void ForAllSubviewUIControlsOfType<TUIType>(Action<TUIType, int> actionToPerform) where TUIType : UIView
{
processSubviewUIControlsOfType<TUIType>(this.View.Subviews, actionToPerform, 0);
}
private void processSubviewUIControlsOfType<TUIType>(UIView[] views, Action<TUIType, int> actionToPerform, int depth) where TUIType : UIView
{
if (views == null)
return;
if (actionToPerform == null)
return;
foreach (UIView view in views)
{
if (view.GetType () == typeof(TUIType))
{
actionToPerform((TUIType)view, depth);
}
if (view.Subviews != null)
{
foreach (UIView subview in view.Subviews)
{
processSubviewUIControlsOfType<TUIType>(view.Subviews, actionToPerform, depth + 1);
}
}
}
}
To solve the original problem and change the text colors of all labels that are in sections, this is what you need to do:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
ForAllSubviewUIControlsOfType<UILabel>((label, depth) => {
if ( /*label.Superview is UIView && label.Superview.Superview is UITableView*/ depth == 1 )
{
label.TextColor = UIColor.Black;
}
});
}
The commented out part is what was determining if the label existed inside a section, before I decided to return an int depth count to see if it was on the view.
EDIT: Seems as if this isn't the perfect solution... the depth count varies for these elements. I'll have to figure out something better in the next day or two.