第一种方式
如果你想用第一种方式访问样式,我们通常在文件中定义样式
App.xaml
例如,我们可以在中定义样式
App.xaml
如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
ââââââ<Application.Resources>
ââââââââââââ<ResourceDictionary>
ââââââââââââââââââ<Style x:Key="buttonStyle" TargetType="Button">
ââââââââââââââââââââââââ<Setter Property="HorizontalOptions"
Value="Center" />
ââââââââââââââââââââââââ<Setter Property="VerticalOptions"
Value="CenterAndExpand" />
ââââââââââââââââââââââââ<Setter Property="BorderColor"
Value="Lime" />
ââââââââââââââââââââââââ<Setter Property="CornerRadius"
Value="5" />
ââââââââââââââââââââââââ<Setter Property="BorderWidth"
Value="5" />
ââââââââââââââââââââââââ<Setter Property="WidthRequest"
Value="200" />
ââââââââââââââââââââââââ<Setter Property="TextColor"
Value="Teal" />
ââââââââââââââââââ</Style>
<!-- ContentPage -->
<Style TargetType="ContentPage"
ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor"
Value="WhiteSmoke" />
</Style>
<!--define the style of Label here-->
<Style x:Key="LabelFiledVerticalStyle" TargetType="Label">
<Setter Property="Margin" Value="0,7,0,0" />
<Setter Property="TextColor"
Value="Red" />
</Style>
</ResourceDictionary>
ââââââ</Application.Resources>
</Application>
然后在我们的页面中,我们可以使用代码访问样式:
new Label { Text="test label",Style=(Style)Application.Current.Resources ["LabelFiledVerticalStyle"]}
使用示例:
public class ApplicationStylesPageCS : ContentPage
ââââââ{
ââââââââââââpublic ApplicationStylesPageCS ()
ââââââââââââ{
ââââââââââââââââââTitle = "Application";
ââââââââââââââââââIconImageSource = "csharp.png";
ââââââââââââââââââPadding = new Thickness (0, 20, 0, 0);
ââââââââââââââââââContent = new StackLayout {
ââââââââââââââââââââââââChildren = {
âââââââââââââââââââââââââââââânew Button { Text = "These buttons", Style = (Style)Application.Current.Resources ["buttonStyle"] },
âââââââââââââââââââââââââââââânew Label { Text="test label",Style=(Style)Application.Current.Resources ["LabelFiledVerticalStyle"]}
ââââââââââââââââââââââââ}
ââââââââââââââââââ};
ââââââââââââ}
ââââââ}
有关详细信息,您可以查看文档
Global Styles in Xamarin.Forms
。
第二种方式
如果您创建
ResourceDictionary
(例如
MyResourceDictionary.xaml
)并将您的风格添加到
字典资源
,如下所示:
MyResourceDictionary.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<!--define the style of Label here-->
<Style x:Key="LabelFiledVerticalStyle" TargetType="Label">
<Setter Property="Margin" Value="0,7,0,0" />
<Setter Property="TextColor"
Value="Red" />
</Style>
</ResourceDictionary>
然后,如果我们想访问样式,我们应该在上面添加
ResourceDictionary
到
ContentPage.Resources
我们页面的。
<ContentPage.Resources>
<!--add your ResourceDictionary here-->
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ContentPage.Resources>
您可以在此处参考示例代码:
ApplicationStylesPage.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" IconImageSource="xaml.png">
ââââââ<ContentPage.Resources>
<!--add your ResourceDictionary here-->
<ResourceDictionary Source="MyResourceDictionary.xaml" />
<ResourceDictionary>
ââââââââââââââââââ<Style x:Key="buttonStyle" TargetType="Button">
ââââââââââââââââââââââââ<Setter Property="HorizontalOptions" Value="Center" />
ââââââââââââââââââââââââ<Setter Property="VerticalOptions" Value="CenterAndExpand" />
ââââââââââââââââââââââââ<Setter Property="BorderColor" Value="Lime" />
ââââââââââââââââââââââââ<Setter Property="CornerRadius" Value="5" />
ââââââââââââââââââââââââ<Setter Property="BorderWidth" Value="5" />
ââââââââââââââââââââââââ<Setter Property="WidthRequest" Value="200" />
ââââââââââââââââââââââââ<Setter Property="TextColor" Value="Red" />
ââââââââââââââââââ</Style>
ââââââââââââ</ResourceDictionary>
ââââââ</ContentPage.Resources>
ââââââ<ContentPage.Content>
ââââââââââââ<StackLayout Padding="0,20,0,0">
ââââââââââââââââââ<Button Text="application style overrides" Style="{StaticResource buttonStyle}" Clicked="Button_Clicked" />
ââââââââââââ</StackLayout>
ââââââ</ContentPage.Content>
</ContentPage>
ApplicationStylesPage.xaml.cs
public partial class ApplicationStylesPage : ContentPage
ââââââ{
ââââââââââââpublic ApplicationStylesPage ()
ââââââââââââ{
ââââââââââââââââââInitializeComponent ();
ââââââââââââ}
private void Button_Clicked(object sender, System.EventArgs e)
{
var hasValue = Resources.TryGetValue("LabelFiledVerticalStyle", out object style);
if (hasValue)
{
var LabelStyle = (Style)style;
}
}
}