Merge pull request #193 from dotnet-architecture/xamarin
Fixed app crash when switching from mock to real services.
This commit is contained in:
commit
1564873356
@ -5,6 +5,8 @@ namespace eShopOnContainers.Services
|
|||||||
{
|
{
|
||||||
public interface INavigationService
|
public interface INavigationService
|
||||||
{
|
{
|
||||||
|
ViewModelBase PreviousPageViewModel { get; }
|
||||||
|
|
||||||
Task InitializeAsync();
|
Task InitializeAsync();
|
||||||
|
|
||||||
Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;
|
Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;
|
||||||
|
@ -12,6 +12,16 @@ namespace eShopOnContainers.Services
|
|||||||
{
|
{
|
||||||
public class NavigationService : INavigationService
|
public class NavigationService : INavigationService
|
||||||
{
|
{
|
||||||
|
public ViewModelBase PreviousPageViewModel
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var mainPage = Application.Current.MainPage as CustomNavigationView;
|
||||||
|
var viewModel = mainPage.Navigation.NavigationStack[mainPage.Navigation.NavigationStack.Count - 2].BindingContext;
|
||||||
|
return viewModel as ViewModelBase;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Task InitializeAsync()
|
public Task InitializeAsync()
|
||||||
{
|
{
|
||||||
if(string.IsNullOrEmpty(Settings.AuthAccessToken))
|
if(string.IsNullOrEmpty(Settings.AuthAccessToken))
|
||||||
|
@ -3,6 +3,7 @@ using System.Windows.Input;
|
|||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using eShopOnContainers.Core.Helpers;
|
using eShopOnContainers.Core.Helpers;
|
||||||
|
using eShopOnContainers.Core.Models.User;
|
||||||
|
|
||||||
namespace eShopOnContainers.Core.ViewModels
|
namespace eShopOnContainers.Core.ViewModels
|
||||||
{
|
{
|
||||||
@ -67,13 +68,7 @@ namespace eShopOnContainers.Core.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand MockServicesCommand => new Command(MockServices);
|
public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
|
||||||
|
|
||||||
private void MockServices()
|
|
||||||
{
|
|
||||||
ViewModelLocator.RegisterDependencies(!UseAzureServices);
|
|
||||||
UpdateInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task InitializeAsync(object navigationData)
|
public override Task InitializeAsync(object navigationData)
|
||||||
{
|
{
|
||||||
@ -82,7 +77,30 @@ namespace eShopOnContainers.Core.ViewModels
|
|||||||
return base.InitializeAsync(navigationData);
|
return base.InitializeAsync(navigationData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateInfo()
|
private async Task ToggleMockServicesAsync()
|
||||||
|
{
|
||||||
|
ViewModelLocator.RegisterDependencies(!UseAzureServices);
|
||||||
|
UpdateInfo();
|
||||||
|
|
||||||
|
var previousPageViewModel = NavigationService.PreviousPageViewModel;
|
||||||
|
if (previousPageViewModel != null)
|
||||||
|
{
|
||||||
|
if (previousPageViewModel is MainViewModel)
|
||||||
|
{
|
||||||
|
// Slight delay so that page navigation isn't instantaneous
|
||||||
|
await Task.Delay(1000);
|
||||||
|
if (UseAzureServices)
|
||||||
|
{
|
||||||
|
Settings.AuthAccessToken = string.Empty;
|
||||||
|
Settings.AuthIdToken = string.Empty;
|
||||||
|
await NavigationService.NavigateToAsync<LoginViewModel>(new LogoutParameter { Logout = true });
|
||||||
|
await NavigationService.RemoveBackStackAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateInfo()
|
||||||
{
|
{
|
||||||
if (!UseAzureServices)
|
if (!UseAzureServices)
|
||||||
{
|
{
|
||||||
|
@ -121,7 +121,7 @@
|
|||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Animate="True"
|
Animate="True"
|
||||||
Checked="{Binding UseAzureServices, Mode=TwoWay}"
|
Checked="{Binding UseAzureServices, Mode=TwoWay}"
|
||||||
Command="{Binding MockServicesCommand}"
|
Command="{Binding ToggleMockServicesCommand}"
|
||||||
Style="{StaticResource SettingsToggleButtonStyle}">
|
Style="{StaticResource SettingsToggleButtonStyle}">
|
||||||
<controls:ToggleButton.CheckedImage>
|
<controls:ToggleButton.CheckedImage>
|
||||||
<OnPlatform x:TypeArguments="ImageSource"
|
<OnPlatform x:TypeArguments="ImageSource"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user