【问题标题】:Xamarin Forms Load Resources on startupXamarin Forms 在启动时加载资源
【发布时间】:2018-11-09 21:36:55
【问题描述】:

我有一个适用于 Android 和 IOS 的带有 MvvmCross 的 Xamarin Forms 应用程序,我想添加一个深色主题。我的想法是必须使用包含深色或浅色主题的资源的字典,并在启动时加载我需要的那个。

我在 MvxApplication 中注册了依赖项后添加了这个:

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

ColorsDarkColorsLight 是我的资源字典。之后我可以在Application.Current.Resources.MergedDictionaries 下看到新字典,但控件无法找到看起来的资源。但是,当我将它添加到 App.xaml 时它确实有效

      <ResourceDictionary Source="Style/ColorsDark.xaml" />

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

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    个人完全不喜欢这种方法。我做什么:有一个静态类,所有颜色、大小等都在静态字段中定义。在应用程序启动或更改皮肤后重新加载应用程序时,只需调用 ex: UiSettings.Init() 即可为此 ui 定义静态类,如下所示:

    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;
    

    更新: 请记住,如果您从不同的程序集访问静态类,您可能会使用未发生 Init() 的默认值访问它的新副本,如果您也遇到这种情况,请从该程序集中调用 Init()。

    【讨论】:

    • 谢谢。到目前为止效果很好。但是,当您再次调用它时,您如何实现它会重新加载?我尝试了 DynamicResources 而不是 x:Static 但没有奏效。
    • 好吧,我想通了。当我设置一个默认值时,它会在启动时设置它。但我仍然需要重新启动应用程序才能应用新的选定样式-
    • 是的,我通过设置 mainpage = pageShowingRestarting,然后 mainpage = yourUsualMainPage 重新启动应用程序。不要忘记在它之前进行清理,取消订阅消息等
    • 对于在运行时更改语言也很有用
    • 这两点你都是对的。我在不同的程序集中有静态类。我还必须将它放在 app.xaml.cs 中的 InitializeComponent 之前,以便在样式初始化时颜色可用。
    【解决方案2】:

    如果您希望在应用加载时加载某些内容,则必须在 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"},
                }
                });
            }
        }
    

    在这段代码中,我设置了我所有页面的背景图像。希望您能从这段代码中获得灵感。

    【讨论】:

    • 感谢您的回答。我将代码移至 app.xaml.cs ,但这并没有改变任何东西。您直接在代码中定义样式。有没有办法以这种方式加载资源字典?
    • 是的。只需将资源字典放入 app.xaml
    • 我的意思是动态加载它们。当我在 app.xaml 中加载它们时,它们是静态的,不是吗?
    • 使用设置插件保存您的样式或颜色,并在您的应用加载时加载它们。
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    相关资源
    最近更新 更多