Xamarin 有没有办法定制我经常使用的标签?
在我的Xamarin XAML中,我多次使用:Xamarin 有没有办法定制我经常使用的标签?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,在我的Xamarin XAML中,我多次使用: <Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome" XAlign="Center" FontSize="13" TextColor="#1E90FF" /> 有没有一种使用C的方法,我可以创建一个自定义版本的Label并使用它,这样我就不必一直指定字体和其他东西了?也许,你可以在你的App.xaml中使用样式 <
<Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome"
XAlign="Center" FontSize="13"
TextColor="#1E90FF" />
有没有一种使用C的方法,我可以创建一个自定义版本的Label并使用它,这样我就不必一直指定字体和其他东西了?也许,你可以在你的App.xaml中使用
样式
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAPp.App">
<Application.Resources>
<ResourceDictionary>
<Style x:Key="FACheck" TargetType="Label">
<Setter Property="Text" Value="{x:Static local:FontAwesome.FACheck}"/>
<Setter Property="FontFamily" Value="FontAwesome"/>
<Setter Property="XAlign" Value="Center"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="TextColor" Value="#1E90FF"/>
</Style>
<ResourceDictionary>
</Application.Resources>
</Application>
要在C#中创建样式,请参见此
C代码示例:(在应用程序类中设置资源字典)
您可以在C#类中创建具有以下样式的控件:
或者在xaml中将其作为静态资源访问:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" Icon="xaml.png">
<ContentPage.Content>
<StackLayout Padding="0,20,0,0">
<Button Text="These buttons" Style="{StaticResource buttonStyle}" />
<Button Text="are demonstrating" Style="{StaticResource buttonStyle}" />
<Button Text="application style overrides" Style="{StaticResource buttonStyle}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
创建您的类并在任何地方使用它
public class MyAwesomeLabel : Xamarin.Forms.Label
{
public MyAwesomeLabel()
{
FontFamily = "FontAwesome";
XAlign = Xamarin.Forms.TextAlignment.Center; //deprecated BTW
FontSize = 13;
TextColor = Color.FromHex(0x1E90FF);
//etc
}
}
感谢上帝,你的建议看起来很有趣。我投了赞成票,但还没有点击“接受”。我也可以在C#中创建此资源吗?是的,您可以在C#中创建此资源,请记住XAML只是C#代码上的syntac sugar。@Alan您可以改用代码:)。我更新了我的答案
public class App : Application
{
public App ()
{
var buttonStyle = new Style (typeof(Button)) {
Setters = {
...
new Setter { Property = Button.TextColorProperty, Value = Color.Teal }
new Setter { Property = Button.BackgroundColor, Value = Color.White }
// add more setters for the properties that you want to set here
}
};
// add this style into your resource dictionary.
Resources = new ResourceDictionary ();
Resources.Add ("buttonStyle", buttonStyle);
...
}
...
}
public class ApplicationStylesPageCS : ContentPage
{
public ApplicationStylesPageCS ()
{
...
Content = new StackLayout {
Children = {
new Button { Text = "These buttons", Style = (Style)Application.Current.Resources ["buttonStyle"] },
new Button { Text = "are demonstrating", Style = (Style)Application.Current.Resources ["buttonStyle"] },
new Button { Text = "application styles", Style = (Style)Application.Current.Resources ["buttonStyle"]
}
}
};
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" Icon="xaml.png">
<ContentPage.Content>
<StackLayout Padding="0,20,0,0">
<Button Text="These buttons" Style="{StaticResource buttonStyle}" />
<Button Text="are demonstrating" Style="{StaticResource buttonStyle}" />
<Button Text="application style overrides" Style="{StaticResource buttonStyle}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
public class MyAwesomeLabel : Xamarin.Forms.Label
{
public MyAwesomeLabel()
{
FontFamily = "FontAwesome";
XAlign = Xamarin.Forms.TextAlignment.Center; //deprecated BTW
FontSize = 13;
TextColor = Color.FromHex(0x1E90FF);
//etc
}
}