Xamarin.Forms - 在Dark和amp;之间切换在运行时轻的主题

时间:2017-03-24 11:07:09

标签: xamarin.forms xamarin.forms-styles

1 - 我们是否可以在Light& amp;使用版本2.3.x中引入的Xamarin.Forms主题的黑暗主题(链接如下)。任何解决方法? https://developer.xamarin.com/guides/xamarin-forms/user-interface/themes/

2 - 此外,我发现此版本自推出以来一直处于预览状态。有任何问题,我们不能在生产中使用它吗?

2 个答案:

答案 0 :(得分:4)

接受的答案并不符合https://github.com/ipfs/js-ipfs#use-in-the-browser

假设您已经安装了软件包,the convention demonstrated by MicrosoftXamarin.Forms.Themes.BaseXamarin.Forms.Themes.Light,并且您的 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>

您可以使用以下命令在运行时更改主题:

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;
}

答案 1 :(得分:2)

是可以的,在App.cs类中按代码添加资源,可以切换要使用的主题。

在类构造函数中,您设置默认主题:

{ '0': { msg: 'helloworld1' } }
{ '0': { msg: 'helloworld2' } }


undefined
undefined

然后,您可以在此类Resources = new Xamarin.Forms.Themes.DarkThemeResources (); 中公开一个方法,您将在其中分配另一个主题:

SwitchTheme()

请注意,如果您已定义样式,则上述代码将无法正常工作,因为它将覆盖您的资源字典。为此,您可以基于这两个创建自己的主题,添加定义样式并使用您的实现进行切换。

相关问题