I've looked around at a number of the ReactiveUI samples, but I can't see a good simple example of how to handle exceptions, where a message should be displayed to the user. (If there is a good example can somebody point me to it?).
My first question is how to handle an exception with ReactiveCommand and ToProperty. For example, I have the following code:
public class MainWindowViewModel : ReactiveObject
{
public ReactiveCommand CalculateTheAnswer { get; set; }
public MainWindowViewModel()
{
CalculateTheAnswer = new ReactiveCommand();
CalculateTheAnswer
.SelectMany(_ => AnswerCalculator())
.ToProperty(this, x => x.TheAnswer);
CalculateTheAnswer.ThrownExceptions
.Select(exception => MessageBox.Show(exception.Message));
}
private readonly ObservableAsPropertyHelper<int> _theAnswer;
public int TheAnswer
{
get { return _theAnswer.Value; }
}
private static IObservable<int> AnswerCalculator()
{
var task = Task.Factory.StartNew(() =>
{
throw new ApplicationException("Unable to calculate answer, because I don't know what the question is");
return 42;
});
return task.ToObservable();
}
}
I think I must be misunderstanding ThrownExceptions, because this observable is not receiving any items when I run the code above. What am I doing wrong?
My second question is how would I do this in a MVVM-friendly way. This blog entry mentions a User Errors feature, but I can't find any documentation on how to use it. How would I implement it into the above example?
Edit: I've published an example solution on github based on Paul's answer below.
CalculateTheAnswer.ThrownExceptions.Select(exception => MessageBox.Show(exception.Message));
is missing actual subscription? – Simmon