我是 Xamarin 的新手
我有這個代碼, Style="{StaticResource MyStyleButton}"但在檔案 2 中不起作用
組件頁面.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>
另一個 xmal 檔案
登錄頁面.xaml
<ContentPage.Content>
<StackLayout>
<Button
Command="{Binding Command1}"
Style="{StaticResource MyStyleButton}"
Text="Button1" />
</StackLayout>
</ContentPage.Content>
如果我把它們放在同一個檔案中,代碼正確我應該改變什么
uj5u.com熱心網友回復:
你身邊也有這種方式,你也可以把它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>
uj5u.com熱心網友回復:
步驟 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456533.html
標籤:xml xamarin.forms
