Created
January 18, 2013 03:02
-
-
Save petarvucetin/4562040 to your computer and use it in GitHub Desktop.
Wire up VM via service locator pattern
This file contains 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
<Application x:Class="Gridlock.App" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:vm="clr-namespace:Gridlock.ViewModel" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
StartupUri="MainWindow.xaml" | |
mc:Ignorable="d"> | |
<Application.Resources> | |
<!--Global View Model Locator--> | |
<vm:ViewModelLocator x:Key="Locator" | |
d:IsDataSource="True" /> | |
</Application.Resources> | |
</Application> |
This file contains 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
<Window.DataContext> | |
<Binding Path="Main" Source="{StaticResource Locator}"/> | |
</Window.DataContext> |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Reflection; | |
using System.Windows; | |
using System.Windows.Input; | |
using AutoMapper; | |
using CsvHelper; | |
using CsvHelper.Configuration; | |
using GalaSoft.MvvmLight; | |
using System.Linq; | |
using GalaSoft.MvvmLight.Command; | |
using Gridlock.Infrasturcture; | |
using Infragistics.Windows.DataPresenter.Events; | |
namespace Gridlock.ViewModel | |
{ | |
/// <summary> | |
/// This class contains properties that the main View can data bind to. | |
/// <para> | |
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel. | |
/// </para> | |
/// <para> | |
/// You can also use Blend to data bind with the tool's support. | |
/// </para> | |
/// <para> | |
/// See http://www.galasoft.ch/mvvm/getstarted | |
/// </para> | |
/// </summary> | |
public class MainViewModel : ViewModelBase | |
{ | |
/// <summary> | |
/// Initializes a new instance of the MainViewModel class. | |
/// </summary> | |
public MainViewModel() | |
{ | |
if (IsInDesignMode) | |
{ | |
// Code runs in Blend --> create design time data. | |
GenerateMockData(); | |
} | |
else | |
{ | |
GenerateFromCSVData(); | |
} | |
} | |
public override void Cleanup() | |
{ | |
// Clean up if needed | |
base.Cleanup(); | |
} | |
} |
This file contains 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
/* | |
In App.xaml: | |
<Application.Resources> | |
<vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Gridlock.ViewModel" | |
x:Key="Locator" /> | |
</Application.Resources> | |
In the View: | |
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" | |
OR (WPF only): | |
xmlns:vm="clr-namespace:Gridlock.ViewModel" | |
DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}" | |
*/ | |
namespace Gridlock.ViewModel | |
{ | |
/// <summary> | |
/// This class contains static references to all the view models in the | |
/// application and provides an entry point for the bindings. | |
/// <para> | |
/// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels | |
/// to this locator. | |
/// </para> | |
/// <para> | |
/// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources: | |
/// </para> | |
/// <code> | |
/// <Application.Resources> | |
/// <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Gridlock.ViewModel" | |
/// x:Key="Locator" /> | |
/// </Application.Resources> | |
/// </code> | |
/// <para> | |
/// Then use: | |
/// </para> | |
/// <code> | |
/// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" | |
/// </code> | |
/// <para> | |
/// You can also use Blend to do all this with the tool's support. | |
/// </para> | |
/// <para> | |
/// See http://www.galasoft.ch/mvvm/getstarted | |
/// </para> | |
/// <para> | |
/// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete | |
/// the Main property and bind to the ViewModelNameStatic property instead: | |
/// </para> | |
/// <code> | |
/// xmlns:vm="clr-namespace:Gridlock.ViewModel" | |
/// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}" | |
/// </code> | |
/// </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 (_main == null) | |
{ | |
CreateMain(); | |
} | |
return _main; | |
} | |
} | |
/// <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; | |
} | |
} | |
/// <summary> | |
/// Provides a deterministic way to delete the Main property. | |
/// </summary> | |
public static void ClearMain() | |
{ | |
_main.Cleanup(); | |
_main = null; | |
} | |
/// <summary> | |
/// Provides a deterministic way to create the Main property. | |
/// </summary> | |
public static void CreateMain() | |
{ | |
if (_main == null) | |
{ | |
_main = new MainViewModel(); | |
} | |
} | |
/// <summary> | |
/// Cleans up all the resources. | |
/// </summary> | |
public static void Cleanup() | |
{ | |
ClearMain(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment