Skip to content

Instantly share code, notes, and snippets.

@zirkelc
Created June 28, 2017 07:29
Show Gist options
  • Save zirkelc/5edb9ab23bb4a4aceb6ad915339e5eca to your computer and use it in GitHub Desktop.
Save zirkelc/5edb9ab23bb4a4aceb6ad915339e5eca to your computer and use it in GitHub Desktop.
public interface INavigationItem<out T> where T : class, IViewModel
{
Type ViewModelType { get; set; }
object ViewModel { get; set; }
}
public interface INavigationService
{
void ShowMasterPage();
void HideMasterPage();
//...
}
public class MenuViewModel : ViewModelBase
{
Command navigateCommand;
public Command NavigateCommand => navigateCommand ?? (navigateCommand = new Command(async (index) => await NavigateAsync((int)index)));
public ObservableRangeCollection<INavigationItem<ViewModelBase>> NavigationItems { get; } = new ObservableRangeCollection<INavigationItem<ViewModelBase>>();
public MenuViewModel()
{
NavigationItems.AddRange(new INavigationItem<ViewModelBase>[]
{
new NavigationItem<FeedViewModel>(),
new NavigationItem<FriendsViewModel>(),
new NavigationItem<GroupsViewModel>(),
new NavigationItem<SettingsViewModel>(),
});
}
private async Task NavigateAsync(int index)
{
NavigationService.Instance.HideMasterPage();
//TODO: maybe faster without reflection
var navigationItem = NavigationItems[index];
dynamic viewModel = navigationItem.ViewModel ?? (navigationItem.ViewModel = Activator.CreateInstance(navigationItem.ViewModelType));
await Task.Delay(225);
NavigationService.Instance.SwitchDetailPage(viewModel);
}
public class NavigationItem<T> : INavigationItem<T> where T : class, IViewModel
{
public Type ViewModelType { get; set; }
public object ViewModel { get; set; }
public NavigationItem()
{
ViewModelType = typeof(T);
}
}
public class NavigationService : INavigationService
{
MasterDetailPage MasterPage
{
get { return Application.Current.MainPage as MasterDetailPage; }
}
public void ShowMasterPage()
{
if(MasterPage != null)
MasterPage.IsPresented = true;
}
public void HideMasterPage()
{
if (MasterPage != null)
MasterPage.IsPresented = false;
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment