2

I'm new to ReactiveUI and currently happy with it. But I have one question: How to handle windows event/message the ReactiveUI way?

In mvvmlight, it's like this:

<Window ...
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:command="http://www.galasoft.ch/mvvmlight">
<i:Interaction.Triggers>
  <i:EventTrigger EventName="Closing">
    <command:EventToCommand Command="{Binding ClosingCommand}"
                        PassEventArgsToCommand="True" />
  </i:EventTrigger>
</i:Interaction.Triggers>

But I don't know how to do it in ReactiveUI. Maybe it's something similar to

MessageBus.Current.Listen<KeyUpEventArgs>()  
    .Where(e => e.KeyCode == KeyCode.Up)
    .Subscribe(x => Console.WriteLine("Up Pressed!"));

but I don't know how to do it for mainframe's Closing event specifically.

I have read somewhere that the author of ReactiveUI is not strongly against code behind code (maybe I remember wrong), what's the suggested way then?

Felix
  • 2,673
  • 3
  • 30
  • 38

1 Answers1

4

There is a separate Nuget package ReactiveUI Events, which contains helpers (extension methods) exposing events as IObservables which you can use inside views. It is described in the docs.

Basically, your code behind in the view will contain something similar to this:

this.Events().Closing
    .Subscribe(_ => Console.WriteLine("Bye!"));

Take a look at this question as well.

pmbanka
  • 1,902
  • 17
  • 24
  • Thanks! I actually use [reactiveui-events.Net40](https://www.nuget.org/packages/reactiveui-events.Net40) instead because I'm using .net 4.0. – Felix Jul 21 '16 at 02:29