首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重用父ContentView中在Xamarin.Forms中定义的样式

如何重用父ContentView中在Xamarin.Forms中定义的样式
EN

Stack Overflow用户
提问于 2018-02-28 19:08:00
回答 3查看 1.9K关注 0票数 3

首先,我有一个名为FieldView的组件是这样定义的:

代码语言:javascript
复制
<?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 )继承,实现如下:

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

问题是,我不能使用FieldView中在TextFieldView组件中定义的样式。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-28 21:05:43

Nick的答案真的很有效,只是语法错误,我无法想象你的应用程序是如何运行的。

但是,如果您创建一个方法来提供ResourceDictionary,而不是在XAML上定义它,那么您就可以扩展样式(和任何其他资源)。所以您的基本视图将如下所示:

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

然后,在后面的代码中,应该实现并使用该方法来获得资源字典:

代码语言:javascript
复制
[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属性,就像在基础上所做的那样,并根据需要添加新的资源:

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

我希望这是有用的。

票数 1
EN

Stack Overflow用户

发布于 2018-02-28 20:29:43

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

但是,如果将TextFieldView基类设置为FieldView,但是从代码中看,您的资源没有在其中正确定义,并且缺少一个关闭的</Style>,那么您要做的工作应该是有效的。例如,当我尝试并在xaml页面中使用TextFieldView时,下面的代码起作用了。

FieldView:

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

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2018-03-01 00:00:33

从新内容页中定义单独的ResourceDictionary并使用合并资源词典

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

重新定义背后的代码:

代码语言:javascript
复制
namespace MyNamespace
{
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class MyStyles : ResourceDictionary
  {
    public MyStyles()
    {
        InitializeComponent();
    }
  }
}

独立资源字典中的样式可以在ContentView中引用。

代码语言:javascript
复制
xmlns:resDictStyles="clr-namespace:MyNamespace"
...
<ContentView.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <resDictStyles:MyStyles />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</ContentView.Resources>
...
<Label Style="{StaticResource LabelResDictStyle}" />

合并资源字典中的样式可以使用BasedOn进行“扩展”

代码语言:javascript
复制
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}" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49036845

复制
相关文章

相似问题

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