预先感谢您的帮助。
我是第一次在MVVM中进行开发,我想将 view模型的业务逻辑分为两个类,即 MyViewViewModel 和 MyViewCommand 。
实际上,我使用RelayCommand绑定视图模型中的命令。
MyViewCommandHandler:
/// <summary>
/// Constructor
/// </summary>
public ConfigRefCommandHandler(Action requestClose)
{
base.RequestClose += requestClose;
// Assign all operations to commands
// Config
this.CmdSelectAllReferences = new RelayCommand(this.SelectAllReferencesCmd, this.CanExecuteSelectAllReferencesCmd);
this.CmdModifyDetails = new RelayCommand(this.ModifyDetailsCmd, this.CanExecuteModifyDetailsCmd);
this.CmdProhibitPlanning = new RelayCommand(this.ProhibitPlannningCmd, this.CanExecuteProhibitPlannningCmd);
this.CmdExit = new RelayCommand(this.ExitCmd, this.CanExecuteExitCmd);
// Macro
this.CmdAddMacro = new RelayCommand(this.AddMacroCmd, this.CanExecuteAddMacroCmd);
this.CmdModMacro = new RelayCommand(this.ModMacroCmd, this.CanExecuteModMacroCmd);
this.CmdDelMacro = new RelayCommand(this.DelMacroCmd, this.CanExecuteDelMacroCmd);
// Program
this.CmdAddProgram = new RelayCommand(this.AddProgramCmd, this.CanExecuteAddProgramCmd);
this.CmdModProgram = new RelayCommand(this.ModProgramCmd, this.CanExecuteModProgramCmd);
this.CmdDelProgram = new RelayCommand(this.DelProgramCmd, this.CanExecuteDelProgramCmd);
}
MyViewViewModel:
/// <summary>
/// Internal reference
/// </summary>
public String InternalReference
{
get
{
return "test";
}
set
{
base.OnPropertyChanged("InternalReference");
}
}
/// <summary>
/// Constructor
/// </summary>
public ConfigRefViewModel(Action requestClose)
{
ConfigRefCommandHandler commandHandler = new ConfigRefCommandHandler(requestClose);
// Config
this.CmdSelectAllReferences = commandHandler.CmdSelectAllReferences;
this.CmdModifyDetails = commandHandler.CmdModifyDetails;
this.CmdProhibitPlanning = commandHandler.CmdProhibitPlanning;
this.CmdExit = commandHandler.CmdExit;
// Macro
this.CmdAddMacro = commandHandler.CmdAddMacro;
this.CmdModMacro = commandHandler.CmdModMacro;
this.CmdDelMacro = commandHandler.CmdDelMacro;
// Program
this.CmdAddProgram = commandHandler.CmdAddProgram;
this.CmdModProgram = commandHandler.CmdModProgram;
this.CmdDelProgram = commandHandler.CmdDelProgram;
}
如何将 MyViewViewModel 传递到 MyViewCommand 以设置 InternalReference 属性以激活 OnPropertyChanged()方法重新加载 MyView ?
答案 0 :(得分:0)
最后,我使用了另一个类来获取所有 ViewModel 。
该课程是静态课,她使用 Singleton 模式,如您所见:
/// <summary>
/// View model handler for all ViewModel
/// </summary>
static class ViewModelHandler
{
#region Properties
/// <summary>
/// Config reference ViewModel
/// </summary>
private static ConfigRefViewModel ConfigRefViewModel = null;
#endregion
#region Getter
/// <summary>
/// Get the ConfigRefViewModel property
/// </summary>
/// <param name="requestClose">The request for closing view</param>
/// <returns>The view model of config reference view</returns>
public static ConfigRefViewModel GetConfigRefViewModel(Action requestClose = null)
{
if (ViewModelHandler.ConfigRefViewModel != null) return ViewModelHandler.ConfigRefViewModel;
ViewModelHandler.SetConfigRefViewModel(new ConfigRefViewModel());
ViewModelHandler.GetConfigRefViewModel().Initialize(requestClose);
return ViewModelHandler.ConfigRefViewModel;
}
#endregion
#region Setter
/// <summary>
/// Set the ConfigRefViewModel property
/// </summary>
/// <param name="configRefViewModel">ConfigRefViewModel to set</param>
private static void SetConfigRefViewModel(ConfigRefViewModel configRefViewModel) => ViewModelHandler.ConfigRefViewModel = configRefViewModel;
#endregion
}
为使其正常工作,我删除了 ConfigRefViewModel 的构造函数并将其放在初始化方法中,因为在该构造函数中, ConfigRefCommand 类是即时的。>
这是一个问题,因为 ConfigRefCommand 类需要关闭视图的方法,而我的 ViewModelHandler 需要在其中实例化一个空的 ConfigRefViewModel 首先。