Today I spent a lot of time to understand why my style doesn’t work.
<Application xmlns="https://xamarin.com/schemas/2014/forms" xmlns:x="https://schemas.microsoft.com/winfx/2009/xaml" x:Class="myProject.App"> <Application.Resources> <ResourceDictionary> <Style x:Key="WarmGreyLine" TargetType="BoxView"> <Setter Property="HeightRequest" Value="1" /> <Setter Property="HorizontalOptions" Value="Fill" /> <Setter Property="Color" Value="#EEE9E5" /> <Setter Property="Margin" Value="0,10,0,10" /> </Style> </ResourceDictionary> </Application.Resources> </Application>
I followed a video about it on Xamarin University. Everything was the same. They say you can copy your style from a ContentPage.Resources and page in the Application.Resources section
<ContentPage.Resources> <ResourceDictionary> <Style x:Key="WarmGreyLine" TargetType="BoxView"> <Setter Property="HeightRequest" Value="1" /> <Setter Property="HorizontalOptions" Value="Fill" /> <Setter Property="Color" Value="#EEE9E5" /> <Setter Property="Margin" Value="0,10,0,10" /> </Style> </ResourceDictionary> </ContentPage.Resources>
The XAML is correct but if you execute the code you receive an error like:
Inner Exception: Position 38:14. StaticResource not found for key WarmGreyLine Message: Exception has been thrown by the target of an invocation.
The Solution
The is a little thing in the video they forgot to say!
In the App.xaml.cs you have to call InitializeComponent();
namespace myInventories { public partial class App : Application { public App() { InitializeComponent(); } } }
Happy coding!