我正在尝试从我的毛伊应用程序中的颜色文件(在样式下)访问颜色:
<Color x:Key="LiteGreen">#7DD2CD</Color>
<Color x:Key="VeryLiteGreen">#a4dbda</Color>
<Color x:Key="LiteBlue">#c3e1ed</Color>
Run Code Online (Sandbox Code Playgroud)
如果我想访问 xaml 中的颜色,这就是我的做法以及对我有用的方法:
BorderColor="{StaticResource LiteGreen}"
Run Code Online (Sandbox Code Playgroud)
现在我尝试在代码中访问相同的颜色:
var color = this.Resources["LiteGreen"] as Color;
Run Code Online (Sandbox Code Playgroud)
失败并显示“LiteGreen 不在资源字典内”
我尝试了许多不同的变化,但没有成功。
编辑:
我的 app.xaml(基本)。
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Skillbased"
x:Class="Skillbased.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
Liy*_*SFT 16
您可以尝试以下代码:
\nif (App.Current.Resources.TryGetValue("LiteGreen", out var colorvalue))\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82var greencolor = (Color)colorvalue;\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
4620 次 |
最近记录: |