StaticResource 不适用于 Xamarin 表单
StaticResource does not work in Xamarin forms
我是 Xamarin 新手
我有此代码,但 Style="{StaticResource MyStyleButton}"
在其他页面中不起作用。
componentPage.xaml
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
另一个xaml文件
LogInPage.xaml
<ContentPage.Content>
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
</StackLayout>
</ContentPage.Content>
如果我将它们放在同一个文件中,代码就可以工作,
我应该改变什么
第 1 步:创建新的 ContentPage 示例名称 ButtonStyles.xaml
第 2 步:编辑
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
...
</ContentPage>
至
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
....
</ResourceDictionary>
在CondeBehind
using Xamarin.Forms.Internals;
[Preserve(AllMembers = true)]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ButtonStyles
{
public ButtonStyles()
{
InitializeComponent();
}
}
第 3 步:将样式放入 ButtonStyles.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
<Style x:Key="MyStyleButton2" TargetType="Button">
.....
</Style>
</ResourceDictionary>
第 4 步:在您的 App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application
x:Class="YourAppName.App"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourAppName;assembly=YourAppName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:ButtonStyles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
第 5 步:在您的任何 ContentPages
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="YourAppName.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
<Button
Command="{Binding Command2}"
Style="{StaticResource MyStyleButton}"
Text="Button2" />
<Button
Command="{Binding Command3}"
Style="{StaticResource MyStyleButton}"
Text="Button3" />
</StackLayout>
</ContentPage>
你那里附近也有这种方法,你也可以把它放在Application.Resources
里,在每个页面都使用。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries
在App.xaml
<Application.Resources>
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
</Application.Resources>
在您喜欢的任何页面中使用
<ContentPage.Content>
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
</StackLayout>
</ContentPage.Content>
我是 Xamarin 新手
我有此代码,但 Style="{StaticResource MyStyleButton}"
在其他页面中不起作用。
componentPage.xaml
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
另一个xaml文件
LogInPage.xaml
<ContentPage.Content>
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
</StackLayout>
</ContentPage.Content>
如果我将它们放在同一个文件中,代码就可以工作, 我应该改变什么
第 1 步:创建新的 ContentPage 示例名称 ButtonStyles.xaml
第 2 步:编辑
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
...
</ContentPage>
至
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
....
</ResourceDictionary>
在CondeBehind
using Xamarin.Forms.Internals;
[Preserve(AllMembers = true)]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ButtonStyles
{
public ButtonStyles()
{
InitializeComponent();
}
}
第 3 步:将样式放入 ButtonStyles.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="YourAppName.ButtonStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
<Style x:Key="MyStyleButton2" TargetType="Button">
.....
</Style>
</ResourceDictionary>
第 4 步:在您的 App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application
x:Class="YourAppName.App"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourAppName;assembly=YourAppName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:ButtonStyles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
第 5 步:在您的任何 ContentPages
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="YourAppName.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
<Button
Command="{Binding Command2}"
Style="{StaticResource MyStyleButton}"
Text="Button2" />
<Button
Command="{Binding Command3}"
Style="{StaticResource MyStyleButton}"
Text="Button3" />
</StackLayout>
</ContentPage>
你那里附近也有这种方法,你也可以把它放在Application.Resources
里,在每个页面都使用。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries
在App.xaml
<Application.Resources>
<Style x:Key="MyStyleButton" TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196f3" />
<Setter Property="WidthRequest" Value="300" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="White" />
</Style>
</Application.Resources>
在您喜欢的任何页面中使用
<ContentPage.Content>
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
</StackLayout>
</ContentPage.Content>