gpt4 book ai didi

c# - MVVM 混合行为和 RelayCommand 未按预期工作

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

我正在尝试设置一些行为来控制 MVVM 应用程序中的窗口。

我的想法是行为可以在 XAML 中装饰性地设置并存在于它们自己的类中,允许在某些情况下在许多不同的 View 中重用它们。

我还有一个绑定(bind)到 RelayCommand 的菜单项。这两个都可以自己正常工作,但是当我尝试将这两件事结合起来时,并没有按预期工作:
MainView.xaml

<Window x:Class="CaseManager.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:CaseManager.ViewModels;assembly=CaseManager.ViewModels"
xmlns:local="clr-namespace:CaseManager.Views"
local:ExitBehavior.ExitWhen="{Binding ExitFlag}"
Title="Main Window" Height="350" Width="525">

<Window.DataContext>
<VM:MainViewModel />
</Window.DataContext>

<Grid>

<Menu Height="20" VerticalAlignment="Top">
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="{Binding ExitCommand}" />
</MenuItem>
</Menu>

</Grid>
</Window>
MainViewModel.cs
namespace CaseManager.ViewModels
{
using System;
using System.Collections.Generic;

using Commands;

public class MainViewModel : ViewModelBase
{
# region RelayCommands
private RelayCommand _exitCommand;
public RelayCommand ExitCommand
{
get { return _exitCommand; }
private set { _exitCommand = value; }
}
#endregion

private bool _exitFlag;
public bool ExitFlag {
get { return _exitFlag; }
private set { _exitFlag = value; }
}

public MainViewModel()
{
ExitCommand = new RelayCommand(ExitCommand_Execute);
ExitFlag = false;
}

private void ExitCommand_Execute(object obj)
{
System.Console.WriteLine("Command executed");
ExitFlag = true;
}
}
}
ExitCommand.cs
namespace CaseManager.Views
{
using System;
using System.Windows;

public static class ExitBehavior
{
public static bool GetExitWhen(DependencyObject obj)
{
return (bool)obj.GetValue(ExitWhenProperty);
}

public static void SetExitWhen(DependencyObject obj, bool value)
{
obj.SetValue(ExitWhenProperty, value);
}

public static readonly DependencyProperty ExitWhenProperty =
DependencyProperty.RegisterAttached(
"ExitWhen", typeof(bool), typeof(ExitBehavior),
new UIPropertyMetadata(OnExitWhenChanged));

static void OnExitWhenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
System.Console.WriteLine("Behavior executed");
Environment.Exit(0);
}
}
}

当我在菜单中选择退出时,按原样运行上面的代码将在控制台中输出“执行的命令”。所以我知道中继命令工作正常。但是行为不是。

如果我更改我的 MainViewModel 的构造函数设置 ExitFlag为 true,程序将启动,将“已执行行为”打印到控制台,然后退出。

ExitBehavior 和 RelayCommand 都单独工作,但是当我尝试通过在中继命令中设置 ExitFlag 来触发 ExitBehavior 时,它们不能一起使用。我在这里错过了什么吗?

为简洁起见,这里是 RelayCommand.cs :
namespace CaseManager.Commands
{
using System;
using System.Windows.Input;

public class RelayCommand : ICommand
{
#region Fields

readonly Action<object> _execute;
readonly Predicate<object> _canExecute;

#endregion

#region Constructors

public RelayCommand(Action<object> execute)
: this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}
#endregion

#region ICommand Members

public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameter)
{
_execute(parameter);
}

#endregion
}
}

最佳答案

您必须通知绑定(bind)引擎,您的属性 ExitFlag被改变了。假设 ViewModelBase实现 INotifyPropertyChanged 不知何故:

       public bool ExitFlag 
{
get { return _exitFlag; }
private set
{
_exitFlag = value;
OnPropertyChanged("ExitFlag");
}
}

在哪里 OnPropertyChanged是一种方法,它会引发 INPC.PropertyChanged事件。

关于c# - MVVM 混合行为和 RelayCommand 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787361/

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