none
如何在metro 中实现 在 xaml 中 gotoVisualElementState? RRS feed

  • 问题

  • metro现在支持Interactivity吗?如果不支持 那该如何实现呢?

    silverlight中的实现方法是下面的代码:

     <RadioButton x:Name="thumb3" Height="130" Style="{StaticResource Thumb3_RadioButtonStyle}" Width="109" Canvas.Left="25" Canvas.Top="314" Content="RadioButton" Cursor="Hand">
                                    <System_Windows_Interactivity:Interaction.Triggers>
                                        <System_Windows_Interactivity:EventTrigger EventName="Checked">
                                            <PCGaming:GoToUserControlState StateName="ShowFeature3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                        <System_Windows_Interactivity:EventTrigger EventName="MouseEnter">
                                            <im:PlaySoundAction Source="Audio/overGeneral.mp3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                        <System_Windows_Interactivity:EventTrigger EventName="Click">
                                            <im:PlaySoundAction Source="Audio/clickMenu.mp3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                    </System_Windows_Interactivity:Interaction.Triggers>
                                </RadioButton>

    // This action searches the visual tree (parent elements) looking for
     // states defined in the topmost UserControl (MainPage).
     // This way one can trigger a UserControl state from inside any template.
        public class GoToUserControlState : TriggerAction<DependencyObject>
        {
            #region Property to Expose

      public static readonly DependencyProperty StateNameProperty = DependencyProperty.Register("State Name", typeof(string), typeof(GoToUserControlState), null);

      [Category("State Properties")]
            public string StateName
            {
                get { return (string)GetValue(StateNameProperty); }
                set { SetValue(StateNameProperty, value); }
            }

            #endregion

            protected override void Invoke(object o)
            {
       DependencyObject parent = this.AssociatedObject as DependencyObject;
       while (parent != null)
       {
                    if (parent.GetType() == typeof(MainPage))
                    {
                        VisualStateManager.GoToState(parent as Control, StateName, true);
                        break;
                    }
        parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
       }
            }
        }

     <System_Windows_Interactivity:EventTrigger EventName="Checked">
                                            <PCGaming:GoToUserControlState StateName="ShowFeature3"/>
                                        </System_Windows_Interactivity:EventTrigger>

    关键地方

    2012年6月30日 13:58

答案

全部回复

  • 不支持,你不应该硬要去做一个转换,而是要考虑选择现在能够实现的方式。 就其Interactivity的原理,我们可以自己去实现他的功能,通过附加属性来获取所制定的事件,然后通过Metro内的反射机制将你的 方法挂到这个事件上,例如你可以自己实现 GoToUserControlState 的方法来转换VS.

    我曾经写过一篇文章是关于在Metro中实现 附加Command属性来使事件和Command建立联系:http://www.codeproject.com/Articles/319855/Attached-Command-for-Windows-8-Metro-Style-in-C 你可以参考一下将其修改至RP版本。

    下面是我基于你的要求设计的一个简单例子,参考了我的那篇文章,代码比较多,基本原理就是利用附加属性将事件转换成一个执行的方法,然后再这个方法中调用自定义的Action来实现不同的需求:https://skydrive.live.com/#cid=51B2FDD068799D15&id=51B2FDD068799D15%211035


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年7月2日 10:09
    版主
  • 这简直就是最完美的答案。。。非常感谢。这种实现方式应该能在wpf中生效吧?


    非常荣幸成为.net 开发者

    2012年7月2日 10:57