合并的 ResourceDictionary 导致空引用异常

Merged ResourceDictionary causes a null reference exception

在 Xamarin.Forms 中,当我在应用到 FlyoutHeader 模板时在另一个 ResourceDirectory 中使用 Merged ResourceDictionary 时出现运行时异常。

当我删除合并字典并直接在 FlyoutHeaderStyle.xaml 字典中添加颜色键时,我没有得到空引用异常。

任何帮助将不胜感激。

\>>> 词典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>

\>>> 词典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)"

我试过调试问题还是找不到问题,我已经清理了,重建解决方案

问题在于您尝试合并词典的方式。请通过 class 名称添加它可以解析 issue.Please 参考此 thread:

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