Viewmodels are now maintained throughout the app lifecycle
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Aurora.Design.Components.NavigationMenu;
|
||||
using Aurora.Design.Views.MainView;
|
||||
@ -11,10 +12,14 @@ namespace Aurora.Design.Views.Main
|
||||
[XamlCompilation(XamlCompilationOptions.Compile)]
|
||||
public partial class MainView : MasterDetailPage
|
||||
{
|
||||
private Dictionary<int, BaseViewModel> _viewModels;
|
||||
private BaseViewModel _lastViewModel;
|
||||
|
||||
public MainView()
|
||||
{
|
||||
InitializeComponent();
|
||||
BindingContext = new MainViewModel();
|
||||
_viewModels = new Dictionary<int, BaseViewModel>();
|
||||
MasterPage.ListView.ItemSelected += ListView_ItemSelected;
|
||||
|
||||
Appearing += OnAppearing;
|
||||
@ -32,6 +37,33 @@ namespace Aurora.Design.Views.Main
|
||||
|
||||
var view = (View)Activator.CreateInstance(item.TargetType);
|
||||
|
||||
//Check if we have an instantiated viewModel
|
||||
BaseViewModel vm = new BaseViewModel();
|
||||
if (_viewModels.ContainsKey(item.Id))
|
||||
{
|
||||
_viewModels.TryGetValue(item.Id, out vm);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.TargetViewModelType.BaseType != typeof(BaseViewModel))
|
||||
{
|
||||
throw new InvalidOperationException("TargetViewModel field must be of type BaseViewModel");
|
||||
}
|
||||
|
||||
//Instantiate new view model
|
||||
vm = (BaseViewModel)Activator.CreateInstance(item.TargetViewModelType);
|
||||
_viewModels.Add(item.Id, vm);
|
||||
}
|
||||
//Activate viewmodel
|
||||
vm.OnActive();
|
||||
|
||||
//Deactivate last viewModel
|
||||
_lastViewModel.OnInactive();
|
||||
|
||||
//Assign viewModel
|
||||
_lastViewModel = vm;
|
||||
view.BindingContext = vm;
|
||||
|
||||
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
|
||||
viewContent.Content = view;
|
||||
|
||||
@ -47,7 +79,26 @@ namespace Aurora.Design.Views.Main
|
||||
{
|
||||
//Set initial view from first item in list
|
||||
ObservableCollection<NavigationGroupItem> screenList = (ObservableCollection<NavigationGroupItem>)MasterPage.ListView.ItemsSource;
|
||||
var view = (View)Activator.CreateInstance(screenList.FirstOrDefault().FirstOrDefault().TargetType);
|
||||
|
||||
//Assign viewModel
|
||||
NavigationItem firstNavItem = screenList.FirstOrDefault().FirstOrDefault();
|
||||
var view = (View)Activator.CreateInstance(firstNavItem.TargetType);
|
||||
|
||||
BaseViewModel vm = new BaseViewModel();
|
||||
if (_viewModels.ContainsKey(firstNavItem.Id))
|
||||
{
|
||||
_viewModels.TryGetValue(firstNavItem.Id, out vm);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Instantiate new view model
|
||||
vm = (BaseViewModel)Activator.CreateInstance(firstNavItem.TargetViewModelType);
|
||||
_viewModels.Add(firstNavItem.Id, vm);
|
||||
}
|
||||
|
||||
view.BindingContext = vm;
|
||||
_lastViewModel = vm;
|
||||
vm.OnActive();
|
||||
|
||||
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
|
||||
viewContent.Content = view;
|
||||
|
@ -32,12 +32,12 @@ namespace Aurora.Design.Views.MainView
|
||||
{
|
||||
_pages = new ObservableCollection<NavigationItem>(new[]
|
||||
{
|
||||
new NavigationItem { Id = 4, Title = "Party", Group="Social", TargetType = typeof(PartyView)},
|
||||
new NavigationItem { Id = 5, Title = "Profile", Group="Social", TargetType = typeof(ProfileView)},
|
||||
new NavigationItem { Id = 0, Title = "Songs", Group="Library", TargetType = typeof(SongsView) },
|
||||
new NavigationItem { Id = 1, Title = "Artists", Group="Library", TargetType = typeof(ArtistsView)},
|
||||
new NavigationItem { Id = 2, Title = "Albums", Group="Library", TargetType = typeof(AlbumsView)},
|
||||
new NavigationItem { Id = 3, Title = "Stations", Group="Library", TargetType = typeof(StationsView)},
|
||||
new NavigationItem { Id = 0, Title = "Songs", Group="Library", TargetType = typeof(SongsView), TargetViewModelType = typeof(SongsViewModel) },
|
||||
new NavigationItem { Id = 1, Title = "Artists", Group="Library", TargetType = typeof(ArtistsView), TargetViewModelType = typeof(ArtistsViewModel)},
|
||||
new NavigationItem { Id = 2, Title = "Albums", Group="Library", TargetType = typeof(AlbumsView), TargetViewModelType = typeof(AlbumsViewModel)},
|
||||
new NavigationItem { Id = 3, Title = "Stations", Group="Library", TargetType = typeof(StationsView), TargetViewModelType = typeof(StationsViewModel)},
|
||||
new NavigationItem { Id = 4, Title = "Party", Group="Social", TargetType = typeof(PartyView), TargetViewModelType = typeof(PartyViewModel)},
|
||||
new NavigationItem { Id = 5, Title = "Profile", Group="Social", TargetType = typeof(ProfileView), TargetViewModelType = typeof(ProfileViewModel)},
|
||||
});
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user