简体   繁体   中英

How to return an IObservable<DialogResult> from ShowDialog

I'm looking to create an extension method with the following signature:

public static IObservable<DialogResult> ShowDialog(this Form form);

I don't know how to get this to work. This is what I have so far:

Task<DialogResult> task = Task.Factory.StartNew(() =>
{
    return form.ShowDialog();
});
return task.ToObservable();

Edit: Cool, thanks guys. I really need to read up on this stuff some more!

I imagine this would work, though I don't understand the point.

public static class Extensions
{
    public static IObservable<DialogResult> ShowDialogObservable(this Form form)
    {
        return Observable.Create<DialogResult>(o =>
        {
            o.OnNext(form.ShowDialog());
            return Disposable.Empty;
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM