简体   繁体   English

VisualStateManager.GoToState在UserControl中的通用Windows应用程序的ToggleButton中不起作用

[英]VisualStateManager.GoToState not working in Universal Windows app's ToggleButton in UserControl

I have a Universal Windows app with a MainPage.xaml that has the following code. 我有一个带有MainPage.xaml的通用Windows应用程序,其中包含以下代码。 The VisualStateManager's GoToState is not working. VisualStateManager的GoToState无法正常工作。 Nothing happens. 什么都没发生。

    public MainPage()
    {            
        ...
        VisualStateManager.GoToState(BottomToolBar.WifiButton, "Checked", false);
    }

BottomToolBar is a User Control that has a ToggleButton with x:Name="WifiBtn". BottomToolBar是一个用户控件,具有一个带有x:Name =“ WifiBtn”的ToggleButton。 Here is the code-behind this User Control: 这是此用户控件背后的代码:

    public sealed partial class BottomToolBar : UserControl
{
    public BottomToolBar()
    {
        this.InitializeComponent();
    }

    private void WifiBtn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        var parentPage = GetParentsPage(this);
        if(parentPage != null)
        {
            parentPage.MapItemsManager.ToggleActivate(MapItemsType.ISF);
        }
    }

    public ContentControl WifiButton
    {
        get
        {
            return WifiBtn;
        }
    }

    ...

}

As you can see I have a public property called WifiButton which returns a ContentControl (You cannot return a ToggleButton). 如您所见,我有一个名为WifiButton的公共属性,该属性返回ContentControl(您不能返回ToggleButton)。

In App.xaml I have an Application Resource that styles the ToggleButton whereby the VisualState "Checked" changes the Opacity of the ToggleButton. 在App.xaml中,我具有一个应用程序资源,该资源为ToggleButton设置样式,从而VisualState“已检查”更改了ToggleButton的不透明度。 Something like: 就像是:

<Application.Resources>
    <Style x:Key="BottomToggleButtonStyle" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                    </Storyboard>
                                </VisualState>

I thought I had everything in place to get that call to GoToState to work. 我以为我已经准备就绪,可以调用GoToState了。 The goal is the initialize the toggle button's Opacity to 0.5 when the application starts. 目标是在应用程序启动时将切换按钮的不透明度初始化为0.5。 In other words, in the MainPage constructor. 换句话说,在MainPage构造函数中。 But when I run the app, the Opacity is not set. 但是,当我运行该应用程序时,未设置不透明度。 The line seems to be completely ignored. 这条线似乎被完全忽略了。 I found other similar threads in SO with the same problem but little to no answers. 我在SO中发现了其他类似的线程,但都遇到了相同的问题,但几乎没有答案。

In order to make the VisualStateManager works you should wait until the Page is Loaded so I added the following in the page constructor and works: 为了使VisualStateManager能够正常工作,您应该等待页面加载完毕,因此我在页面构造函数中添加了以下内容并可以正常工作:

this.Loaded += (s, e) =>
        {
            VisualStateManager.GoToState(BottomToolBar.WifiButton, "Checked", false);
        };

For your case, make sure the following 3 points 对于您的情况,请确保以下三点

  1. you need to use ToggleButton's IsChecked propery to change the state. 您需要使用ToggleButton的IsChecked属性更改状态。

      var btn = BottomToolBar.WifiButton as ToggleButton; btn.IsChecked = true; 
  2. make sure you've set the Opacity to 0.5 in visual state as you want. 确保根据需要在视觉状态下将“不透明度”设置为0.5。

  3. do it in page's loaded event as Juan suggested. 按照Juan的建议在网页的加载事件中执行此操作。

But please note : it's a default system control, and most of its default states are associated with Property(For example the "Checked" state is associated with IsChecked Property). 但是请注意 :这是一个默认的系统控件,其大多数默认状态与Property相关联(例如,“ Checked”状态与IsChecked Property相关联)。 Try to imagine, if we simply change the state, but didn't change the property. 试想一下,如果我们只是更改状态,但不更改属性。 Things will mess up. 事情会糟透了。

So here are my suggestions: 所以这是我的建议:

For default states of control, do not use GoToState, but use property change instead. 对于默认的控制状态,请勿使用GoToState,而应使用属性更改。

For extended states of control, it's recommended to let the control to handle its state by itself, unless you have very special requirements. 对于扩展的控制状态,除非有非常特殊的要求,否则建议让控件自行处理其状态。

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

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