Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix UWP activating too soon (#1930)
- Loading branch information
1 parent
829e702
commit 49de639
Showing
2 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/ReactiveUI/Platforms/uap10.0.16299/ActivationForViewFetcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using System.Reflection; | ||
using System.Windows; | ||
|
||
using Windows.UI.Xaml; | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// ActiveationForViewFetcher is how ReactiveUI determine when a | ||
/// View is activated or deactivated. This is usually only used when porting | ||
/// ReactiveUI to a new UI framework. | ||
/// </summary> | ||
public class ActivationForViewFetcher : IActivationForViewFetcher | ||
{ | ||
/// <inheritdoc/> | ||
public int GetAffinityForView(Type view) | ||
{ | ||
return typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ? 10 : 0; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IObservable<bool> GetActivationForView(IActivatable view) | ||
{ | ||
var fe = view as FrameworkElement; | ||
|
||
if (fe == null) | ||
{ | ||
return Observable<bool>.Empty; | ||
} | ||
|
||
var viewLoaded = WindowsObservable.FromEventPattern<FrameworkElement, object>( | ||
x => fe.Loading += x, | ||
x => fe.Loading -= x).Select(_ => true); | ||
|
||
var viewUnloaded = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>( | ||
x => fe.Unloaded += x, | ||
x => fe.Unloaded -= x).Select(_ => false); | ||
|
||
return viewLoaded | ||
.Merge(viewUnloaded) | ||
.Select(b => b ? fe.WhenAnyValue(x => x.IsHitTestVisible).SkipWhile(x => !x) : Observables.False) | ||
.Switch() | ||
.DistinctUntilChanged(); | ||
} | ||
} | ||
} |