Xamarin.forms Xamarin表单在启动时加载资源

Xamarin.forms Xamarin表单在启动时加载资源,xamarin.forms,Xamarin.forms,我有一个Xamarin Forms应用程序,带有Android和IOS的MvvmCross,我想添加一个黑色主题。我的想法是,必须为黑暗主题或光明主题编写带有资源的词典,并在启动时加载我需要的资源 在MVX应用程序中注册依赖项后,我添加了以下内容: if (Mvx.IoCProvider.Resolve<ISettingsManager>().Theme == AppTheme.Dark) { Application.Curre

我有一个Xamarin Forms应用程序,带有Android和IOS的MvvmCross,我想添加一个黑色主题。我的想法是,必须为黑暗主题或光明主题编写带有资源的词典,并在启动时加载我需要的资源

在MVX应用程序中注册依赖项后,我添加了以下内容:

        if (Mvx.IoCProvider.Resolve<ISettingsManager>().Theme == AppTheme.Dark)
        {
            Application.Current.Resources.Add(new ColorsDark());
        }
        else
        {
            Application.Current.Resources.Add(new ColorsLight());
        }

我是否必须在代码中添加另一部分,或者这是一种错误的方法?

如果你想在应用程序加载时加载某些内容,那么你必须在app.xaml.cs中对其进行编码

protected override void OnStart ()
    {
        if (Mvx.IoCProvider.Resolve<ISettingsManager>().Theme == AppTheme.Dark)
        {
            Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(ContentPage))
            {
                ApplyToDerivedTypes = true,
                Setters = {
                new Xamarin.Forms.Setter { Property = ContentPage.BackgroundImageProperty, Value = "bkg7.png"},
            }
            });
        }
        else
        {
            Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(ContentPage))
            {
                ApplyToDerivedTypes = true,
                Setters = {
                new Xamarin.Forms.Setter { Property = ContentPage.BackgroundImageProperty, Value = "bkg7.png"},
            }
            });
        }
    }
protected override void OnStart()
{
if(Mvx.IoCProvider.Resolve().Theme==AppTheme.Dark)
{
Application.Current.Resources.Add(新的Xamarin.Forms.Style(typeof(ContentPage))
{
ApplyToDerivedTypes=true,
设定器={
新的Xamarin.Forms.Setter{Property=ContentPage.BackgroundImageProperty,Value=“bkg7.png”},
}
});
}
其他的
{
Application.Current.Resources.Add(新的Xamarin.Forms.Style(typeof(ContentPage))
{
ApplyToDerivedTypes=true,
设定器={
新的Xamarin.Forms.Setter{Property=ContentPage.BackgroundImageProperty,Value=“bkg7.png”},
}
});
}
}

在这段代码中,我将设置所有页面的背景图像。希望您能从这段代码中得到启发。

我个人根本不喜欢这种方法。我所做的:有一个静态类,包含在静态字段中定义的所有颜色、大小等。在应用程序启动时或更改外观后重新加载应用程序时,只需为此ui定义静态类调用ex:
UiSettings.Init()
,如下所示:

public static class UiSettings
{       
      public static Init()
      {
            if (Settings.AppSettings.Skin=="butterfly")
            {
             ColorButton = Color.Blue;
             TitleSize= 12.0;
            }
            else
            if (Settings.AppSettings.Skin=="yammy")
            {
             ColorButton = Color.Red;
             if (Core.IsAndroid)
                ButtonsMargin = new Thickness(0.5,0.6,0.7,0.8);
            }
            // else just use default unmodified field default values 
        }

        public static Color ColorButton = Color.Green;
        public static Thickness ButtonsMargin = new Thickness(0.3,0.3,0.2,0.2);
        public static double TitleSize= 14.0;

}
在XAML使用示例中:

 Color= "{x:Static xam:UiSettings.ColorProgressBack}"
Color = UiSettings.ColorProgressBack;
在代码使用示例中:

 Color= "{x:Static xam:UiSettings.ColorProgressBack}"
Color = UiSettings.ColorProgressBack;
更新:
请记住,如果您从不同的程序集访问静态类,您可能会使用默认值访问它的新副本,而Init()并没有发生,如果您也面临这样的情况,请从此程序集调用Init()。

谢谢您的回答。我将代码移动到app.xaml.cs,但这并没有改变任何事情。您可以直接在代码中定义样式。有没有办法用这种方式加载Ressource字典?有。只需将资源字典放在app.xamlI中,就可以动态加载它们。当我在app.xaml中加载它们时,它们是静态的,不是吗?使用设置插件保存您的样式或颜色,并在应用加载时加载它们。谢谢。到目前为止效果还不错。但是,当您再次调用它时,如何实现它的重新加载呢?我尝试了DynamicResources而不是x:Static,但没有成功。好吧,我有点明白了。当我设置默认值时,它会在启动时将其设置为正确。但我仍然需要重新启动应用程序以应用新选择的样式是,我通过设置mainpage=PageShowingStarting,然后设置mainpage=YourUsualInput来重新启动应用程序。不要忘了在它之前做一次清理,取消订阅消息等等,这对于在运行时更改语言非常有用,这两点你都是对的。我在另一个程序集中使用了静态类。此外,我还必须将其放在app.xaml.cs中的InitializeComponent之前,以便在初始化样式时可以使用颜色。