gpt4 book ai didi

c# - VisualStateGroup的ViewModel方法事件

转载 作者:行者123 更新时间:2023-12-03 10:35:14 24 4
gpt4 key购买 nike

我已使用 EventTriggerBehavior CallMethodAction 成功将事件转换为方法(在ViewModel中使用),如以下示例所示(此处选择了一个页面 Loaded 事件进行说明)。
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:CallMethodAction TargetObject="{Binding Mode=OneWay}" MethodName="PageLoadedCommand"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>

但是,当涉及到 VisualStateGroup CurrentStateChanged 事件时,如下所示(是的,嵌套在<VisualStateGroup>块中,因为 CurrentStateChanged 事件属于 VisualStateGroup 事件),没有成功:
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="CurrentStateChanged">
<core:CallMethodAction MethodName="CurrentVisualStateChanged" TargetObject="{Binding Mode=OneWay}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>

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

如果您如上所述成功触发了 CurrentStateChanged 事件触发(或使用其他方法),我非常想知道。

最佳答案

However, no success when it comes to the CurrentStateChanged event of VisualStateGroup as shown below



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

可行的方法是创建专门针对此场景的 自定义行为,请参阅 撰写的this blog 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());
}
}

请检查我在Github上完成的示例

关于c# - VisualStateGroup的ViewModel方法事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com