ViewModelLocator is now responsible for connecting view models to views.

Uses an auto-wiring convention-based approach.
This commit is contained in:
David Britch 2017-03-07 16:47:11 +00:00
parent a7f9d2c73c
commit 14efed11a8
27 changed files with 137 additions and 159 deletions

View File

@ -1,6 +1,6 @@
using eShopOnContainers.Core.Helpers; using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Services; using eShopOnContainers.Services;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Xaml; using Xamarin.Forms.Xaml;
@ -28,12 +28,13 @@ namespace eShopOnContainers
{ {
UseMockServices = Settings.UseMocks; UseMockServices = Settings.UseMocks;
ViewModelLocator.Instance.UpdateDependencies(UseMockServices); ViewModelLocator.Initialize();
ViewModelLocator.UpdateDependencies(UseMockServices);
} }
private Task InitNavigation() private Task InitNavigation()
{ {
var navigationService = ViewModelLocator.Instance.Resolve<INavigationService>(); var navigationService = ViewModelLocator.Resolve<INavigationService>();
return navigationService.InitializeAsync(); return navigationService.InitializeAsync();
} }

View File

@ -1,6 +1,6 @@
using eShopOnContainers.Core.Models.Basket; using eShopOnContainers.Core.Models.Basket;
using eShopOnContainers.Core.Models.Catalog; using eShopOnContainers.Core.Models.Catalog;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -21,7 +21,7 @@ namespace eShopOnContainers.Core.Helpers
try try
{ {
if (!ViewModelLocator.Instance.UseMockService if (!ViewModelLocator.UseMockService
&& Settings.UrlBase != GlobalSetting.DefaultEndpoint) && Settings.UrlBase != GlobalSetting.DefaultEndpoint)
{ {
foreach (var catalogItem in catalogItems) foreach (var catalogItem in catalogItems)
@ -54,7 +54,7 @@ namespace eShopOnContainers.Core.Helpers
try try
{ {
if (!ViewModelLocator.Instance.UseMockService if (!ViewModelLocator.UseMockService
&& Settings.UrlBase != GlobalSetting.DefaultEndpoint) && Settings.UrlBase != GlobalSetting.DefaultEndpoint)
{ {
foreach (var basketItem in basketItems) foreach (var basketItem in basketItems)

View File

@ -1,4 +1,4 @@
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using Plugin.Settings; using Plugin.Settings;
using Plugin.Settings.Abstractions; using Plugin.Settings.Abstractions;
@ -27,7 +27,7 @@ namespace eShopOnContainers.Core.Helpers
private const string IdUrlBase = "url_base"; private const string IdUrlBase = "url_base";
private static readonly string AccessTokenDefault = string.Empty; private static readonly string AccessTokenDefault = string.Empty;
private static readonly string IdTokenDefault = string.Empty; private static readonly string IdTokenDefault = string.Empty;
private static readonly bool UseMocksDefault = ViewModelLocator.Instance.UseMockService; private static readonly bool UseMocksDefault = ViewModelLocator.UseMockService;
private static readonly string UrlBaseDefault = GlobalSetting.Instance.BaseEndpoint; private static readonly string UrlBaseDefault = GlobalSetting.Instance.BaseEndpoint;
#endregion #endregion

View File

@ -1,4 +1,4 @@
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,9 +1,10 @@
using eShopOnContainers.Core.Helpers; using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Core.ViewModels; using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Core.Views; using eShopOnContainers.Core.Views;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System; using System;
using System.Collections.Generic; using System.Globalization;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xamarin.Forms; using Xamarin.Forms;
@ -11,9 +12,7 @@ namespace eShopOnContainers.Services
{ {
public class NavigationService : INavigationService public class NavigationService : INavigationService
{ {
protected readonly Dictionary<Type, Type> _mappings; protected Application CurrentApplication
protected Application CurrentApplication
{ {
get get
{ {
@ -21,13 +20,6 @@ namespace eShopOnContainers.Services
} }
} }
public NavigationService()
{
_mappings = new Dictionary<Type, Type>();
CreatePageViewModelMappings();
}
public Task InitializeAsync() public Task InitializeAsync()
{ {
if(string.IsNullOrEmpty(Settings.AuthAccessToken)) if(string.IsNullOrEmpty(Settings.AuthAccessToken))
@ -100,7 +92,7 @@ namespace eShopOnContainers.Services
protected virtual async Task InternalNavigateToAsync(Type viewModelType, object parameter) protected virtual async Task InternalNavigateToAsync(Type viewModelType, object parameter)
{ {
Page page = CreateAndBindPage(viewModelType, parameter); Page page = CreatePage(viewModelType, parameter);
if (page is LoginView) if (page is LoginView)
{ {
@ -109,7 +101,6 @@ namespace eShopOnContainers.Services
else else
{ {
var navigationPage = CurrentApplication.MainPage as CustomNavigationView; var navigationPage = CurrentApplication.MainPage as CustomNavigationView;
if (navigationPage != null) if (navigationPage != null)
{ {
await navigationPage.PushAsync(page); await navigationPage.PushAsync(page);
@ -125,40 +116,23 @@ namespace eShopOnContainers.Services
protected Type GetPageTypeForViewModel(Type viewModelType) protected Type GetPageTypeForViewModel(Type viewModelType)
{ {
if (!_mappings.ContainsKey(viewModelType)) var viewName = viewModelType.FullName.Replace("Model", string.Empty);
{ var viewModelAssemblyName = viewModelType.GetTypeInfo().Assembly.FullName;
throw new KeyNotFoundException($"No map for ${viewModelType} was found on navigation mappings"); var viewAssemblyName = string.Format(CultureInfo.InvariantCulture, "{0}, {1}", viewName, viewModelAssemblyName);
} var viewType = Type.GetType(viewAssemblyName);
return viewType;
return _mappings[viewModelType];
} }
protected Page CreateAndBindPage(Type viewModelType, object parameter) protected Page CreatePage(Type viewModelType, object parameter)
{ {
Type pageType = GetPageTypeForViewModel(viewModelType); Type pageType = GetPageTypeForViewModel(viewModelType);
if (pageType == null) if (pageType == null)
{ {
throw new Exception($"Mapping type for {viewModelType} is not a page"); throw new Exception($"Cannot locate page type for {viewModelType}");
} }
Page page = Activator.CreateInstance(pageType) as Page; Page page = Activator.CreateInstance(pageType) as Page;
ViewModelBase viewModel = ViewModelLocator.Instance.Resolve(viewModelType) as ViewModelBase;
page.BindingContext = viewModel;
return page; return page;
} }
private void CreatePageViewModelMappings()
{
_mappings.Add(typeof(BasketViewModel), typeof(BasketView));
_mappings.Add(typeof(CatalogViewModel), typeof(CatalogView));
_mappings.Add(typeof(CheckoutViewModel), typeof(CheckoutView));
_mappings.Add(typeof(LoginViewModel), typeof(LoginView));
_mappings.Add(typeof(MainViewModel), typeof(MainView));
_mappings.Add(typeof(OrderDetailViewModel), typeof(OrderDetailView));
_mappings.Add(typeof(ProfileViewModel), typeof(ProfileView));
_mappings.Add(typeof(SettingsViewModel), typeof(SettingsView));
}
} }
} }

View File

@ -1,4 +1,4 @@
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;

View File

@ -3,7 +3,7 @@ using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using Xamarin.Forms; using Xamarin.Forms;
namespace eShopOnContainers.ViewModels.Base namespace eShopOnContainers.Core.ViewModels.Base
{ {
public abstract class ExtendedBindableObject : BindableObject public abstract class ExtendedBindableObject : BindableObject
{ {

View File

@ -1,9 +1,8 @@
using eShopOnContainers.Core; using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Services; using eShopOnContainers.Services;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace eShopOnContainers.ViewModels.Base namespace eShopOnContainers.Core.ViewModels.Base
{ {
public abstract class ViewModelBase : ExtendedBindableObject public abstract class ViewModelBase : ExtendedBindableObject
{ {
@ -28,14 +27,14 @@ namespace eShopOnContainers.ViewModels.Base
public ViewModelBase() public ViewModelBase()
{ {
DialogService = ViewModelLocator.Instance.Resolve<IDialogService>(); DialogService = ViewModelLocator.Resolve<IDialogService>();
NavigationService = ViewModelLocator.Instance.Resolve<INavigationService>(); NavigationService = ViewModelLocator.Resolve<INavigationService>();
GlobalSetting.Instance.BaseEndpoint = Settings.UrlBase; GlobalSetting.Instance.BaseEndpoint = Settings.UrlBase;
} }
public virtual Task InitializeAsync(object navigationData) public virtual Task InitializeAsync(object navigationData)
{ {
return Task.FromResult(false); return Task.FromResult(false);
} }
} }
} }

View File

@ -1,7 +1,8 @@
using Microsoft.Practices.Unity; using Microsoft.Practices.Unity;
using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Services; using eShopOnContainers.Services;
using System; using System;
using System.Globalization;
using System.Reflection;
using eShopOnContainers.Core.Services.Catalog; using eShopOnContainers.Core.Services.Catalog;
using eShopOnContainers.Core.Services.OpenUrl; using eShopOnContainers.Core.Services.OpenUrl;
using eShopOnContainers.Core.Services.RequestProvider; using eShopOnContainers.Core.Services.RequestProvider;
@ -9,54 +10,53 @@ using eShopOnContainers.Core.Services.Basket;
using eShopOnContainers.Core.Services.Identity; using eShopOnContainers.Core.Services.Identity;
using eShopOnContainers.Core.Services.Order; using eShopOnContainers.Core.Services.Order;
using eShopOnContainers.Core.Services.User; using eShopOnContainers.Core.Services.User;
using Xamarin.Forms;
namespace eShopOnContainers.ViewModels.Base namespace eShopOnContainers.Core.ViewModels.Base
{ {
public class ViewModelLocator public static class ViewModelLocator
{ {
private bool _useMockService; private static readonly IUnityContainer _unityContainer = new UnityContainer();
private readonly IUnityContainer _unityContainer;
private static readonly ViewModelLocator _instance = new ViewModelLocator(); public static readonly BindableProperty AutoWireViewModelProperty =
BindableProperty.CreateAttached("AutoWireViewModel", typeof(bool), typeof(ViewModelLocator), default(bool), propertyChanged: OnAutoWireViewModelChanged);
public static ViewModelLocator Instance public static bool GetAutoWireViewModel(BindableObject bindable)
{ {
get { return _instance; } return (bool)bindable.GetValue(ViewModelLocator.AutoWireViewModelProperty);
} }
public bool UseMockService public static void SetAutoWireViewModel(BindableObject bindable, bool value)
{ {
get { return _useMockService; } bindable.SetValue(ViewModelLocator.AutoWireViewModelProperty, value);
set { _useMockService = value; ; } }
}
protected ViewModelLocator() public static bool UseMockService { get; set; }
{
_unityContainer = new UnityContainer();
// Services public static void Initialize()
_unityContainer.RegisterType<IDialogService, DialogService>(); {
RegisterSingleton<INavigationService, NavigationService>(); // Services
_unityContainer.RegisterType<IOpenUrlService, OpenUrlService>(); _unityContainer.RegisterType<IDialogService, DialogService>();
_unityContainer.RegisterType<IRequestProvider, RequestProvider>(); _unityContainer.RegisterType<INavigationService, NavigationService>(new ContainerControlledLifetimeManager());
_unityContainer.RegisterType<IIdentityService, IdentityService>(); _unityContainer.RegisterType<IOpenUrlService, OpenUrlService>();
_unityContainer.RegisterType<IRequestProvider, RequestProvider>();
_unityContainer.RegisterType<IIdentityService, IdentityService>();
_unityContainer.RegisterType<ICatalogService, CatalogMockService>();
_unityContainer.RegisterType<IBasketService, BasketMockService>();
_unityContainer.RegisterType<IUserService, UserMockService>();
_unityContainer.RegisterType<ICatalogService, CatalogMockService>(); // View models
_unityContainer.RegisterType<IBasketService, BasketMockService>(); _unityContainer.RegisterType<BasketViewModel>();
_unityContainer.RegisterType<IUserService, UserMockService>(); _unityContainer.RegisterType<CatalogViewModel>();
_unityContainer.RegisterType<CheckoutViewModel>();
_unityContainer.RegisterType<LoginViewModel>();
_unityContainer.RegisterType<MainViewModel>();
_unityContainer.RegisterType<OrderDetailViewModel>();
_unityContainer.RegisterType<ProfileViewModel>();
_unityContainer.RegisterType<SettingsViewModel>();
}
// View models public static void UpdateDependencies(bool useMockServices)
_unityContainer.RegisterType<BasketViewModel>();
_unityContainer.RegisterType<CatalogViewModel>();
_unityContainer.RegisterType<CheckoutViewModel>();
_unityContainer.RegisterType<LoginViewModel>();
_unityContainer.RegisterType<MainViewModel>();
_unityContainer.RegisterType<OrderDetailViewModel>();
_unityContainer.RegisterType<ProfileViewModel>();
_unityContainer.RegisterType<SettingsViewModel>();
}
public void UpdateDependencies(bool useMockServices)
{ {
// Change injected dependencies // Change injected dependencies
if (useMockServices) if (useMockServices)
@ -80,29 +80,31 @@ namespace eShopOnContainers.ViewModels.Base
} }
} }
public T Resolve<T>() public static T Resolve<T>()
{ {
return _unityContainer.Resolve<T>(); return _unityContainer.Resolve<T>();
} }
public object Resolve(Type type) private static void OnAutoWireViewModelChanged(BindableObject bindable, object oldValue, object newValue)
{ {
return _unityContainer.Resolve(type); var view = bindable as Element;
} if (view == null)
{
return;
}
public void Register<T>(T instance) var viewType = view.GetType();
{ var viewName = viewType.FullName.Replace(".Views.", ".ViewModels.");
_unityContainer.RegisterInstance<T>(instance); var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
} var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}Model, {1}", viewName, viewAssemblyName);
public void Register<TInterface, T>() where T : TInterface var viewModelType = Type.GetType(viewModelName);
{ if (viewModelType == null)
_unityContainer.RegisterType<TInterface, T>(); {
} return;
}
public void RegisterSingleton<TInterface, T>() where T : TInterface var viewModel = _unityContainer.Resolve(viewModelType);
{ view.BindingContext = viewModel;
_unityContainer.RegisterType<TInterface, T>(new ContainerControlledLifetimeManager()); }
}
} }
} }

View File

@ -4,8 +4,6 @@ using eShopOnContainers.Core.Models.Catalog;
using eShopOnContainers.Core.Services.Basket; using eShopOnContainers.Core.Services.Basket;
using eShopOnContainers.Core.Services.User; using eShopOnContainers.Core.Services.User;
using eShopOnContainers.Core.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.ViewModels.Base;
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,14 +1,11 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using Xamarin.Forms; using Xamarin.Forms;
using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Models.Catalog; using eShopOnContainers.Core.Models.Catalog;
using eShopOnContainers.Core.Services.Catalog; using eShopOnContainers.Core.Services.Catalog;
using System.Windows.Input; using System.Windows.Input;
using System.Linq;
using eShopOnContainers.Core.Services.Basket; using eShopOnContainers.Core.Services.Basket;
using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Core.Services.User; using eShopOnContainers.Core.Services.User;
namespace eShopOnContainers.Core.ViewModels namespace eShopOnContainers.Core.ViewModels

View File

@ -1,5 +1,5 @@
using eShopOnContainers.Core.Models.Navigation; using eShopOnContainers.Core.Models.Navigation;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Windows.Input; using System.Windows.Input;
using Xamarin.Forms; using Xamarin.Forms;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -138,7 +138,7 @@ namespace eShopOnContainers.Core.ViewModels
await _basketService.ClearBasketAsync(_shippingAddress.Id.ToString(), authToken); await _basketService.ClearBasketAsync(_shippingAddress.Id.ToString(), authToken);
// Reset Basket badge // Reset Basket badge
var basketViewModel = ViewModelLocator.Instance.Resolve<BasketViewModel>(); var basketViewModel = ViewModelLocator.Resolve<BasketViewModel>();
basketViewModel.BadgeCount = 0; basketViewModel.BadgeCount = 0;
// Navigate to Orders // Navigate to Orders

View File

@ -4,7 +4,7 @@ using eShopOnContainers.Core.Services.Identity;
using eShopOnContainers.Core.Services.OpenUrl; using eShopOnContainers.Core.Services.OpenUrl;
using eShopOnContainers.Core.Services.User; using eShopOnContainers.Core.Services.User;
using eShopOnContainers.Core.Validations; using eShopOnContainers.Core.Validations;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using IdentityModel.Client; using IdentityModel.Client;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
@ -133,10 +133,9 @@ namespace eShopOnContainers.Core.ViewModels
public override Task InitializeAsync(object navigationData) public override Task InitializeAsync(object navigationData)
{ {
if(navigationData is LogoutParameter) if (navigationData is LogoutParameter)
{ {
var logoutParameter = (LogoutParameter)navigationData; var logoutParameter = (LogoutParameter)navigationData;
if (logoutParameter.Logout) if (logoutParameter.Logout)
{ {
Logout(); Logout();

View File

@ -1,5 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Models.Navigation; using eShopOnContainers.Core.Models.Navigation;
using Xamarin.Forms; using Xamarin.Forms;
using eShopOnContainers.Core.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;

View File

@ -1,6 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using eShopOnContainers.Core.Models.Orders; using eShopOnContainers.Core.Models.Orders;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Services.Catalog; using eShopOnContainers.Core.Services.Catalog;
using eShopOnContainers.Core.Services.Basket; using eShopOnContainers.Core.Services.Basket;
using eShopOnContainers.Core.Services.Order; using eShopOnContainers.Core.Services.Order;

View File

@ -3,7 +3,7 @@ using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Core.Models.Orders; using eShopOnContainers.Core.Models.Orders;
using eShopOnContainers.Core.Models.User; using eShopOnContainers.Core.Models.User;
using eShopOnContainers.Core.Services.Order; using eShopOnContainers.Core.Services.Order;
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;

View File

@ -1,4 +1,4 @@
using eShopOnContainers.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using System.Windows.Input; using System.Windows.Input;
using Xamarin.Forms; using Xamarin.Forms;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -71,7 +71,7 @@ namespace eShopOnContainers.Core.ViewModels
private void MockServices() private void MockServices()
{ {
ViewModelLocator.Instance.UpdateDependencies(!UseAzureServices); ViewModelLocator.UpdateDependencies(!UseAzureServices);
UpdateInfo(); UpdateInfo();
} }

View File

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.BasketView" x:Class="eShopOnContainers.Core.Views.BasketView"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core" xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core" xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="Cart"> Title="Cart">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.CatalogView" x:Class="eShopOnContainers.Core.Views.CatalogView"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core" xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core"
xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core" xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core" xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core"
xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core" xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core"
xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core" xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="Catalog"> Title="Catalog">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -47,7 +47,8 @@ namespace eShopOnContainers.Core.Views
{ {
base.OnBindingContextChanged(); base.OnBindingContextChanged();
_filterView.BindingContext = BindingContext; if (_filterView != null)
_filterView.BindingContext = BindingContext;
} }
private void OnFilterChanged(object sender, EventArgs e) private void OnFilterChanged(object sender, EventArgs e)

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.CheckoutView" x:Class="eShopOnContainers.Core.Views.CheckoutView"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core" xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="Checkout"> Title="Checkout">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -2,9 +2,11 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.LoginView" x:Class="eShopOnContainers.Core.Views.LoginView"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core" xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core"
xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core" xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core"
xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"> xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true">
<ContentPage.Title> <ContentPage.Title>
<OnPlatform <OnPlatform
x:TypeArguments="x:String" x:TypeArguments="x:String"

View File

@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.MainView" x:Class="eShopOnContainers.Core.Views.MainView"
xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core" xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:controls="clr-namespace:eShopOnContainers.Core.Controls;assembly=eShopOnContainers.Core" xmlns:controls="clr-namespace:eShopOnContainers.Core.Controls;assembly=eShopOnContainers.Core"
BarBackgroundColor="{StaticResource DarkGreenColor}" BarBackgroundColor="{StaticResource DarkGreenColor}"
BackgroundColor="{StaticResource BackgroundColor}" BackgroundColor="{StaticResource BackgroundColor}"
BarTextColor="{StaticResource WhiteColor}"> BarTextColor="{StaticResource WhiteColor}"
viewModelBase:ViewModelLocator.AutoWireViewModel="true">
<TabbedPage.Title> <TabbedPage.Title>
<OnPlatform <OnPlatform
x:TypeArguments="x:String" x:TypeArguments="x:String"

View File

@ -1,6 +1,5 @@
using eShopOnContainers.Core.ViewModels; using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Core.ViewModels.Base; using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.ViewModels.Base;
using Xamarin.Forms; using Xamarin.Forms;
namespace eShopOnContainers.Core.Views namespace eShopOnContainers.Core.Views
@ -32,17 +31,9 @@ namespace eShopOnContainers.Core.Views
} }
}); });
var homeViewModel = ViewModelLocator.Instance.Resolve<CatalogViewModel>(); await ((CatalogViewModel)HomeView.BindingContext).InitializeAsync(null);
await homeViewModel.InitializeAsync(null); await ((BasketViewModel)BasketView.BindingContext).InitializeAsync(null);
HomeView.BindingContext = homeViewModel; await ((ProfileViewModel)ProfileView.BindingContext).InitializeAsync(null);
var basketViewModel = ViewModelLocator.Instance.Resolve<BasketViewModel>();
await basketViewModel.InitializeAsync(null);
BasketView.BindingContext = basketViewModel;
var profileViewModel = ViewModelLocator.Instance.Resolve<ProfileViewModel>();
await profileViewModel.InitializeAsync(null);
ProfileView.BindingContext = profileViewModel;
} }
} }
} }

View File

@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.OrderDetailView" x:Class="eShopOnContainers.Core.Views.OrderDetailView"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core" xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core"
xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core" xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core"
xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core" xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="{Binding Order.OrderNumber}"> Title="{Binding Order.OrderNumber}">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.ProfileView" x:Class="eShopOnContainers.Core.Views.ProfileView"
xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core" xmlns:views="clr-namespace:eShopOnContainers.Core.Views;assembly=eShopOnContainers.Core"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core" xmlns:templates="clr-namespace:eShopOnContainers.Core.Views.Templates;assembly=eShopOnContainers.Core"
xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core" xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"
Title="My profile"> viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="My profile">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eShopOnContainers.Core.Views.SettingsView" x:Class="eShopOnContainers.Core.Views.SettingsView"
xmlns:viewModelBase="clr-namespace:eShopOnContainers.Core.ViewModels.Base;assembly=eShopOnContainers.Core"
xmlns:controls="clr-namespace:eShopOnContainers.Core.Controls;assembly=eShopOnContainers.Core" xmlns:controls="clr-namespace:eShopOnContainers.Core.Controls;assembly=eShopOnContainers.Core"
xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core" xmlns:animations="clr-namespace:eShopOnContainers.Core.Animations;assembly=eShopOnContainers.Core"
xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core" xmlns:triggers="clr-namespace:eShopOnContainers.Core.Triggers;assembly=eShopOnContainers.Core"
xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core" xmlns:behaviors="clr-namespace:eShopOnContainers.Core.Behaviors;assembly=eShopOnContainers.Core"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
Title="Settings"> Title="Settings">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <ResourceDictionary>