Advertisement
GeneralGDA

Rx 2.0 primitive sample

Mar 8th, 2015
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. internal sealed partial class MainWindow
  2. {
  3.     //Image is an object of type System.Windows.Controls.Image from XAML: <Image x:Name="Image" ...
  4.     //Canvas is an object of type System.Windows.Controls.Canvas from XAML: <Canvas x:Name="Canvas" ...
  5.  
  6.     private IDisposable _unsubscribe;
  7.  
  8.     public MainWindow()
  9.     {
  10.         InitializeComponent();
  11.            
  12.         var mouseDownEvents = from e in Observable.FromEventPattern<MouseButtonEventHandler, MouseEventArgs>
  13.             (
  14.                 listener => listener.Invoke,
  15.                 listener => Image.MouseDown += listener,
  16.                 listener => Image.MouseDown -= listener
  17.             )
  18.             select e.EventArgs.GetPosition(Image);
  19.  
  20.         var mouseMoveEvents = from e in Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
  21.             (
  22.                 listener => listener.Invoke,
  23.                 listener => Image.MouseMove += listener,
  24.                 listener => Image.MouseMove -= listener
  25.             )
  26.             select e.EventArgs.GetPosition(Canvas);
  27.            
  28.         var mouseUpEvents = Observable.FromEventPattern<MouseButtonEventHandler, MouseEventArgs>
  29.             (
  30.                 listener => listener.Invoke,
  31.                 listener => Image.MouseUp += listener,
  32.                 listener => Image.MouseUp -= listener
  33.             );
  34.  
  35.         var mouseLeaveEvents = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
  36.             (
  37.                 listener => listener.Invoke,
  38.                 listener => Image.MouseLeave += listener,
  39.                 listener => Image.MouseLeave -= listener
  40.             );
  41.  
  42.         var dragAndDrops =
  43.             from clickPosition in mouseDownEvents
  44.                 from movePosition in mouseMoveEvents.TakeUntil(mouseUpEvents.Merge(mouseLeaveEvents))
  45.                     select new
  46.                     {
  47.                         X = movePosition.X - clickPosition.X,
  48.                         Y = movePosition.Y - clickPosition.Y
  49.                     };
  50.  
  51.         _unsubscribe = dragAndDrops.ObserveOn(SynchronizationContext.Current).Subscribe(newImagePosition =>
  52.         {
  53.             Canvas.SetLeft(Image, newImagePosition.X);
  54.             Canvas.SetTop(Image, newImagePosition.Y);
  55.         });
  56.     }
  57.  
  58.     private void StopTracking(object sender, RoutedEventArgs e)
  59.     {
  60.         if (null != _unsubscribe)
  61.         {
  62.             _unsubscribe.Dispose();
  63.             _unsubscribe = null;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement