.NET MAUI中未找到key对应的StaticResource。

5
当我将 mainpage 注入到 App 类构造函数中时,我会收到 StaticResource not found for key 的错误提示,但如果我不在 App 构造函数中注入 Mainpage 则可以正常工作。
我有一个全局资源主题文件,我在 App.xaml.cs 中调用它,在那里我声明了静态资源。
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Themes/LightTheme.xaml" /> <!--Theme file-->
            <ResourceDictionary Source="Themes/DarkTheme.xaml" /> <!--Theme file-->
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这是我的 App.cs 文件:
public App(MainPage mainPage)
{

    InitializeComponent();

    MainPage = mainPage;

}

以下代码位于MainPage.xaml文件中:
    <StackLayout BackgroundColor="{StaticResource SecondaryBackroundColor}" Grid.Row="0">
        <Image 
            Source="ic_logo.png"
            SemanticProperties.Description="Cute dot net bot waving hi to you!"
            HeightRequest="200"
            HorizontalOptions="Center"  VerticalOptions="CenterAndExpand"/>

    </StackLayout>

我已将MainPage添加到mauiprogram.cs类中

builder.Services.AddTransient<MainPage>();


不确定它是否解决了你的问题,但是似乎你的xaml中有一个拼写错误:Background中缺少一个'g'。如果你的theme.xaml中没有这个错别字,那么就可以解释你的错误了。 - TheTanic
不是打错了,我已经仔细检查过了。 - Phuluso Ramulifho
4个回答

4
我创建了一个新项目来测试你的代码,结果遇到了相同的错误。然后我添加了两个断点,一个在MainPage的构造方法上,另一个在App.cs的构造方法上。
当我使用builder.Services.AddTransient<MainPage>();时,我发现MainPage的构造方法先于App.cs执行,因此出现了错误。
最后,你可以在github上关注这个问题:Can't use App.xaml resources after Dependency Injection .NET MAUI。或者只需使用DynamicResource而不是StaticResource,例如:
 <StackLayout BackgroundColor="{DynamicResource SecondaryBackroundColor}"

我已经尝试过了,使用DynamicResource是有效的。


使用DynamicResource是不起作用的。它可以避免异常,但资源无法解析。 - MSicc
使用DynamicResource是不起作用的。它可以避免异常,但资源无法解析。 - MSicc
使用DynamicResource是不起作用的。它可以避免异常,但是资源无法解析。 - undefined
不行!这显然是一个需要在MAUI中解决的错误。不要引导人们使用DynamicResource,而StaticResource绝对是正确的方法。 - undefined

0
对于使用.NET 7出现的这个错误,我将我的代码修改如下:
<!-- In App.xaml -->
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:local="clr-namespace:StylesDemo"
              x:Class="StylesDemo.App">
    <Application.Resources>

        <ResourceDictionary>

            <!--APP-level styles-->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>


            <!--Example of creating Global Styles globalButtonstyle in App.xaml-->
            <Color x:Key="backGroundColorButtonGlobal">#440055</Color>
            <Color x:Key="textColorButtonGlobal">#FFFFFF</Color>

            <Style TargetType="Button" x:Key="GlobalButtonstyle">
                <Setter Property="TextColor" Value="{StaticResource textColorButtonGlobal}" />
                <Setter Property="BackgroundColor" Value="{StaticResource backGroundColorButtonGlobal}" />
                <Setter Property="FontAttributes" Value="Bold" />
                <Setter Property="FontSize" Value="Micro" />
            </Style>
        </ResourceDictionary>

    </Application.Resources>
</Application>



<!-- In MainPage.xaml -->
<Button Text="Global Style" Style="{StaticResource globalButtonStyle}"/>
<Button Text="Global Style" Style="{DynamicResource globalButtonStyle}"/>

你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人能够确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

0
我遇到了同样的问题,解决办法是除了第一个/主要页面外,其他页面都使用DI。在App.xaml.cs中创建了一个新的页面。
 public partial class App : Application
{
    //public App(MainPage mainPage)
    public App(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage(serviceProvider.GetRequiredService<MainPageViewModel>()));
    }
}

0
我们无法在App ctor中注入"MainPage",如果MainPage使用了来自App.xaml的静态资源(或进一步引用的xaml文件,例如Styles.xaml或Colors.xaml)。MainPage的实例是由DI在注入到App.xaml.cs的构造函数之前创建的。我们需要在运行MainPage的InitializeComponent()方法之前运行App.xaml.cs的InitializeComponent()方法。
public partial class App : Application
{
    public App(IServiceProvider serviceProvider)
    {
        this.InitializeComponent();

        var mainPage = serviceProvider.GetRequiredService<MainPage>();
        this.MainPage = new NavigationPage(mainPage);
    }
}

除非真的必要(例如在需要运行时更新资源的情况下),否则不要使用DynamicResource。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接