【问题标题】:Is there a way to change the style basedon property programmatically?有没有办法以编程方式根据属性更改样式?
【发布时间】:2021-12-13 04:43:34
【问题描述】:

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

 <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}" />

只要我可以将颜色/样式从它设置的任何颜色更改为绿色,然后返回,就可以接受任何解决方法。我对 Xamarin 表单非常陌生。

【问题讨论】:

    标签: c# xamarin.forms xamarin.android visual-studio-2019


    【解决方案1】:

    有没有办法以编程方式根据属性更改样式?

    为此,您可以使用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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多