mirror of
https://github.com/farcasclaudiu/myfriendsaround.git
synced 2026-06-29 13:02:05 +03:00
IoC and NavigationService implementation
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Device.Location;
|
||||
using System.Net;
|
||||
using System.ServiceModel.Channels;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using GalaSoft.MvvmLight.Threading;
|
||||
using Hammock;
|
||||
using Hammock.Serialization;
|
||||
using Microsoft.Phone.Controls;
|
||||
using Microsoft.Silverlight.Testing;
|
||||
using MyFriendsAround.Common.Entities;
|
||||
using MyFriendsAround.WP7.Service;
|
||||
using MyFriendsAround.WP7.Utils;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MyFriendsAround.WP7.ViewModel
|
||||
{
|
||||
public class AboutViewModel : ViewModelBase
|
||||
{
|
||||
public string ApplicationTitle
|
||||
{
|
||||
get
|
||||
{
|
||||
return "MyFriendsAround";
|
||||
}
|
||||
}
|
||||
|
||||
public string PageName
|
||||
{
|
||||
get
|
||||
{
|
||||
return "About";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the MainViewModel class.
|
||||
/// </summary>
|
||||
public AboutViewModel()
|
||||
{
|
||||
if (IsInDesignMode)
|
||||
{
|
||||
// Code runs in Blend --> create design time data.
|
||||
}
|
||||
else
|
||||
{
|
||||
// Code runs "for real"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public string AppBarTextAbout {
|
||||
get { return "About"; }
|
||||
}
|
||||
|
||||
public string AppBarTextPublish
|
||||
{
|
||||
get { return "Publish"; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Ink;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Shapes;
|
||||
using MicroIoc;
|
||||
|
||||
namespace MyFriendsAround.WP7.ViewModel
|
||||
{
|
||||
public sealed class Container
|
||||
{
|
||||
Container()
|
||||
{
|
||||
}
|
||||
|
||||
public static IMicroIocContainer Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return Nested.instance;
|
||||
}
|
||||
}
|
||||
|
||||
class Nested
|
||||
{
|
||||
// Explicit static constructor to tell C# compiler
|
||||
// not to mark type as before field init
|
||||
static Nested()
|
||||
{
|
||||
}
|
||||
internal static readonly IMicroIocContainer instance =
|
||||
new MicroIocContainer();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,14 +51,6 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
public string Welcome
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Welcome to MVVM Light";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the MainViewModel class.
|
||||
/// </summary>
|
||||
@@ -68,6 +60,7 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
PublishLocationCommand = new RelayCommand(() => PublishLocationAction());
|
||||
DisplayAboutCommand = new RelayCommand(() => DisplayAbout());
|
||||
InputBoxCommand = new RelayCommand(() => InputBox());
|
||||
NavigateToAboutCommand = new RelayCommand(() => NavigateToAbout());
|
||||
|
||||
if (IsInDesignMode)
|
||||
{
|
||||
@@ -82,6 +75,12 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
|
||||
}
|
||||
|
||||
private void NavigateToAbout()
|
||||
{
|
||||
//
|
||||
this.PageNav.NavigateTo(new Uri("/Views/AboutPage.xaml", UriKind.Relative));
|
||||
}
|
||||
|
||||
private void InputBox()
|
||||
{
|
||||
MessageBox.Show("Input box");
|
||||
@@ -102,7 +101,7 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
//f.LocationStr
|
||||
result.Add(new PushPinModel()
|
||||
{
|
||||
PinSource = "ApplicationIcon.png",
|
||||
PinSource = "/ApplicationIcon.png",
|
||||
Location = new GeoCoordinate(f.Latitude, f.Longitude)
|
||||
});
|
||||
});
|
||||
@@ -161,10 +160,17 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
public RelayCommand PublishLocationCommand { get; set; }
|
||||
public RelayCommand DisplayAboutCommand { get; set; }
|
||||
public RelayCommand InputBoxCommand { get; set; }
|
||||
|
||||
public ICommand PublishLocationCommand { get; set; }
|
||||
public ICommand DisplayAboutCommand { get; set; }
|
||||
public ICommand InputBoxCommand { get; set; }
|
||||
|
||||
public ICommand NavigateToAboutCommand
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
|
||||
public string MyName { get; set; }
|
||||
|
||||
public string AppBarTextAbout {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Ink;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Shapes;
|
||||
using MyFriendsAround.WP7.Helpers.Navigation;
|
||||
|
||||
namespace MyFriendsAround.WP7.ViewModel
|
||||
{
|
||||
public class ViewModelBase : GalaSoft.MvvmLight.ViewModelBase
|
||||
{
|
||||
private object context;
|
||||
public object Context
|
||||
{
|
||||
get { return context; }
|
||||
set
|
||||
{
|
||||
if (context == value)
|
||||
return;
|
||||
context = value;
|
||||
RaisePropertyChanged("Context");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets PageNavigation from Container
|
||||
/// </summary>
|
||||
public IPageNavigation PageNav
|
||||
{
|
||||
get
|
||||
{
|
||||
IPageNavigation pageNav =
|
||||
(IPageNavigation)Container.Instance.Resolve(typeof(PageNavigation), "PageNavigation");
|
||||
return pageNav;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
|
||||
*/
|
||||
|
||||
using MyFriendsAround.WP7.Helpers.Navigation;
|
||||
namespace MyFriendsAround.WP7.ViewModel
|
||||
{
|
||||
/// <summary>
|
||||
@@ -55,81 +56,79 @@ namespace MyFriendsAround.WP7.ViewModel
|
||||
/// </summary>
|
||||
public class ViewModelLocator
|
||||
{
|
||||
private static MainViewModel _main;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ViewModelLocator class.
|
||||
/// </summary>
|
||||
public ViewModelLocator()
|
||||
{
|
||||
////if (ViewModelBase.IsInDesignModeStatic)
|
||||
////{
|
||||
//// // Create design time view models
|
||||
////}
|
||||
////else
|
||||
////{
|
||||
//// // Create run time view models
|
||||
////}
|
||||
|
||||
CreateMain();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Main property.
|
||||
/// </summary>
|
||||
public static MainViewModel MainStatic
|
||||
{
|
||||
get
|
||||
if (ViewModelBase.IsInDesignModeStatic)
|
||||
{
|
||||
if (_main == null)
|
||||
{
|
||||
CreateMain();
|
||||
}
|
||||
|
||||
return _main;
|
||||
// Create design time view models
|
||||
}
|
||||
else
|
||||
{
|
||||
//Register PageNavigation - only if not design time
|
||||
Container.Instance.RegisterInstance(typeof(PageNavigation), "PageNavigation");
|
||||
|
||||
// Create run time view models
|
||||
}
|
||||
|
||||
Container.Instance.RegisterInstance(typeof(MainViewModel), "MainViewModel");
|
||||
Container.Instance.RegisterInstance(typeof(AboutViewModel), "AboutViewModel");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Main property.
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
|
||||
"CA1822:MarkMembersAsStatic",
|
||||
Justification = "This non-static member is needed for data binding purposes.")]
|
||||
public MainViewModel Main
|
||||
{
|
||||
get
|
||||
{
|
||||
return MainStatic;
|
||||
MainViewModel mainViewModel = GetViewModel<MainViewModel>();
|
||||
return mainViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a deterministic way to delete the Main property.
|
||||
/// Gets the About property.
|
||||
/// </summary>
|
||||
public static void ClearMain()
|
||||
public AboutViewModel About
|
||||
{
|
||||
_main.Cleanup();
|
||||
_main = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a deterministic way to create the Main property.
|
||||
/// </summary>
|
||||
public static void CreateMain()
|
||||
{
|
||||
if (_main == null)
|
||||
get
|
||||
{
|
||||
_main = new MainViewModel();
|
||||
AboutViewModel aboutViewModel = GetViewModel<AboutViewModel>();
|
||||
return aboutViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up all the resources.
|
||||
/// </summary>
|
||||
public static void Cleanup()
|
||||
public void Cleanup()
|
||||
{
|
||||
ClearMain();
|
||||
MainViewModel mainViewModel = GetViewModel<MainViewModel>();
|
||||
mainViewModel.Cleanup();
|
||||
AboutViewModel aboutViewModel = GetViewModel<AboutViewModel>();
|
||||
aboutViewModel.Cleanup();
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region Local Helpers
|
||||
|
||||
private T GetViewModel<T>() where T : ViewModelBase
|
||||
{
|
||||
// Create a new view model
|
||||
T vm = Container.Instance.Resolve<T>();
|
||||
|
||||
//Assign the Context from PageNavigation to Context property of the ViewModelBase
|
||||
vm.Context = vm.PageNav.CurrentContext;
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user