Using Rx with MVVM pattern
-
I have a canvas which I hook to the ViewModel:
With the ViewModel:
class MainViewModel
{
public MainViewModel()
{
this.MouseDown = new DelegateCommand(
this.OnMouseDown, this.CanMouseDown);
}public ICommand MouseDown { get; private set; } private void OnMouseDown(object arg) { //Do Something } private bool CanMouseDown(object arg) { return true; } }
But I really don't need all these details, I just need the positions received:
var MouseDownPosition = from evt in Observable.FromEventPattern(
h => cnvMainView.MouseDown += h,
h => cnvMainView.MouseDown -= h)
select evt.EventArgs.GetPosition(cnvMainView);So how do people hook the Rx observables to the ViewModel?
-
I have a canvas which I hook to the ViewModel:
With the ViewModel:
class MainViewModel
{
public MainViewModel()
{
this.MouseDown = new DelegateCommand(
this.OnMouseDown, this.CanMouseDown);
}public ICommand MouseDown { get; private set; } private void OnMouseDown(object arg) { //Do Something } private bool CanMouseDown(object arg) { return true; } }
But I really don't need all these details, I just need the positions received:
var MouseDownPosition = from evt in Observable.FromEventPattern(
h => cnvMainView.MouseDown += h,
h => cnvMainView.MouseDown -= h)
select evt.EventArgs.GetPosition(cnvMainView);So how do people hook the Rx observables to the ViewModel?
I gave up on the Rx part and did my own EventCommand:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;namespace WpfSimpleReflectionAssistant
{
//Code from:
//https://social.msdn.microsoft.com/Forums/silverlight/en-US/5cd586e7-640f-447b-9040-e9270173abf7/passing-drop-event-data-in-a-command-parameter-using-mvvm-and-the-interactivity-framework?forum=silverlightmvvm
public sealed class EventCommand : TriggerAction
{public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommand), null); public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof(ICommand), typeof(EventCommand), null); public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register( "InvokeParameter", typeof(object), typeof(EventCommand), null); private string commandName; public object InvokeParameter { get { return this.GetValue(InvokeParameterProperty); } set { this.SetValue(InvokeParameterProperty, value); } } public ICommand Command { get { return (ICommand)this.GetValue(CommandProperty); } set { this.SetValue(CommandProperty, value); } } public string CommandName { get { return this.commandName; } set { if (this.CommandName != value) { this.commandName = value; } } } public object CommandParameter { get { return this.GetValue(CommandParameterProperty); } set { this.SetValue(CommandParameterProperty, value); } } public object Sender { get; set; } protected override void Invoke(object parameter) { this.InvokeParameter = parameter; if (this.AssociatedObject != null) { ICommand command