简体   繁体   English

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

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

I have the following VisualState Setter properties in my UWP app. 我的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>

I want to have a trial and a paid version of my app with the trial version having ads and the paid version without ads. 我想拥有我的应用程序的试用版和付费版,其中试用版带有广告,付费版没有广告。 I am using LicenseInformation.IsTrial property to determine whether the app is trial or paid version and when the app is loading I am making the visibility of DesktopAds and MobileAds as collapsed. 我正在使用LicenseInformation.IsTrial属性来确定该应用程序是试用版还是付费版本,以及何时加载该应用程序,因此我将DesktopAds和MobileAds的可见性折叠了。 But because of the AdaptiveTriggers the DesktopAds and MobileAds visibility gets changed to visible and I still see the ads. 但是由于有了AdaptiveTriggers,DesktopAds和MobileAds的可见性已更改为可见,我仍然可以看到广告。 How to make the visibility permanently collapsed in my paid version of the app? 如何使可见性在我的付费版应用中永久崩溃?

What you really want to do is to use Visual State Groups. 您真正想做的是使用可视状态组。 Like this: 像这样:

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

Then, let the adaptive triggers handle layout, and let your code-behind handle hiding and showing your adverts. 然后,让自适应触发器处理布局,并让您的代码隐藏处理隐藏和显示广告。 Because you put states into groups, they are mutually exclusive, and can be set indep. 因为您将状态分为几组,所以它们是互斥的,可以独立设置。 Sort of like how a button can be both pressed and enabled . 有点像如何同时pressedenabled按钮。 They do it with groups. 他们与团体一起做。 This is a very typical approach, by the way. 顺便说一下,这是一种非常典型的方法。

Best of luck! 祝你好运!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM