使用EventToCommand & PassEventArgsToCommand :: 如何获取发送者,或者更好的比喻?
我所做的事情的重点是,视图模型中需要发生很多事情,但是当视图加载时,而不是在构造函数上发生。我可以连接事件处理程序并发送消息,但这对我来说似乎有点草率。我正在实现一个基本视图和基本视图模型,其中包含此逻辑,因此我的所有视图都默认获得它,希望如此。
也许我什至无法得到我想要的东西:发件人。我只是想这就是 RoutedEventArgs.OriginalSource 应该是什么?
[编辑] 与此同时,我在 xaml.cs 中连接了一个 EventHandler,果然,OriginalSource 在那里也为 null。所以我想我真的需要知道是否也可以在命令中获取对视图/发送者的引用? [/编辑]
我的实现要求我的视图模型的帮助器类负责创建“窗口”,它知道所有窗口都添加到的“主机”控件。我愿意接受在使用 eventtocommand 范围之外完成此任务的建议。 :)
(Unloaded 的代码是相同的)
#region ViewLoadedCommand
private RelayCommand<RoutedEventArgs> _viewLoadedCommand = null;
/// <summary>
/// Command to handle the control's Loaded event.
/// </summary>
public RelayCommand<RoutedEventArgs> ViewLoadedCommand
{
get
{
// lazy-instantiate the RelayCommand on first usage
if (_viewLoadedCommand == null)
{
_viewLoadedCommand = new RelayCommand<RoutedEventArgs>(
e => this.OnViewLoadedCommand(e));
}
return _viewLoadedCommand;
}
}
#endregion ViewLoadedCommand
#region View EventHandlers
protected virtual void OnViewLoadedCommand(RoutedEventArgs e)
{
EventHandler handler = ViewLoaded;
if (handler != null)
{
handler(this, e);
}
}
#endregion
The point of what I'm doing is that there are a lot of things that need to happen in the viewmodel, but when the view has been loaded, not on constructor. I could wire up event handlers and send messages, but that just seems kinda sloppy to me. I'm implementing a base view and base viewmodel where this logic is contained so all my views get it by default, hopefully.
Perhaps I can't even get what I'm wanting: the sender. I just figured this is what RoutedEventArgs.OriginalSource was supposed to be?
[Edit] In the meantime, I've hooked up an EventHandler in the xaml.cs, and sure enough, OriginalSource is null there as well. So I guess really I need to know if it's possible to get a reference to the view/sender in the Command as well? [/Edit]
My implementation requires that a helper class to my viewmodels which is responsible for creating 'windows' knows of the 'host' control that all the windows get added to. i'm open to suggestions for accomplishing this outside the scope of using eventtocommand. :)
(the code for Unloaded is the same)
#region ViewLoadedCommand
private RelayCommand<RoutedEventArgs> _viewLoadedCommand = null;
/// <summary>
/// Command to handle the control's Loaded event.
/// </summary>
public RelayCommand<RoutedEventArgs> ViewLoadedCommand
{
get
{
// lazy-instantiate the RelayCommand on first usage
if (_viewLoadedCommand == null)
{
_viewLoadedCommand = new RelayCommand<RoutedEventArgs>(
e => this.OnViewLoadedCommand(e));
}
return _viewLoadedCommand;
}
}
#endregion ViewLoadedCommand
#region View EventHandlers
protected virtual void OnViewLoadedCommand(RoutedEventArgs e)
{
EventHandler handler = ViewLoaded;
if (handler != null)
{
handler(this, e);
}
}
#endregion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试使用直接的 xaml 和命令(如果可能)来解决这些问题。我在 View (UserControl) 本身上使用了 EventToCommand,如下所示:
I try to solve these problems with straight xaml and commanding when possible. I've used the EventToCommand on the View (UserControl) itself like this:
Joe,
我对 MVVM 的立场是:
View:了解ViewModel
ViewModel :了解模型(不了解视图)
模型:保存数据。 (对 ViewModel、View 没有了解)
如果我处于您的情况,我会让基本 ViewModel 实现自定义接口
IViewModel
。 (事实上,我有一个 IViewModel 接口,它是由我的公共抽象类 ViewModelBase 实现的,并且我的所有 ViewModel 都继承 ViewModelBase。)然后在您的视图中:
最后,区分特定视图和/或视图模型,我使用公共字符串令牌(Guid 生成)。
Joe,
My stance on MVVM is:
View : knows about ViewModel
ViewModel : knows about Model (has NO knowledge of View)
Model : holds data. (has NO knowledge of ViewModel, View)
If I was in your situation, I would have the base ViewModel implement a custom interface
IViewModel
. (In fact, I have anIViewModel
interface which is implemented by mypublic abstract class ViewModelBase
and all my ViewModels inherit ViewModelBase.)then in your View:
Finally, to distinguish among specific Views and/or ViewModels, I use a public string Token (Guid-generated).