简体   繁体   English

如何在Xamarin Forms中从ContentPage保存App.xaml中的资源?

[英]How to save resource in App.xaml from ContentPage in Xamarin Forms?

I have a Xamarin Forms App which needs theming, basically what i want is that when i open my app there is a toolbar which has a theme page and a theme page has some buttons, when i click on the button, it will change the background image of the entire app. 我有一个需要主题的Xamarin Forms应用程序,基本上我想要的是,当我打开我的应用程序时,有一个工具栏,其中有一个主题页面,而主题页面有一些按钮,当我单击按钮时,它将改变背景整个应用的图像。 The issue is when i close and re-opens the app, the image is not there. 问题是当我关闭并重新打开应用程序时,图像不存在。

Here is my App.xaml 这是我的App.xaml

    <ResourceDictionary>

        <!-- Styles -->
        <!-- Styles -->

    </ResourceDictionary>

</Application.Resources>

The code in the ContentPage button click 单击ContentPage按钮中的代码

Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(ContentPage))
            {
                ApplyToDerivedTypes = true,
                Setters = {
                new Xamarin.Forms.Setter { Property = ContentPage.BackgroundImageProperty, Value = "Assets/bkg8.png"},
            }
            });

The code is working fine but the current style is not saving itself in App.xaml. 该代码可以正常工作,但是当前样式无法将其自身保存在App.xaml中。 I want it to be saved and load every time the app starts. 我希望每次应用启动时都保存并加载它。

Thanks. 谢谢。

You can use Xamarin property storage to store your valuable and then change the resource on next startup. 您可以使用Xamarin属性存储来存储您的宝贵资产,然后在下次启动时更改资源。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/application-class#properties-dictionary https://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/app-fundamentals/application-class#properties-dictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM