有没有办法以编程方式更改基于 属性 的样式?

Is there a way to change the style basedon property programmatically?

我有下面的代码,它基本上是默认的选项卡式视图,添加了将背景色设置为绿色而不是动态主色的样式。

 <Shell.Resources>
        <ResourceDictionary>
            <Style x:Key="BaseStyle" TargetType="Element" >
                <Setter Property="Shell.BackgroundColor" Value="{DynamicResource Primary}" />
                <Setter Property="Shell.ForegroundColor" Value="White" />
                <Setter Property="Shell.TitleColor" Value="White" />
                <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
                <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
                <Setter Property="Shell.TabBarBackgroundColor" Value="{DynamicResource Primary}" />
                <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
                <Setter Property="Shell.TabBarTitleColor" Value="White"/>
            </Style>
            <Style x:Key="GreenStyle" TargetType="Element" >
                <Setter Property="Shell.BackgroundColor" Value="Green" />
                <Setter Property="Shell.ForegroundColor" Value="White" />
                <Setter Property="Shell.TitleColor" Value="White" />
                <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
                <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
                <Setter Property="Shell.TabBarBackgroundColor" Value="Green" />
                <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
                <Setter Property="Shell.TabBarTitleColor" Value="White"/>
            </Style>
            <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
        </ResourceDictionary>
    </Shell.Resources>

我想知道是否可以通过编程方式将 TabBar BasedOn 属性 更改为 GreenStyle。如:

*Trigger*
{
TabBar.BasedOn = GreenStyle;
}

但以一种实际有效的方式。

我问是因为它的静态特性似乎无法动态工作,而且它不会让我将其更改为:

<Style TargetType="TabBar" BasedOn="{DynamicResource BaseStyle}" />

接受任何解决方法,只要我可以将 color/style 从它设置的任何颜色更改为绿色并返回。我对 Xamarin 表单很陌生。

Is there a way to change the style basedon property programmatically?

为此,您可以使用Dynamic Styles来实现这个功能。 应用程序可以通过使用动态资源在运行时动态响应样式更改。

您可以参考以下代码:

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="baseStyle" TargetType="View">
            <Setter Property="VerticalOptions" Value="CenterAndExpand" />
        </Style>
        <Style x:Key="blueSearchBarStyle" TargetType="SearchBar" BasedOn="{StaticResource baseStyle}">
            <Setter Property="FontAttributes" Value="Italic" />
            <Setter Property="PlaceholderColor" Value="Blue" />
        </Style>
        <Style x:Key="greenSearchBarStyle" TargetType="SearchBar">
            <Setter Property="FontAttributes" Value="None" />
            <Setter Property="PlaceholderColor" Value="Green" />
        </Style>
        <Style x:Key="buttonStyle" TargetType="Button" BasedOn="{StaticResource baseStyle}">
            <Setter Property="FontSize" Value="Large" />
            <Setter Property="TextColor" Value="Red" />
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout Padding="0,20,0,0">
        <SearchBar Placeholder="These SearchBar controls" Style="{DynamicResource searchBarStyle}" />
        <SearchBar Placeholder="are demonstrating" Style="{DynamicResource searchBarStyle}" />
        <SearchBar Placeholder="dynamic styles," Style="{DynamicResource searchBarStyle}" />
        <SearchBar Placeholder="but this isn't dynamic" Style="{StaticResource blueSearchBarStyle}" />
        <Button Text="Change Style" Style="{StaticResource buttonStyle}" Clicked="OnButtonClicked" />
    </StackLayout>
</ContentPage.Content>

xaml.cs代码是:

public partial class DynamicStylesPage : ContentPage
{
    bool originalStyle = true;

    public DynamicStylesPage ()
    {
        InitializeComponent ();
        Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
    }

    void OnButtonClicked (object sender, EventArgs e)
    {
        if (originalStyle) {
            Resources ["searchBarStyle"] = Resources ["greenSearchBarStyle"];
            originalStyle = false;
        } else {
            Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
            originalStyle = true;
        }
    }
}

详情请查看文档Dynamic Styles in Xamarin.Forms

上面的文档中有一个示例,你可以在这里查看:https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-styles-dynamicstyles/