forked from PrismLibrary/Prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewModelBase.cs
112 lines (89 loc) · 3.5 KB
/
ViewModelBase.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace MauiModule.ViewModels;
public abstract class ViewModelBase : BindableBase, IInitialize, INavigatedAware, IPageLifecycleAware
{
protected INavigationService _navigationService { get; }
protected IPageDialogService _pageDialogs { get; }
protected IDialogService _dialogs { get; }
protected ViewModelBase(BaseServices baseServices)
{
_navigationService = baseServices.NavigationService;
_pageDialogs = baseServices.PageDialogs;
_dialogs = baseServices.Dialogs;
Title = Regex.Replace(GetType().Name, "ViewModel", string.Empty);
Id = Guid.NewGuid().ToString();
NavigateCommand = new DelegateCommand<string>(OnNavigateCommandExecuted);
ShowPageDialog = new DelegateCommand(OnShowPageDialog);
Messages = new ObservableCollection<string>();
Messages.CollectionChanged += (sender, args) =>
{
foreach (string message in args.NewItems)
Console.WriteLine($"{Title} - {message}");
};
AvailableDialogs = baseServices.DialogRegistry.Registrations.Select(x => x.Name).ToList();
SelectedDialog = AvailableDialogs.FirstOrDefault();
ShowDialog = new DelegateCommand(OnShowDialogCommand, () => !string.IsNullOrEmpty(SelectedDialog))
.ObservesProperty(() => SelectedDialog);
GoBack = new DelegateCommand<string>(OnGoBack);
}
public IEnumerable<string> AvailableDialogs { get; }
public string Title { get; }
public string Id { get; }
private string _selectedDialog;
public string SelectedDialog
{
get => _selectedDialog;
set => SetProperty(ref _selectedDialog, value);
}
public ObservableCollection<string> Messages { get; }
public DelegateCommand<string> NavigateCommand { get; }
public DelegateCommand ShowPageDialog { get; }
public DelegateCommand ShowDialog { get; }
public DelegateCommand<string> GoBack { get; }
private void OnNavigateCommandExecuted(string uri)
{
Messages.Add($"OnNavigateCommandExecuted: {uri}");
_navigationService.NavigateAsync(uri)
.OnNavigationError(ex => Console.WriteLine(ex));
}
private void OnShowPageDialog()
{
Messages.Add("OnShowPageDialog");
_pageDialogs.DisplayAlertAsync("Message", $"Hello from {Title}. This is a Page Dialog Service Alert!", "Ok");
}
private void OnShowDialogCommand()
{
Messages.Add("OnShowDialog");
_dialogs.ShowDialog(SelectedDialog, null, DialogCallback);
}
private void DialogCallback(IDialogResult result) =>
Messages.Add("Dialog Closed");
private void OnGoBack(string viewName)
{
Messages.Add($"On Go Back {viewName}");
_navigationService.GoBackAsync(viewName);
}
public void Initialize(INavigationParameters parameters)
{
Messages.Add("ViewModel Initialized");
foreach (var parameter in parameters.Where(x => x.Key.Contains("message")))
Messages.Add(parameter.Value.ToString());
}
public void OnNavigatedFrom(INavigationParameters parameters)
{
Messages.Add("ViewModel NavigatedFrom");
}
public void OnNavigatedTo(INavigationParameters parameters)
{
Messages.Add("ViewModel NavigatedTo");
}
public void OnAppearing()
{
Messages.Add("View Appearing");
}
public void OnDisappearing()
{
Messages.Add("View Disappearing");
}
}