c# - 在App.xaml样式中动态设置颜色值

标签 c# xaml xamarin xamarin.forms prism

在我的 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/

相关文章:

android - 在 PCL 项目中的平台之间共享字符串资源

c# - 为什么GridViewRow的 "FindControl"找不到下拉列表?

c# - 将 XSD 转换为 SQL 关系表

c# - 嵌套在 XAML 中的容器是否会影响渲染性能?

c# - 如何在 android xamarin 中动态地将按钮添加到 View ?

xamarin - 在 Xamarin.Forms 中使用 SkiaSharp 将点击事件添加到 SVG 内的元素

c# - https 代理服务器中的 firefox 证书问题

c# - 更新 LINQ 子句中的属性会破坏与 UI 的数据绑定(bind)

c# - win 8 metro app c# 多项目模板

C# Windows 通用应用程序 ListView 所选项目内容