如何基于标签在Xamarin中创建新元素?
我经常使用这个元素:如何基于标签在Xamarin中创建新元素?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我经常使用这个元素: <Label Style="{DynamicResource ListItemTextStyle}" Text="Front Side" HorizontalOptions="StartAndExpand" /> 有没有一种方法可以创建一个名为SettingsText的新元素,然后只需指定 <SettingText Text="Front Side" /> 您可以根据需要创建自定义标签并在此构造函数中设置属性
<Label Style="{DynamicResource ListItemTextStyle}"
Text="Front Side"
HorizontalOptions="StartAndExpand" />
有没有一种方法可以创建一个名为SettingsText的新元素,然后只需指定
<SettingText Text="Front Side" />
您可以根据需要创建自定义标签并在此构造函数中设置属性 您需要创建自定义标签,如下所示:
namespace ProjectName
public class SettingText : Label
{
public SettingText()
{
this.Style = new Style(typeof(Label)){
Setters = {
new Setter {Property = Label.HorizontalOptionsProperty, Value = LayoutOptions.FillAndExpand},
new Setter {Property = Label.VerticalOptionsProperty, Value = LayoutOptions.StartAndExpand}
}
};
this.TextColor = Color.Red;
}
}
在Xaml文件中添加自定义标签的命名空间:
xmlns:local="clr-namespace:ProjectName"
现在,您可以在xaml中使用此自定义标签,如:
<local:SettingText Text="Hello" />
您可以根据需要创建自定义标签并在此构造函数中设置属性 您需要创建自定义标签,如下所示:
namespace ProjectName
public class SettingText : Label
{
public SettingText()
{
this.Style = new Style(typeof(Label)){
Setters = {
new Setter {Property = Label.HorizontalOptionsProperty, Value = LayoutOptions.FillAndExpand},
new Setter {Property = Label.VerticalOptionsProperty, Value = LayoutOptions.StartAndExpand}
}
};
this.TextColor = Color.Red;
}
}
在Xaml文件中添加自定义标签的命名空间:
xmlns:local="clr-namespace:ProjectName"
现在,您可以在xaml中使用此自定义标签,如:
<local:SettingText Text="Hello" />
您可以在App.xaml中定义样式:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="CustomStyle" TargetType="Label">
<Setter Property="BackgroundColor" Value="Red" />
<Setter Property="TextColor" Value="Black" />
<Setter Property="HorizontalOptions" Value="StartAndExpand" />
</Style>
<Style x:Key="ListItemTextStyle" TargetType="Label" BasedOn="{StaticResource CustomStyle}"/>
</ResourceDictionary>
</Application.Resources>
用法:
<local:SettingText Text="123"/>
更新
ListItemTextStyle
是一种在XAML中定义标签样式的样式,找到它并在customize类中写入这些属性
class SettingText : Label
{
public SettingText()
{
BackgroundColor = Color.Red;
TextColor = Color.Red;
VerticalOptions = LayoutOptions.StartAndExpand;
}
}
或
您可以在App.xaml中定义样式:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="CustomStyle" TargetType="Label">
<Setter Property="BackgroundColor" Value="Red" />
<Setter Property="TextColor" Value="Black" />
<Setter Property="HorizontalOptions" Value="StartAndExpand" />
</Style>
<Style x:Key="ListItemTextStyle" TargetType="Label" BasedOn="{StaticResource CustomStyle}"/>
</ResourceDictionary>
</Application.Resources>
用法:
<local:SettingText Text="123"/>
更新
ListItemTextStyle
是一种在XAML中定义标签样式的样式,找到它并在customize类中写入这些属性
class SettingText : Label
{
public SettingText()
{
BackgroundColor = Color.Red;
TextColor = Color.Red;
VerticalOptions = LayoutOptions.StartAndExpand;
}
}
或
你也可以举一个例子,说明我如何直接在SettingsText中定义样式。我想最好把所有东西都放在一个地方。特别是,我不确定如何在C#:Style=“{DynamicResource ListItemTextStyle}中定义它@Alan2 ListItemTextStyle是您在应用程序的某个地方用XAML定义的一种样式,只需找到它并在SettingText的构造函数中编写相应的代码即可。您还可以举一个例子说明我如何直接在SettingsText中定义该样式。我想最好将所有内容都放在一个地方。尤其是我不是su关于如何在C#:Style=“{DynamicResource ListItemTextStyle}@Alan2 ListItemTextStyle是您在应用程序的某个地方用XAML定义的一种样式,只需找到它并在SettingText的构造函数中编写perporties。