问题描述
我已经声明了一种将在我的应用程序中大量使用的颜色,并且我希望能够在页面中调用该特定颜色.这种颜色很可能用于 XAML 以及隐藏的代码.在 App.xaml 中,我有
I have declared a color that I will be using a lot in my application, and I would like to be able to call that specific color within a page. This color will most likely be used in in XAML as well as code behind. In App.xaml I have
<Color x:Name="Blue" A="255" R="35" G="85" B="145"/>
但是我如何在我的页面 UI 和代码中调用它?
But how would I call this in my Page's UI and code behind?
其实要注意,上面在App.xaml中设置颜色会在启动时出现调试错误?
Actually to note, the above setting a color in App.xaml gives a debugging error on startup?
public App()
{
// Standard XAML initialization
InitializeComponent(); //XamlParseException occurs here
...
}
编辑**
SolidColorBrush 更新不起作用
Update for SolidColorBrush not working
我有一个在 XAML 中声明的 Slider 控件和两个 ToggleSwitch 控件,我希望在 XAML 中更改 Slider 前景并在后面的代码中更改 ToggleSwitch 控件.两者都不起作用
I have a Slider control and two ToggleSwitch controls declared in XAML, and I wish to change the Slider foreground in XAML and change the ToggleSwitch controls in code behind. Neither is working
应用程序.xaml
<Color x:Key="ThemeColorBlue" A="255" R="35" G="85" B="145"/>
<SolidColorBrush x:Key="ThemeBrushBlue" Color="{StaticResource ThemeColorBlue}"/>
因此,当尝试在 XAML 中更改 Slider 控件前景时,使用
and so when attempting to change the Slider control foreground in XAML I get no errors using
Foreground="{StaticResource ThemeBrushBlue}"
但是在后面的代码中更改 ToggleSwitch 前景时,我收到一条错误消息,指出 无法将类型对象"隐式转换为System.Windows.Media.Brush"
but when changing the ToggleSwitch foreground in code behind I get an error stating Cannot implicitly convert type 'object' to 'System.Windows.Media.Brush'
this.ToggleSwitch.SwitchForeground = Application.Current.Resources["ThemeBrushBlue"];
推荐答案
我觉得问题是
<SolidColorBrush x:Key="ThemeBrushBlue" Color="{StaticResource ThemeColorBlue}"/>
只需重复颜色即可:
<SolidColorBrush x:Key="ThemeBrushBlue" Color="#235591"/>
这篇关于如何从页面中的 App.xaml 获取颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!