为什么 .NET-Maui 全局样式不起作用?

问题描述 投票:0回答:3

我已经在 .Net Maui 中声明了全局样式,并尝试从其中一个页面访问它,但它抛出异常

Microsoft.Maui.Controls.Xaml.XamlParseException:位置 10:37。类型转换器失败:调用目标已引发异常。 Microsoft.Maui.Controls.Xaml.XamlParseException:位置 8:34。未找到主键的 StaticResource。

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:MyApp"
         x:Class="MyApp.App">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <Style x:Key="redLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                    Value="Red"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>

    <Style TargetType="Label">
        <Setter Property="TextColor"
                    Value="Green"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>
</Application.Resources>

MainPage.xaml 代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         NavigationPage.HasNavigationBar="False"
         x:Class="MyApp.MainPage">

<VerticalStackLayout HorizontalOptions="CenterAndExpand"
                     VerticalOptions="CenterAndExpand">
    <Label Style="{StaticResource redLabelStyle}"
           Text="Global Style Red Label"/>
    <Label Text="GLobal Style Green Label"/>
    <Label Text="GLobal Style Green Label"/>

</VerticalStackLayout>

</ContentPage>

注意:这是.Net Maui 创建的默认应用程序。

xamarin xamarin.forms maui .net-maui
3个回答
6
投票

我认为这是错误的地方

</ResourceDictionary>

试试这个:

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries> 
    <Style x:Key="redLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                    Value="Red"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>
    <Style TargetType="Label">
        <Setter Property="TextColor"
                    Value="Green"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>
  </ResourceDictionary>
</Application.Resources>

同时为第二种样式命名。

<Style TargetType="Label">
x:Key=""

中的名字

喜欢

x:Key="greenLabelStyle"

2
投票

您还可以将全局样式添加到 MergedDictionaries 中引用的

Styles.xaml
文件

请记下文件位置

<ResourceDictionary Source="Resources/Styles/Styles.xaml" />

您可以将样式直接复制到那里,这样您就可以将所有全局样式保留在一个中心位置。这有助于保持您的 App.xaml 美观整洁。


0
投票

您的一个样式键丢失,未在颜色文件中定义,并且您在此处使用这会导致异常,请重新检查。

© www.soinside.com 2019 - 2024. All rights reserved.