【问题标题】:VisualStateManager.GoToState not working in Universal Windows app's ToggleButton in UserControlVisualStateManager.GoToState 在 UserControl 中的通用 Windows 应用程序的 ToggleButton 中不起作用
【发布时间】:2015-08-26 15:01:21
【问题描述】:

我有一个带有 MainPage.xaml 的通用 Windows 应用程序,该应用程序具有以下代码。 VisualStateManager 的 GoToState 不起作用。什么都没有发生。

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

BottomToolBar 是一个用户控件,它有一个带有 x:Name="WifiBtn" 的 ToggleButton。这是此用户控件的代码隐藏:

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

    ...

}

如您所见,我有一个名为 WifiButton 的公共属性,它返回一个 ContentControl(您不能返回一个 ToggleButton)。

在 App.xaml 中,我有一个应用程序资源,它设置了 ToggleButton 的样式,由此 VisualState“已检查”更改了 ToggleButton 的不透明度。比如:

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

我认为我已经准备好让 GoToState 调用正常工作。目标是在应用程序启动时将切换按钮的不透明度初始化为 0.5。换句话说,在 MainPage 构造函数中。但是当我运行该应用程序时,未设置不透明度。这条线似乎被完全忽略了。 我在 SO 中发现了其他类似的线程,它们有同样的问题,但几乎没有答案。

【问题讨论】:

    标签: c# xaml win-universal-app


    【解决方案1】:

    为了使 VisualStateManager 工作,你应该等到页面加载,所以我在页面构造函数中添加了以下内容并工作:

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

    【讨论】:

      【解决方案2】:

      根据您的情况,请确保以下 3 点

      1. 您需要使用 ToggleButton 的 IsChecked 属性来更改状态。

            var btn = BottomToolBar.WifiButton as ToggleButton;
            btn.IsChecked = true;
        
      2. 确保您已根据需要在视觉状态下将不透明度设置为 0.5。

      3. 按照 Juan 的建议在页面的加载事件中执行此操作。

      但请注意:它是一个默认的系统控件,它的大部分默认状态都与Property相关联(例如“Checked”状态与IsChecked Property相关联)。试着想象一下,如果我们只是改变了状态,但没有改变属性。事情会一团糟。

      以下是我的建议:

      对于默认控制状态,不要使用 GoToState,而是使用属性更改。

      对于控件的扩展状态,建议让控件自己处理其状态,除非您有非常特殊的要求。

      【讨论】:

      • 我没有意识到我可以做到这一点,因为它不允许我在我的代码中使用 ToggleButton。当我在步骤 1 中添加您的代码时,Visual Studio 为 Windows.UI.Xaml.Controls.Primitives 添加了一条 using 语句。这样做解决了我的问题。我不必在 Loaded 事件中添加 GoToState。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2017-01-18
      相关资源
      最近更新 更多