使用EventToCommand & PassEventArgsToCommand :: 如何获取发送者,或者更好的比喻?

发布于 2024-10-11 08:09:49 字数 1428 浏览 0 评论 0原文

我所做的事情的重点是,视图模型中需要发生很多事情,但是当视图加载时,而不是在构造函数上发生。我可以连接事件处理程序并发送消息,但这对我来说似乎有点草率。我正在实现一个基本视图和基本视图模型,其中包含此逻辑,因此我的所有视图都默认获得它,希望如此。

也许我什至无法得到我想要的东西:发件人。我只是想这就是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思慕 2024-10-18 08:09:49

我尝试使用直接的 xaml 和命令(如果可能)来解决这些问题。我在 View (UserControl) 本身上使用了 EventToCommand,如下所示:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="Loaded">
    <mvvmlight:EventToCommand Command="{Binding LoadCommand}" />
  </i:EventTrigger>
</i:Interaction.Triggers>

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:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="Loaded">
    <mvvmlight:EventToCommand Command="{Binding LoadCommand}" />
  </i:EventTrigger>
</i:Interaction.Triggers>
屌丝范 2024-10-18 08:09:49

Joe,

我对 MVVM 的立场是:
View:了解ViewModel
ViewModel :了解模型(不了解视图)
模型:保存数据。 (对 ViewModel、View 没有了解)

如果我处于您的情况,我会让基本 ViewModel 实现自定义接口 IViewModel。 (事实上​​,我有一个 IViewModel 接口,它是由我的公共抽象类 ViewModelBase 实现的,并且我的所有 ViewModel 都继承 ViewModelBase。)

public interface IViewModel
{  
   void Load();
   void UnLoad();
   // other Properties, Methods, etc.
}
public abstract class ViewModelBase : IViewModel
{
   public virtual/*or abstract*/ void Load()
   {
      // code
   }
}

然后在您的视图中:

this.Loaded += (o, e) => { if(ViewModel != null) { ViewModel.Load(); } }  

最后,区分特定视图和/或视图模型,我使用公共字符串令牌(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 an IViewModel interface which is implemented by my public abstract class ViewModelBase and all my ViewModels inherit ViewModelBase.)

public interface IViewModel
{  
   void Load();
   void UnLoad();
   // other Properties, Methods, etc.
}
public abstract class ViewModelBase : IViewModel
{
   public virtual/*or abstract*/ void Load()
   {
      // code
   }
}

then in your View:

this.Loaded += (o, e) => { if(ViewModel != null) { ViewModel.Load(); } }  

Finally, to distinguish among specific Views and/or ViewModels, I use a public string Token (Guid-generated).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文