-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
62 lines (55 loc) · 2.5 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
62 lines (55 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using CommunityToolkit.Mvvm.Input;
using MaterialDesignThemes.Wpf;
using Microsoft.Extensions.DependencyInjection;
using System.Reactive.Linq;
using System.Windows;
namespace DialogHostStartupExample;
public partial class MainWindow
{
public const string DialogHostIdentifier = "MyDialogHost";
public MainWindow(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
InitializeComponent();
//Scenario 1: fails due to System.InvalidOperationException: 'No loaded DialogHost instances.'.
Observable
.FromEventPattern<RoutedEventArgs>(this, nameof(MainWindow.Loaded))
.Select(_ => Observable.FromAsync(() => ShowTestViewCommand.ExecuteAsync(null), System.Reactive.Concurrency.TaskPoolScheduler.Default))
.Concat()
.Take(1)
.Subscribe();
//Scenario 2: works because of small delay. Not ideal.
//var uiScheduler = new System.Reactive.Concurrency.DispatcherScheduler(Dispatcher, System.Windows.Threading.DispatcherPriority.Background);
//Observable
// .FromEventPattern<RoutedEventArgs>(this, nameof(MainWindow.Loaded))
// .Delay(TimeSpan.FromSeconds(1), uiScheduler)
// .Select(_ => Observable.FromAsync(() => ShowTestViewCommand.ExecuteAsync(null), System.Reactive.Concurrency.TaskPoolScheduler.Default))
// .Concat()
// .Take(1)
// .Subscribe();
//Scenario 3: works because we wait for DialogHost.LoadedEvent. I think this introduces a new problem if DialogHost is alraedy loaded.
//Observable
// .FromEventPattern<RoutedEventArgs>(this, nameof(MainWindow.Loaded))
// .CombineLatest(Observable.FromEventPattern<RoutedEventArgs>(MyDialogHost, nameof(MyDialogHost.Loaded)), (a, b) => b.Sender)
// .Select(_ => Observable.FromAsync(() => ShowTestViewCommand.ExecuteAsync(null), System.Reactive.Concurrency.TaskPoolScheduler.Default))
// .Concat()
// .Take(1)
// .Subscribe();
}
private readonly IServiceProvider serviceProvider;
[RelayCommand]
private async Task ShowTestViewAsync()
{
try
{
if (MyDialogHost is { IsLoaded: true } && serviceProvider.GetService<Views.TestView>() is { } view)
{
await DialogHost.Show(view, DialogHostIdentifier);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(this, ex.Message);
}
}
}