Search code examples
c#xamlxamarinstyles

Xamarin.Form Previewer : An Exception occurred while rendering the control


I'm new in Xamarin. I want to learn about <Style> tag. I have problem When I start adding tag on App.xaml file,warning message appear.I don't know what mistake that i make. Hope you guys can help me solve this problem.Thank you in advance.

Error Message

enter image description here

App.xaml

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecordApp.App">
    <Application.Resources>
        <!-- Application resource dictionary -->
        <ResourceDictionary>
            <Color x:Key="blueColor">#1E90FF</Color>
            <Color x:Key="whiteColor">#FFFFFF</Color>
            <Style>
                <Setter Property="BackgroundColor" Value="{StaticResource blueColor}" />
                <Setter Property="TextColor" Value="{StaticResource whiteColor}"/>
            </Style>
        </ResourceDictionary>

    </Application.Resources>
</Application>

Solution

  • A Style tag must always specify the TargetType. So if, for instance, your style targets labels, you would have to change your code to:

    <Style TargetType="Label">
        <Setter Property="BackgroundColor" Value="{StaticResource blueColor}" />
        <Setter Property="TextColor" Value="{StaticResource whiteColor}"/>
    </Style>
    

    By the way, the Xamarin.Forms previewer is a pain in the a**. The error message you're getting probably won't go away even after you fixed it. I had to do a complete clean & rebuild, then open the previewer for a different page, close it and open it again for the page I wanted before it would display anything.

    You may be better off not using it.