首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么.NET-Maui全球样式不起作用?

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

Stack Overflow用户
提问于 2022-08-21 05:19:17
回答 2查看 732关注 0票数 3

我已经在.Net Maui中声明了全局样式,并试图从其中一个页面访问它,但是它会引发异常

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

App.xaml代码

代码语言:javascript
复制
<?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代码

代码语言:javascript
复制
<?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创建的默认应用程序。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-21 06:27:41

我想这是个错误的地方,</ResourceDictionary>

试试这个:

代码语言:javascript
复制
 <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=""中的名称

喜欢

代码语言:javascript
复制
x:Key="greenLabelStyle"
票数 2
EN

Stack Overflow用户

发布于 2022-09-14 15:32:52

还可以将全局样式添加到在Styles.xaml中引用的MergedDictionaries文件中。

请注意文件的位置

代码语言:javascript
复制
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73431829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档