Md Idiake
Md Idiake

Reputation: 83

Why am I getting this error ("Object reference not set to an instance of an object.") when trying to implement dark theme on my xamarin.forms app?

I am getting the above error when trying to implement the light/Dark modes in my app.

In app.xaml, this is my original code (and there are no issues):

<?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="glimng2.App">
    <!--
        Define global resources and styles here, that apply to all pages in your app.
    -->

<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="Primary">#2196F3</Color>
        <Style TargetType="Button">
            <Setter Property="TextColor" Value="White"></Setter>
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="{StaticResource Primary}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="#332196F3" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ResourceDictionary>
</Application.Resources>

But, when I replace it with this code below (there are no pre-build errors), the above error comes up when I am at the point of deploying to the emulator:

    <?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="glimng2.App">
<!--
    Define global resources and styles here, that apply to all pages in your app.
-->

 <Application.Resources>

    <Color x:Key="TransparentColor">Transparent</Color>

    <!-- Light colors -->
    <Color x:Key="LightPageBackgroundColor">White</Color>
    <Color x:Key="LightNavigationBarColor">WhiteSmoke</Color>
    <Color x:Key="LightPrimaryColor">WhiteSmoke</Color>
    <Color x:Key="LightSecondaryColor">Black</Color>
    <Color x:Key="LightPrimaryTextColor">Black</Color>
    <Color x:Key="LightSecondaryTextColor">White</Color>
    <Color x:Key="LightTertiaryTextColor">Gray</Color>

    <!-- Dark colors -->
    <Color x:Key="DarkPageBackgroundColor">Black</Color>
    <Color x:Key="DarkNavigationBarColor">Teal</Color>
    <Color x:Key="DarkPrimaryColor">Teal</Color>
    <Color x:Key="DarkSecondaryColor">White</Color>
    <Color x:Key="DarkPrimaryTextColor">White</Color>
    <Color x:Key="DarkSecondaryTextColor">White</Color>
    <Color x:Key="DarkTertiaryTextColor">WhiteSmoke</Color>

    <Style TargetType="NavigationPage">
        <Setter Property="BarBackgroundColor"
                Value="{AppThemeBinding Light={StaticResource LightNavigationBarColor}, Dark={StaticResource DarkNavigationBarColor}}" />
        <Setter Property="BarTextColor"
                Value="{AppThemeBinding Light={StaticResource LightSecondaryColor}, Dark={StaticResource DarkSecondaryColor}}" />
    </Style>

    <Style x:Key="ButtonStyle"
           TargetType="Button">
        <Setter Property="BackgroundColor"
                Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
        <Setter Property="TextColor"
                Value="{AppThemeBinding Light={StaticResource LightSecondaryColor}, Dark={StaticResource DarkSecondaryColor}}" />
        <Setter Property="HeightRequest"
                Value="45" />
        <Setter Property="WidthRequest"
                Value="190" />
        <Setter Property="CornerRadius"
                Value="18" />
    </Style>

    <Style x:Key="LargeLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                Value="{AppThemeBinding Light={StaticResource LightSecondaryTextColor}, Dark={StaticResource DarkSecondaryTextColor}}" />
        <Setter Property="FontSize"
                Value="30" />
    </Style>

    <Style x:Key="MediumLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                Value="{AppThemeBinding Light={StaticResource LightPrimaryTextColor}, Dark={StaticResource DarkPrimaryTextColor}}" />
        <Setter Property="FontSize"
                Value="25" />
    </Style>

    <Style x:Key="SmallLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                Value="{AppThemeBinding Light={StaticResource LightTertiaryTextColor}, Dark={StaticResource DarkTertiaryTextColor}}" />
        <Setter Property="FontSize"
                Value="15" />
    </Style>
</Application.Resources>
</Application>

So, what should I do now to remedy the situation?

Thanks.

The error occurs in the AppShell.xaml.g.cs file (the cursor stops at the 3rd brace to the end of the file) :

     //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will 
be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: 

global::Xamarin.Forms.Xaml.XamlResourceIdAttribute("glimng2.AppShell.xaml", "AppShell.xaml", 

typeof(global::glimng2.AppShell))]

namespace glimng2 {


[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("AppShell.xaml")]
public partial class AppShell : global::Xamarin.Forms.Shell {
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private global::Xamarin.Forms.FlyoutItem LoginName;
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private global::Xamarin.Forms.FlyoutItem ToolsMenu;
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private void InitializeComponent() {
        global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(AppShell));
        LoginName = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.FlyoutItem>(this, "LoginName");
        ToolsMenu = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.FlyoutItem>(this, "ToolsMenu");
    }
}

}

Upvotes: 1

Views: 1462

Answers (0)

Related Questions