当我将 mainpage 注入到 App 类构造函数中时,我会收到 StaticResource not found for key 的错误提示,但如果我不在 App 构造函数中注入 Mainpage 则可以正常工作。
我有一个全局资源主题文件,我在 App.xaml.cs 中调用它,在那里我声明了静态资源。
这是我的 App.cs 文件:
以下代码位于MainPage.xaml文件中:
我有一个全局资源主题文件,我在 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>();