Browse Source

feature: Support ReactiveUI WhenActivated

pull/1951/head
artyom 8 years ago
parent
commit
dd528260fe
  1. 4
      src/Avalonia.ReactiveUI/AppBuilderExtensions.cs
  2. 38
      src/Avalonia.ReactiveUI/AvaloniaActivationForViewFetcher.cs

4
src/Avalonia.ReactiveUI/AppBuilderExtensions.cs

@ -4,6 +4,7 @@
using Avalonia.Controls;
using Avalonia.Threading;
using ReactiveUI;
using Splat;
namespace Avalonia
{
@ -15,6 +16,9 @@ namespace Avalonia
return builder.AfterSetup(_ =>
{
RxApp.MainThreadScheduler = AvaloniaScheduler.Instance;
Locator.CurrentMutable.Register(
() => new AvaloniaActivationForViewFetcher(),
typeof(IActivationForViewFetcher));
});
}
}

38
src/Avalonia.ReactiveUI/AvaloniaActivationForViewFetcher.cs

@ -0,0 +1,38 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Reflection;
using System.Reactive.Linq;
using Avalonia;
using Avalonia.VisualTree;
using ReactiveUI;
namespace Avalonia
{
public class AvaloniaActivationForViewFetcher : IActivationForViewFetcher
{
public int GetAffinityForView(Type view)
{
return typeof(IVisual).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ? 10 : 0;
}
public IObservable<bool> GetActivationForView(IActivatable view)
{
if (!(view is IVisual visual)) return Observable.Return(false);
var viewLoaded = Observable
.FromEventPattern<VisualTreeAttachmentEventArgs>(
x => visual.AttachedToVisualTree += x,
x => visual.DetachedFromVisualTree -= x)
.Select(args => true);
var viewUnloaded = Observable
.FromEventPattern<VisualTreeAttachmentEventArgs>(
x => visual.DetachedFromVisualTree += x,
x => visual.DetachedFromVisualTree -= x)
.Select(args => false);
return viewLoaded
.Merge(viewUnloaded)
.DistinctUntilChanged();
}
}
}
Loading…
Cancel
Save