XAML StaticResource在Xamarin表单中不起作用

yqkkidmi  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(110)

我是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>


如果我把它们放在同一个文件中代码就可以工作了,我应该改变什么

4smxwvx5

4smxwvx51#

步骤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>


在CodeBehind中

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>

csga3l58

csga3l582#

你也可以把它放在Application.Resources中,在每个页面中使用它。https://learn.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>

相关问题