Permalink
Browse files
Update SingleWindowDispatcherScheduler.cs (#2251)
type: fix
accessing CoreApplication.Views throws in XamlIslands, thus in this case get the CoreDispatcher by alternate means
issue: #2154
- Loading branch information...
Showing
with
22 additions
and
4 deletions.
-
+22
−4
src/ReactiveUI/Platforms/uap/SingleWindowDispatcherScheduler.cs
|
@@ -38,10 +38,24 @@ public class SingleWindowDispatcherScheduler : IScheduler |
|
|
/// </summary> |
|
|
public SingleWindowDispatcherScheduler() |
|
|
{ |
|
|
if (CoreApplication.Views.Count > 0) |
|
|
if (CoreApplication.Views.Count == 0) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
|
|
|
CoreDispatcher coreDispatcher; |
|
|
|
|
|
try |
|
|
{ |
|
|
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views[0].Dispatcher, null); |
|
|
coreDispatcher = CoreApplication.Views[0].Dispatcher; |
|
|
} |
|
|
catch |
|
|
{ |
|
|
// in the XamlIsland case, accessing Views throws. Thus, falling back to the old way. This HAS to be initialized on the MainThread. |
|
|
coreDispatcher = Window.Current.Dispatcher; |
|
|
} |
|
|
|
|
|
Interlocked.CompareExchange(ref _dispatcher, coreDispatcher, null); |
|
|
} |
|
|
|
|
|
/// <summary> |
|
@@ -152,9 +166,13 @@ private IDisposable ScheduleOnDispatcherNow<TState>(TState state, Func<ISchedule |
|
|
{ |
|
|
try |
|
|
{ |
|
|
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views.FirstOrDefault()?.Dispatcher, null); |
|
|
// if _dispatcher is still null (and only then) CompareExchange it with the dispatcher from the first view found |
|
|
if (_dispatcher == null) |
|
|
{ |
|
|
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views.FirstOrDefault()?.Dispatcher, null); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch |
|
|
{ |
|
|
// Ignore |
|
|
} |
|
|
0 comments on commit
02e932d