-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseNavigationViewModel.cs
executable file
·70 lines (61 loc) · 2.35 KB
/
BaseNavigationViewModel.cs
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
63
64
65
66
67
68
69
70
using System;
using System.Threading.Tasks;
using Microsoft.MobCAT.MVVM.Abstractions;
namespace Microsoft.MobCAT.MVVM
{
public class BaseNavigationViewModel : BaseViewModel
{
private static INavigationService _registeredService;
private static Func<INavigationService> _registeredServiceFunc;
/// <summary>
/// Gets or sets whether the ViewModel is modal.
/// </summary>
/// <value><c>true</c> if is modal; otherwise, <c>false</c>.</value>
public bool IsModal { get; set; }
protected INavigationService Navigation
{
get
{
if (_registeredService == null)
{
_registeredService = _registeredServiceFunc?.Invoke() ??
ServiceContainer.Resolve<INavigationService>(nullIsAcceptable: true) ??
ServiceContainer.Resolve<IRouteNavigationService>(nullIsAcceptable: true) as INavigationService;
if (_registeredService == null)
Logger.Warn($"No {nameof(INavigationService)} or {nameof(IRouteNavigationService)} implementation has been registered.");
}
return _registeredService;
}
}
/// <summary>
/// Registers the INavigationService
/// </summary>
/// <param name="service">Service to register.</param>
public static void RegisterService(INavigationService service)
{
ServiceContainer.Register<INavigationService>(service);
_registeredServiceFunc = null;
_registeredService = service;
}
/// <summary>
/// Lazily registers the INavigationService
/// </summary>
/// <param name="service">Service to register.</param>
public static void RegisterService(Func<INavigationService> service)
{
ServiceContainer.Register<INavigationService>(service);
_registeredService = null;
_registeredServiceFunc = service;
}
/// <summary>
/// Dismisses the ViewModel.
/// </summary>
/// <returns>The Task.</returns>
public virtual Task Dismiss()
{
if (IsModal)
return Navigation.PopModalAsync();
return Navigation.PopAsync();
}
}
}