My *_*per 5 xamarin.forms xamarin.forms-styles
1-如果我们可以使用版本2.3.x中引入的Xamarin.Forms主题在浅色和深色主题之间进行切换(请参见下面的链接),那么请教。任何解决方法? https://developer.xamarin.com/guides/xamarin-forms/user-interface/themes/
2-自发布以来,我还看到此版本处于预览状态。有什么问题,我们不能在生产中使用它吗?
接受的答案不符合Microsoft展示的约定。
假设您已经安装了Xamarin.Forms.Themes.Base,Xamarin.Forms.Themes.Light和Xamarin.Forms.Themes.Dark软件包,并且您的App.xaml看起来像,
<?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>
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令在运行时更改主题:
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;
}
Run Code Online (Sandbox Code Playgroud)
是的,可以,通过在 App.cs 类中的代码添加资源,您可以切换要使用的主题。
在类构造函数中设置默认主题:
Resources = new Xamarin.Forms.Themes.DarkThemeResources ();
Run Code Online (Sandbox Code Playgroud)
然后,您在此类中公开一个方法,SwitchTheme()您将在其中分配另一个主题:
public void SwitchTheme ()
{
if (Resources?.GetType () == typeof (DarkThemeResources))
{
Resources = new LightThemeResources ();
return;
}
Resources = new DarkThemeResources ();
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您定义了样式,则上面的代码将不起作用,因为它将覆盖您的资源字典。为此,您可以基于这两个创建自己的主题,添加您的定义样式并使用您的实现进行切换。
| 归档时间: |
|
| 查看次数: |
2608 次 |
| 最近记录: |