如何在xamarin.forms中的父contentview中定义的样式重复使用样式



拳头,我有一个以这种方式定义的称为FieldView的组件:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="CustomViews.Components.Fields.FieldView">
    <ContentView.Resources>
        <ResourceDictionary>
            <Style x:Key="LabelStyle" TargetType="Label">
                <Setter Property="FontSize" Value="20"></Setter>
        </ResourceDictionary>
    </ContentView.Resources>
    <StackLayout>
        <Label Text="Hello world" Style="{StaticResource LabelStyle}"></Label>
    </StackLayout>
</ContentView>

FieldView组件具有名为" LabelStyle"的样式。显然,我可以在FieldView组件中使用此样式。但是,该组件是将由其他组件(例如TextFieldView(继承的基本组件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<local:FieldView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:GeoSapiens.Coletum.CustomViews.Components.Fields"
    x:Class="GeoSapiens.Coletum.CustomViews.Components.Fields.TextFieldView">
    <StackLayout>
        <!-- the style is not accessible here -->
        <Label Text="Hello from TextFieldView" Style="{StaticResource LabelStyle}"></Label>
    </StackLayout>
</local:FieldView>

问题是我无法使用TextFieldView组件内FieldView中定义的样式。

有没有一种方法可以在TextFieldView组件中的FieldView中引用样式?也就是说:访问父组件中定义的样式。我应该以任何方式使用代码文件吗?

如果您在整个应用程序中使用了多个视图中的同一Style,我可能只会将您的样式移动到App.xaml并从那里使用它。

但是,如果TextFieldView基类设置为 FieldView,您要做的工作应该可以使用,但是从您的代码中看来,您的资源在其中未正确定义,并且缺少闭合</Style>。例如,下面的代码在我尝试出去并在XAML页面中使用了TextFieldView时工作。

fieldview:

<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="TestApp.Forms.FieldView">
    <ContentView.Resources>
        <ResourceDictionary>
            <Style x:Key="LabelStyle" TargetType="Label">
                <Setter Property="FontSize" Value="40" />
            </Style>
        </ResourceDictionary>
    </ContentView.Resources>
    <StackLayout>
        <Label Text="Hello world" Style="{StaticResource LabelStyle}" />
    </StackLayout>
</ContentView>

textfieldview:

<local:FieldView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:NuclearHalfLife.Forms" 
    x:Class="TestApp.Forms.TextFieldView">
    <StackLayout>
        <Label Text="Hello from TextFieldView" Style="{StaticResource LabelStyle}" />
    </StackLayout>
</local:FieldView>

尼克的答案确实有效,这只是一个语法错误,我无法想象您的应用程序如何使用它...

但是,如果您创建了提供ResourceDictionary而不是在XAML上定义的方法,则可以扩展样式(以及任何其他资源(。因此,您的基本视图看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CustomViews.Components.Fields.FieldView">
    <StackLayout>
        <Label Text="Hello world" 
               Style="{StaticResource LabelStyle}"/>
    </StackLayout>
</ContentView>

然后,在背后的代码中,您应该实现并使用该方法获取资源字典:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class FieldView : ContentView
{
    public FieldView()
    {
        InitializeComponent();
        // Here you set the Resources property through your method
        Resources = GetResources();
    }
    protected virtual ResourceDictionary GetResources()
    {
        ResourceDictionary ret = new ResourceDictionary();
        // Create your style here
        Style labelStyle = new Style(typeof(Label));
        labelStyle.Setters.Add(new Setter() { Property = Label.FontSizeProperty, Value = 20 });
        // Add the style to Resource Dictionary
        ret.Add("LabelStyle", labelStyle);
        // Return the resource Dictionary
        return ret;
    }
}

在您的孩子视图中,您应该像在基础上一样设置Resources属性,并根据需要添加新资源:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChildView : FieldView
{
    public ChildView()
    {
        InitializeComponent();
        // Call the same method
        Resources = GetResources();
    }
    protected override ResourceDictionary GetResources()
    {
        ResourceDictionary res = base.GetResources();
        // You can add new Styles here, for example
        Style newStyle = new Style(typeof(Button));
        newStyle.Setters.Add(new Setter() { Property = Button.BackgroundColorProperty, Value = Color.Red });
        res.Add("DangerButtonStyle", newStyle);
        return res;
    }
}

我希望它将有用。

从新的内容页面定义一个单独的资源cestionaly,并使用合并的资源词典:

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="MyNamespace.MyStyles">
  <Style x:Key="LabelResDictStyle" TargetType="Label">
    <Setter Property="Text" Value="Hello from ResDictStyle" />
  </Style>
</ResourceDictionary>

重新定义背后的代码:

namespace MyNamespace
{
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class MyStyles : ResourceDictionary
  {
    public MyStyles()
    {
        InitializeComponent();
    }
  }
}

可以在ContentView中引用单独的资源字典中的样式:

xmlns:resDictStyles="clr-namespace:MyNamespace"
...
<ContentView.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <resDictStyles:MyStyles />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</ContentView.Resources>
...
<Label Style="{StaticResource LabelResDictStyle}" />

合并资源词典的样式可以使用BasedOn

"扩展"
xmlns:resDictStyles="clr-namespace:MyNamespace"
...
<ContentPage.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <resDictStyles:MyStyles />
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="LabelPageStyle" TargetType="Label" BasedOn="{StaticResource LabelResDictStyle}">
      <Setter Property="FontSize" Value="20"></Setter>
    </Style>
  </ResourceDictionary>
</ContentPage.Resources>
...
<Label Style="{StaticResource LabelPageStyle}" />

最新更新