Skip to content

Commit c580e40

Browse files
committed
Add a way to lazily bolt on Activated / Deactivated
1 parent d436761 commit c580e40

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

ReactiveUI/Activation.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive;
55
using System.Reactive.Disposables;
66
using System.Reactive.Linq;
7+
using System.Reflection;
78
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -92,4 +93,19 @@ public static IDisposable WhenActivated(this IViewFor This, Action<Action<IDispo
9293
}).Item2;
9394
}, RxApp.SmallCacheLimit);
9495
}
96+
97+
public class CanActivateViewFetcher : IActivationForViewFetcher
98+
{
99+
public int GetAffinityForView(Type view)
100+
{
101+
return (typeof(ICanActivate).GetTypeInfo().IsAssignableFrom(view.GetType().GetTypeInfo())) ?
102+
10 : 0;
103+
}
104+
105+
public Tuple<IObservable<Unit>, IObservable<Unit>> GetActivationForView(IViewFor view)
106+
{
107+
var ca = view as ICanActivate;
108+
return Tuple.Create(ca.Activated, ca.Deactivated);
109+
}
110+
}
95111
}

ReactiveUI/Interfaces.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,12 @@ public interface ISupportsActivation
444444
ViewModelActivator Activator { get; }
445445
}
446446

447+
public interface ICanActivate
448+
{
449+
public IObservable<Unit> Activated { get; }
450+
public IObservable<Unit> Deactivated { get; }
451+
}
452+
447453
/// <summary>
448454
/// Allows an additional string to make view resolution more specific than just a type.
449455
/// </summary>

ReactiveUI/Registrations.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Register(Action<Func<object>, Type> registerFunction)
1919
registerFunction(() => new StringConverter(), typeof(IBindingTypeConverter));
2020
registerFunction(() => new DefaultViewLocator(), typeof(IViewLocator));
2121
registerFunction(() => new DummySuspensionHost(), typeof(ISuspensionHost));
22+
registerFunction(() => new CanActivateViewFetcher(), typeof(IActivationForViewFetcher));
2223
}
2324
}
2425
}

0 commit comments

Comments
 (0)