在我的 Xamarin Forms 应用程序中,我有多种样式,包括颜色。
应用程序.xaml:
<prism:PrismApplication.Resources>
<ResourceDictionary>
<Color x:Key="primary_colour">#000a3d</Color>
...
我在其他 .xaml 页面上使用:
<ContentPage Title="Menu" BackgroundColor="{StaticResource primary_colour}">
我的问题是,如何动态更改 App.xaml 中的颜色值 (#000a3d)(根据登录的客户端不同)? 我可以从 App.xaml.cs 文件绑定(bind)该值吗?
最佳答案
使用DynamicResource
,来自Xamarin documentaion它说:
The DynamicResource markup extension is similar to the StaticResource markup extension in that both use a dictionary key to fetch a value from a ResourceDictionary. However, while the StaticResource performs a single dictionary lookup, the DynamicResource maintains a link to the dictionary key. Therefore, if the dictionary entry associated with the key is replaced, the change is applied to the visual element. This enables runtime style changes to be made in an application.
因此您的页面将是:
<ContentPage Title="Menu" BackgroundColor="{DynamicResource primary_colour}">
更改颜色的代码是:
Application.Current.Resources["primary_colour"] = Color.Green;
关于c# - 在App.xaml样式中动态设置颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45481264/