Xamarin.Forms-在黑暗与黑暗之间切换运行时轻主题 [英] Xamarin.Forms - Switching between Dark & Light themes at run time
问题描述
1-我们可以在Light& amp;之间切换的任何想法使用2.3.x版中引入的Xamarin.Forms主题的深色主题(下面的链接).任何解决方法? https://developer.xamarin.com/guides/xamarin-forms/用户界面/主题/
1 - Any idea if we can switch between Light & Dark themes using the Xamarin.Forms themes introduced in version 2.3.x (link below). Any workaround? https://developer.xamarin.com/guides/xamarin-forms/user-interface/themes/
2-自发布以来,我还看到此版本处于预览状态.有什么问题,我们不能在生产中使用它吗?
2 - Also I see this release is in preview ever since it was introduced. Are there any issues and we cannot use it in production?
推荐答案
接受的答案不符合假设您已经安装了软件包,请 Xamarin.Forms.Themes.Base , Xamarin.Forms.Themes.Light 和 Xamarin.Forms.Themes.Dark ,并且您的 App.xaml 看起来像
Assuming you've installed the packages, Xamarin.Forms.Themes.Base, Xamarin.Forms.Themes.Light, and Xamarin.Forms.Themes.Dark, and your App.xaml looks like,
<?xml version="1.0" encoding="utf-8" ?>
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:light="clr-namespace:Xamarin.Forms.Themes;assembly=Xamarin.Forms.Theme.Light"
xmlns:dark="clr-namespace:Xamarin.Forms.Themes;assembly=Xamarin.Forms.Theme.Dark"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyNamespace.MyApp">
<Application.Resources>
<ResourceDictionary MergedWith="light:LightThemeResources">
...
</ResourceDictionary>
</Application.Resources>
</Application>
您可以使用以下命令在运行时更改主题:
you can change the theme at run time with the following:
public enum Themes
{
Dark,
Light
}
var origin = App.Current.Resources;
switch (theme)
{
case Themes.Dark:
origin.MergedWith = typeof(DarkThemeResources);
break;
case Themes.Light:
origin.MergedWith = typeof(LightThemeResources);
break;
}
这篇关于Xamarin.Forms-在黑暗与黑暗之间切换运行时轻主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!