xamarin 如何使用.NET MAUI中的代码访问全局资源字典中定义的样式?

omqzjyyz  于 2023-04-27  发布在  .NET
关注(0)|答案(1)|浏览(152)

我在Styles.xaml文件中定义了样式,如下所示:

<Style
     x:Key="LabelFiledVerticalStyle"
     TargetType="Label">   
     <Setter Property="Margin" Value="0,7,0,0" />
</Style>

方式一

我想从我的代码隐藏文件访问它,所以我这样做了:

var LabelStyle = (Style)Application.Current.Resources["LabelFiledVerticalStyle"];

当我运行应用程序时,它给出了这个错误:字典中不存在资源“LabelFiledVerticalStyle”

方式二从代码中按键访问资源

我已经通过微软提供的官方文档,我已经找到了其他方法来做到这一点。所以,做了尝试,以及喜欢:

var hasValue = Resources.TryGetValue("LabelFiledVerticalStyle", out object style);
if (hasValue)
{
    var LabelStyle = (Style)style;
}

在这里,我也得到了hasValue = false,尽管样式在Resource dictionary中。
有人知道我们如何从代码后面访问它吗?请让我知道。

j0pj023g

j0pj023g1#

第一条路

如果你想用第一种方式访问样式,我们通常在文件App.xaml中定义样式
例如,我们可以在App.xaml中定义样式如下:

<?xml version="1.0" encoding="UTF-8"?> 
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
      <Application.Resources>
            <ResourceDictionary>
            
                  <Style x:Key="buttonStyle" TargetType="Button">
                        <Setter Property="HorizontalOptions" 
                        Value="Center" />
                        <Setter Property="VerticalOptions" 
                        Value="CenterAndExpand" />
                        <Setter Property="BorderColor"
                        Value="Lime" />
                        <Setter Property="CornerRadius" 
                        Value="5" />
                        <Setter Property="BorderWidth" 
                        Value="5" />
                        <Setter Property="WidthRequest"
                        Value="200" />
                        <Setter Property="TextColor" 
                        Value="Teal" />
                  </Style>
            
            <!-- ContentPage -->
            <Style TargetType="ContentPage"
                   ApplyToDerivedTypes="True">
                <Setter Property="BackgroundColor"
                        Value="WhiteSmoke" />
            </Style>

            <!--define the style of Label  here-->

            <Style x:Key="LabelFiledVerticalStyle"  TargetType="Label">
                <Setter Property="Margin" Value="0,7,0,0" />
                <Setter Property="TextColor" 
                        Value="Red" />
            </Style>

        </ResourceDictionary>
      </Application.Resources>
</Application>

然后在我们的页面中,我们可以通过代码访问样式:

new Label { Text="test label",Style=(Style)Application.Current.Resources ["LabelFiledVerticalStyle"]}

使用示例:

public class ApplicationStylesPageCS : ContentPage 
      {
            public ApplicationStylesPageCS ()
            {
                  Title = "Application";
                  IconImageSource = "csharp.png";
                  Padding = new Thickness (0, 20, 0, 0);

                  Content = new StackLayout {
                        Children = {
                              new Button { Text = "These buttons", Style = (Style)Application.Current.Resources ["buttonStyle"] },

                              new Label { Text="test label",Style=(Style)Application.Current.Resources ["LabelFiledVerticalStyle"]}
                        }
                  };
            }
      }

有关详细信息,请查看文档Global Styles in Xamarin.Forms

第二条路

如果创建一个ResourceDictionary(例如MyResourceDictionary.xaml)并将样式添加到ResourceDictionary,如下所示:
MyResourceDictionary.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">
   
    <!--define the style of Label  here-->

    <Style x:Key="LabelFiledVerticalStyle"  TargetType="Label">
        <Setter Property="Margin" Value="0,7,0,0" />
        <Setter Property="TextColor" 
                        Value="Red" />
    </Style>

</ResourceDictionary>

然后,如果我们想访问样式,我们应该将ResourceDictionary以上添加到我们页面的ContentPage.Resources。     

<ContentPage.Resources>
    <!--add your ResourceDictionary here-->
    <ResourceDictionary Source="MyResourceDictionary.xaml" />
    
</ContentPage.Resources>

您可以在这里参考示例代码:
ApplicationStylesPage.xaml

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" IconImageSource="xaml.png">
      <ContentPage.Resources>
        <!--add your ResourceDictionary here-->
        <ResourceDictionary Source="MyResourceDictionary.xaml" />
        
        <ResourceDictionary>
                  <Style x:Key="buttonStyle" TargetType="Button">
                        <Setter Property="HorizontalOptions" Value="Center" />
                        <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                        <Setter Property="BorderColor" Value="Lime" />
                        <Setter Property="CornerRadius" Value="5" />
                        <Setter Property="BorderWidth" Value="5" />
                        <Setter Property="WidthRequest" Value="200" />
                        <Setter Property="TextColor" Value="Red" />
                  </Style>
            </ResourceDictionary>
      </ContentPage.Resources>
      <ContentPage.Content>
            <StackLayout Padding="0,20,0,0">

                  <Button Text="application style overrides" Style="{StaticResource buttonStyle}" Clicked="Button_Clicked" />
            </StackLayout>
      </ContentPage.Content>
</ContentPage>

ApplicationStylesPage.xaml.cs

public partial class ApplicationStylesPage : ContentPage 
      {
            public ApplicationStylesPage ()
            {
                  InitializeComponent ();
            }

        private void Button_Clicked(object sender, System.EventArgs e)
        {
            var hasValue = Resources.TryGetValue("LabelFiledVerticalStyle", out object style);
            if (hasValue)
            {
                var LabelStyle = (Style)style;
            }
        }
    }

相关问题