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

时间:2016-01-27 10:31:19

标签: c# windows-store-apps winrt-xaml windows-10 windows-10-universal

我的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的可见性变为可见,我仍然可以看到广告。如何在我的付费应用版本中永久折叠可见性?

2 个答案:

答案 0 :(得分:2)

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

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

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

祝你好运!

答案 1 :(得分:0)

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

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