如何通过 C# 中的代码更改 VisualState Setter 属性?

How to change VisualState Setter property via code in C#?

我的 UWP 应用程序中有以下 VisualState Setter 属性。

 <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="Desktop">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="800" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="DesktopAds.Visibility" Value="Visible" />
                    <Setter Target="DesktopAds.(Grid.Row)" Value="0" />
                    <Setter Target="DesktopAds.(Grid.Column)" Value="4" />
                    <Setter Target="DesktopAds.(Grid.ColumnSpan)" Value="1" />
                    <Setter Target="MainScrollViewer.(Grid.Row)" Value="0" />
                    <Setter Target="MainScrollViewer.(Grid.Column)" Value="2" />
                    <Setter Target="MediaControl.Height" Value="600" />

                    <Setter Target="MobileAds.Visibility" Value="Collapsed" />
                    <Setter Target="MainScrollViewer.(Grid.ColumnSpan)" Value="1" />

                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Phone">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="DesktopAds.Visibility" Value="Collapsed" />

                    <Setter Target="MediaControl.Height" Value="400" />

                    <Setter Target="DesktopAds.(Grid.Row)" Value="1" />
                    <Setter Target="DesktopAds.(Grid.Column)" Value="0" />
                    <Setter Target="DesktopAds.(Grid.ColumnSpan)" Value="3" />

                    <Setter Target="MobileAds.Visibility" Value="Visible" />
                    <Setter Target="MainScrollViewer.(Grid.Row)" Value="0" />
                    <Setter Target="MainScrollViewer.(Grid.Column)" Value="0" />
                    <Setter Target="MainScrollViewer.(Grid.ColumnSpan)" Value="5" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

我想要我的应用程序的试用版和付费版,试用版有广告,付费版没有广告。我使用 LicenseInformation.IsTrial 属性 来确定该应用程序是试用版还是付费版,并且在加载该应用程序时,我将 DesktopAds 和 MobileAds 的可见性设置为折叠。但是由于 AdaptiveTriggers,DesktopAds 和 MobileAds 的可见性变为可见,我仍然可以看到广告。如何在我的付费版应用程序中永久隐藏可见性?

以编程方式更改视觉状态:

VisualStateManager.GoToState(yourPageHere, "Phone", useTransitions: false);

您真正想做的是使用视觉状态组。像这样:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="LayoutVisualStateGroup">
        <VisualState x:Name="Desktop" />
        <VisualState x:Name="Phone" />        
    </VisualStateGroup>
    <VisualStateGroup x:Name="AdvertVisualStateGroup">
        <VisualState x:Name="ShowAds" />
        <VisualState x:Name="HideAds" />        
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

然后,让自适应触发器处理布局,让您的代码隐藏处理隐藏和显示您的广告。因为您将状态分组,所以它们是互斥的,并且可以独立设置。有点像按钮如何既可以是 pressed 又可以是 enabled。他们与团体一起做。顺便说一句,这是一种非常典型的方法。

祝你好运!