I want to set my resources on the app.xaml and then use it in the differents views of the app, but when I set the color the app crashes u.u, could someone help me ?
app.xaml
<Application.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#FFC107</Color>
</ResourceDictionary>
</Application.Resources>
use it in a StackLayout
<StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
Has you called InitializeComponent in the App.xaml.cs?
you need to use Static Resource,I find a good resource for you:
https://blog.xamarin.com/easy-app-theming-with-xamarin-forms-styles/
So you need to do the following:
1- Define a ResourceDictionary
at the app-level in App.xaml
<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- Use StaticResource markup extension to reference predefined resources:
<Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
BackgroundColor="{DynamicResource Primary}"
its a DynamicResource, not a StaticResource.
Here's what my code looks like for example:
App.xaml has
<Color x:Key="titleColor">Green</Color>
and a page.xaml has
TextColor="{DynamicResource titleColor}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With