如何从全球资源中使用颜色?
how to user color from global resources?
我想在 app.xaml 上设置我的资源,然后在应用程序的不同视图中使用它,但是当我设置颜色时应用程序崩溃 u.u,有人可以帮助我吗?
app.xaml
<Application.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#FFC107</Color>
</ResourceDictionary>
</Application.Resources>
在 StackLayout 中使用它
<StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
BackgroundColor="{DynamicResource Primary}"
它是动态资源,而不是静态资源。
例如,我的代码如下所示:
App.xaml 有
<Color x:Key="titleColor">Green</Color>
并且 page.xaml 有
TextColor="{DynamicResource titleColor}"
你需要使用静态资源,我给你找了个好资源:
https://blog.xamarin.com/easy-app-theming-with-xamarin-forms-styles/
因此您需要执行以下操作:
1- 在 App.xaml
中的应用级别定义一个 ResourceDictionary
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MonkeyTweet.App">
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
</Application>
2- 使用 StaticResource 标记扩展来引用预定义资源:
<Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
你在App.xaml.cs中调用了InitializeComponent了吗?
我想在 app.xaml 上设置我的资源,然后在应用程序的不同视图中使用它,但是当我设置颜色时应用程序崩溃 u.u,有人可以帮助我吗?
app.xaml
<Application.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#FFC107</Color>
</ResourceDictionary>
</Application.Resources>
在 StackLayout 中使用它
<StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
BackgroundColor="{DynamicResource Primary}"
它是动态资源,而不是静态资源。
例如,我的代码如下所示:
App.xaml 有
<Color x:Key="titleColor">Green</Color>
并且 page.xaml 有
TextColor="{DynamicResource titleColor}"
你需要使用静态资源,我给你找了个好资源:
https://blog.xamarin.com/easy-app-theming-with-xamarin-forms-styles/
因此您需要执行以下操作:
1- 在 App.xaml
中的应用级别定义一个ResourceDictionary
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MonkeyTweet.App">
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
</Application>
2- 使用 StaticResource 标记扩展来引用预定义资源:
<Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
你在App.xaml.cs中调用了InitializeComponent了吗?