Home > database >  Merged ResourceDictionary causes a null reference exception
Merged ResourceDictionary causes a null reference exception

Time:04-06

In Xamarin.Forms I get a runtime Exception when I use a Merged ResourceDictionary in another ResourceDirectory when applied to a FlyoutHeader Template.

When I remove the Merged Dictionary and add the color keys directly in the FlyoutHeaderStyle.xaml Dictionary then I don't get a null reference exception.

Any help would be really appreciated.

\>>> Dictionary Colors.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <!-- Colours Light Theme -->
    <Color x:Key="ColorForegroundText">#414141</Color>
    <Color x:Key="ColorForegroundAction">#00698F</Color>
    <Color x:Key="ColorForegroundFocus">#9163DE</Color>
    <Color x:Key="ColorForegroundMuted">#636363</Color>
    <Color x:Key="ColorForegroundBorder">#808080</Color>

    <Color x:Key="ColorBackground">#FFFFFF</Color>
    <Color x:Key="ColorBackgroundShade">#F5F5F5</Color>
    <Color x:Key="ColorBackgroundAlt">#EBEBEB</Color>
    <Color x:Key="ColorBackgroundAltShade">#E0E0E0</Color>

</ResourceDictionary>

\>>> Dictionary FlyoutHeaderStyle.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <!--This Causes a null reference exception-->
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Styles/Colors.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!--Direct Color keys works-->
    <Color x:Key="ColorBackgroundAAA">Green</Color>

    <Style TargetType="Grid">
        <Setter Property="BackgroundColor" Value="Black"/>
        <Setter Property="HeightRequest" Value="80"/>
    </Style>

    <Style TargetType="Label">
        <Setter Property="TextColor" Value="{StaticResource Key=ColorBackgroundAAA}"/>
    </Style>


</ResourceDictionary>

>>> FlyoutHeaderControl

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="knibz.Controls.FlyoutHeaderControl">
    <ContentView.Content>       
        <Grid>
            <Grid.Resources>
                  <ResourceDictionary Source="/Styles/FlyoutHeader.xaml" />
            </Grid.Resources>
            <Label Text="Knibz"/>
        </Grid>
    </ContentView.Content>

</ContentView>

Stacktrace " at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 \n at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 \n at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)"

I tried debugging the problem but still can not find the problem, I already cleaned, rebuild the solution

CodePudding user response:

The issue was the way you tried to merge the dictionary. Please add it by class name that may resolve the issue.Please refer to this thread:

https://docs.microsoft.com/en-us/answers/questions/310767/issue-with-mergeddictionaries-after-updating-to-xa.html

  • Related