首先,我有一个名为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组件中定义的样式?即:访问父组件中定义的样式。我应该以任何方式使用代码隐藏文件吗?
发布于 2018-02-28 21:05:43
Nick的答案真的很有效,只是语法错误,我无法想象你的应用程序是如何运行的。
但是,如果您创建一个方法来提供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;
}
}我希望这是有用的。
发布于 2018-02-28 20:29:43
如果您在整个应用程序的多个视图中使用相同的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>发布于 2018-03-01 00:00:33
从新内容页中定义单独的ResourceDictionary并使用合并资源词典
<?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}" />https://stackoverflow.com/questions/49036845
复制相似问题