Created
          December 13, 2018 16:27 
        
      - 
      
- 
        Save Henkoglobin/9bdfc3607b92c2ff1db41287f663ecc0 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public partial class App : Application, IAppNavigationResolver { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public App() { | |
| InitializeComponent(); | |
| var container = new UnityDependencyContainer() | |
| .Register<ISubViewModel, SubViewModel>() | |
| .Register<IMainViewModel, MainViewModel>(); | |
| var navigationService = new NavigationService(this, container); | |
| container.Register<INavigationService>(navigationService); | |
| navigationService | |
| .RegisterPage<IMainViewModel, MainPage>() | |
| .RegisterPage<ISubViewModel, SubPage>(); | |
| var viewModel = container.Resolve<IMainViewModel>(); | |
| viewModel.ActivateAsync(); | |
| MainPage = new NavigationPage(new MainPage() { | |
| BindingContext = viewModel | |
| }); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public async Task<IViewModel> GoBackAsync() { | |
| await ((NavigationPage)this.MainPage).Navigation.PopAsync(); | |
| var newPage = ((NavigationPage)this.MainPage).CurrentPage; | |
| return (IViewModel)newPage.BindingContext; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public interface IMainViewModel : IViewModel { | |
| ICommand SubCommand { get; } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public interface ISubViewModel : IViewModel { | |
| ICommand GoBackCommand { get; } | |
| String Something { get; } | |
| void Configure(string something); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public interface IViewModel { | |
| Task ActivateAsync(); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class MainViewModel : IMainViewModel { | |
| private INavigationService navigationService; | |
| public ICommand SubCommand { get; } | |
| public MainViewModel(INavigationService navigationService) { | |
| this.navigationService = navigationService; | |
| this.SubCommand = new RelayCommand(Navigate); | |
| } | |
| private async void Navigate() { | |
| var viewModel = await this.navigationService.NavigateAsync<ISubViewModel>(); | |
| viewModel.configure("This is awesome!"); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class SubViewModel : ISubViewModel, INotifyPropertyChanged { | |
| public event PropertyChangedEventHandler PropertyChanged; | |
| private INavigationService navigationService; | |
| public ICommand GoBackCommand { get; } | |
| private string something; | |
| public string Something { | |
| get { return this.something; } | |
| set { | |
| this.something = value; | |
| this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.Something))); | |
| } | |
| } | |
| public SubViewModel(INavigationService navigationService) { | |
| this.navigationService = navigationService; | |
| this.GoBackCommand = new RelayCommand(NavigateBack); | |
| } | |
| public void Configure(string something) { | |
| this.Something = something; | |
| } | |
| private async void NavigateBack() { | |
| await this.navigationService.GoBackAsync(); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment