Welcome Folks!
We are pleased to share some Xamarin Forms related info
that may help you.Xamarin Forms is a cross-platform framework that allows us to
share both UI code and business logic across platforms.Guys you can refer to
https://visualstudio.microsoft.com/xamarin/ in detail.
Navigation bar is a very important part of Mobile apps that gives description of your entire app theme and help user to understand motive of this page.Here we are to get some basic information how we can use it as per our requirement. What we have faced previously is we have to set same navigation color or one theme color in our native classes that is style.cs for Android and App Delegate for iOS. But now we have option to set it as per our pages requirement and choice. Lets go through each step and reach to our requirement:
Step 1: Create Interface in your Portable project.
Now its time to use it wherever you want by defining it your xaml.cs class.
Navigation bar is a very important part of Mobile apps that gives description of your entire app theme and help user to understand motive of this page.Here we are to get some basic information how we can use it as per our requirement. What we have faced previously is we have to set same navigation color or one theme color in our native classes that is style.cs for Android and App Delegate for iOS. But now we have option to set it as per our pages requirement and choice. Lets go through each step and reach to our requirement:
Step 1: Create Interface in your Portable project.
public interface IStatusBarStyleInterface
{
void SetLightTheme();
void SetDarkTheme();
}
Step 2: Create StatusBarStyleManager Renderer in your Xamarin Android Renderer folder.{
void SetLightTheme();
void SetDarkTheme();
}
[assembly: Xamarin.Forms.Dependency(typeof(StatusBarStyleManager))]
namespace Sample.Droid.Renderer
{
public class StatusBarStyleManager : IStatusBarStyleInterface
{
public void SetDarkTheme()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.SetStatusBarColor(Android.Graphics.Color.Blue);
});
}
}
public void SetLightTheme
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.SetStatusBarColor(Android.Graphics.Color.White);
});
}
}
Window GetCurrentWindow()
{
var window = CrossCurrentActivity.Current.Activity.Window;
return window;
}
}
}
Step 3: Create StatusBarStyleManager Renderer in your Xamarin iOS Renderer folder.namespace Sample.Droid.Renderer
{
public class StatusBarStyleManager : IStatusBarStyleInterface
{
public void SetDarkTheme()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.SetStatusBarColor(Android.Graphics.Color.Blue);
});
}
}
public void SetLightTheme
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.SetStatusBarColor(Android.Graphics.Color.White);
});
}
}
Window GetCurrentWindow()
{
var window = CrossCurrentActivity.Current.Activity.Window;
return window;
}
}
}
[assembly: Dependency(typeof(StatusBarStyleManager))]
namespace Sample.iOS.Renderer
{ public class StatusBarStyleManager : IStatusBarStyleInterface
{
public void SetDarkTheme()
{
try
{
Device.BeginInvokeOnMainThread(() =>
{
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
catch (Exception ex)
{
}
}
UIViewController GetCurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
return vc;
}
public void SetLightTheme()
{
try
{
Device.BeginInvokeOnMainThread(() =>
{
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent,false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
catch (Exception ex)
{
}
}
}
}
namespace Sample.iOS.Renderer
{ public class StatusBarStyleManager : IStatusBarStyleInterface
{
public void SetDarkTheme()
{
try
{
Device.BeginInvokeOnMainThread(() =>
{
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
catch (Exception ex)
{
}
}
UIViewController GetCurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
return vc;
}
public void SetLightTheme()
{
try
{
Device.BeginInvokeOnMainThread(() =>
{
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent,false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
catch (Exception ex)
{
}
}
}
}
Now its time to use it wherever you want by defining it your xaml.cs class.
DependencyService.Get().SetLightTheme();
Thanks guys, here we learned how to customize your navigation bar as per your need.
Comments
Post a Comment