代码之家  ›  专栏  ›  技术社区  ›  Divyesh

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

  •  0
  • Divyesh  · 技术社区  · 2 年前

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

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

    方式1

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

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

    当我运行应用程序时,它会出现以下错误: 字典中不存在资源“LabelFiledVerticalStyle”

    方式2通过代码中的键访问资源

    我已经查看了微软提供的官方文档,并找到了其他方法。所以,我也尝试过,比如:

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

    在这篇文章中,我也得到了hasValue=false,尽管样式在Resource字典中。

    有人知道我们如何从代码背后访问它吗?请告诉我。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Jessie Zhang -MSFT    2 年前

    第一种方式

    如果你想用第一种方式访问样式,我们通常在文件中定义样式 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 )并将您的风格添加到 字典资源 ,如下所示:

    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;
                }
            }
        }