Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

2 mars 2014 7 02 /03 /mars /2014 20:03

1 ViewModelLocator "simple"

Exemple

  public class ViewModelLocator
    {
        public MainViewModel MainViewModel { get; set; }   
        public ArticleListViewModel ArticleListViewModel { get; set; }
        public ArticleDetailsViewModel ArticleDetailsViewModel { get; set; }
 
        public ViewModelLocator()
        {
            MainViewModel = new MainViewModel();
            ArticleListViewModel = new  ArticleListViewModel();
            ArticleDetailsViewModel = new ArticleDetailsViewModel();
        }
    }

Puis dans App.xaml

<Application x:Class="MVVMExample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ViewModels="clr-namespace:MVVMExample.ViewModels"
             xmlns:Framework="clr-namespace:MVVMExample.Framework"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
 
            <!-- etc. -->
            <Framework:ViewModelLocator x:Key="viewModelLocator"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Utilisation

 <Views:ArticleListView DataContext="{Binding ArticleListViewModel, Source={StaticResource viewModelLocator}}" />

 

2-Un ViewModelLocator générique
« Framework » (dossier ou projet)
public class ViewModelLocator
    {
        private readonly static Dictionary<string, ViewModelBase>cache = new Dictionary<string, ViewModelBase>();
 
        public ViewModelBase this[string key]
        {
            get
            {
                ViewModelBase viewModel;
                cache.TryGetValue(key, out viewModel);
                return viewModel;
            }
        }
        public static void Register(string key, ViewModelBase viewModel)
        {
            cache.Add(key, viewModel);
        }
        public static void Register(ViewModelBase viewModel)
        {
            Register(viewModel.GetType().Name, viewModel);
        }      
        public static void Register<TViewModel>(ViewModelBase viewModel)
        {
            Register(typeof(TViewModel).Name, viewModel);
        }      
    }

 

App.xaml.cs
On enregistre les ViewModel
public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
 
            RegisterViewModels();
        }
        private void RegisterViewModels()
        {
            ViewModelLocator.Register(new MainViewModel());
            ViewModelLocator.Register(new ArticleListViewModel());
            ViewModelLocator.Register(new ArticleDetailsViewModel());
        }
    }

 

App.xaml
<Framework:ViewModelLocator x:Key="viewModelLocator"/>

 

Utilisation
<Views:ArticleListView DataContext="{Binding .[ArticleListViewModel], Source={StaticResource viewModelLocator}}" />

 

Partager cet article

Repost 0
Published by Romagny13 - dans MVVM
commenter cet article

commentaires