Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VisualStateGroup的事件到视图模型方法_C#_Xaml_Mvvm_Windows 10_Uwp - Fatal编程技术网

C# VisualStateGroup的事件到视图模型方法

C# VisualStateGroup的事件到视图模型方法,c#,xaml,mvvm,windows-10,uwp,C#,Xaml,Mvvm,Windows 10,Uwp,我已使用EventTriggerBehavior和CallMethodAction成功地将事件转换为方法(在ViewModel中使用),如以下示例所示(此处选择了一个加载的页面事件进行说明) 但是,对于VisualStateGroup的CurrentStateChanged事件(是的,嵌套在块中,因为CurrentStateChanged事件属于VisualStateGroup)没有成功: 我怀疑VisualStateGroup(或VisualStateManager)和CurrentS

我已使用EventTriggerBehaviorCallMethodAction成功地将事件转换为方法(在ViewModel中使用),如以下示例所示(此处选择了一个加载的页面事件进行说明)


但是,对于VisualStateGroupCurrentStateChanged事件(是的,嵌套在
块中,因为CurrentStateChanged事件属于VisualStateGroup)没有成功:


我怀疑VisualStateGroup(或VisualStateManager)和CurrentStateChanged事件可能存在问题。我这样说是因为,我可以用这种方法处理其他事件。我已经检查并重新检查了CallMethodAction方法签名(事件参数传递格式),但没有机会

如果您成功地获得了如上所述的CurrentStateChanged事件触发(或使用替代方法),我非常想知道

但是,对于VisualStateGroup的CurrentStateChanged事件,没有成功,如下所示

是,EventTriggerBehavior不适用于VisualStateGroup.CurrentStateChanged事件

可行的方法是创建一个专门针对该场景的定制行为,请参见Marco Minerva撰写的文章

此行为可以帮助我们在自定义属性(ViewModelState类型)的Set方法中监视当前VisualStatus,并根据需要调用方法:

public class MainViewModel : ViewModelBase
{
        public enum ViewModelState
        {
            Default,
            Details
        }

        private ViewModelState currentState;
        public ViewModelState CurrentState
        {
            get { return currentState; }
            set
            {
                this.Set(ref currentState, value);
                OnCurrentStateChanged(value);
            }
        }

        public RelayCommand GotoDetailsStateCommand { get; set; }
        public RelayCommand GotoDefaultStateCommand { get; set; }

        public MainViewModel()
        {
            GotoDetailsStateCommand = new RelayCommand(() =>
            {
                CurrentState = ViewModelState.Details;
            });

            GotoDefaultStateCommand = new RelayCommand(() =>
            {
                CurrentState = ViewModelState.Default;
            });
        }

        public void OnCurrentStateChanged(ViewModelState e)
        {
            Debug.WriteLine("CurrentStateChanged: " + e.ToString());
        }
}

请检查我在

上完成的示例,可能是由于最新的SDK,我成功地使其使用动态绑定(对于事件到方法模式),如下所示

在XAML中,绑定到
CurrentStateChanged
事件为:

<VisualStateGroup CurrentStateChanged="{x:Bind ViewModel.CurrentVisualStateChanged}">
上面的这些在我之前并不适用,现在我在VS2015更新2之后尝试过,我怀疑最新的SDK得到了增强??无论如何,现在可以通过动态绑定在视图模型中获得VisualState名称,这是一个好消息

public void CurrentVisualStateChanged(object sender, VisualStateChangedEventArgs e)
{
    var stateName = e?.NewState.Name;  // get VisualState name from View
    ...
    // more code to make use of the VisualState
}