拳头,我有一个这样定义的名为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>


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

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

最佳答案

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

但是,如果您创建一种方法来提供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;
    }
}


我希望它会有用。

09-06 03:45