IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

application avec MVVM


Sujet :

C#

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Septembre 2015
    Messages
    107
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Septembre 2015
    Messages : 107
    Par défaut application avec MVVM
    Bonsoir tous le monde, je cherche a développe une simple application pour comprendre l'architecture MVVM avec WPF. je veux juste insèrér un texte saisie dans un champ de text saisie sur une listview lors de press sur le bouton ajouter.

    je créer deux dossier qui sont Modèle et ViewModel et voila le code source insèrer pour chaque fichier sachant que j'utilise l'extension MVVMLightToolkit.

    pour le dossier Modèle:
    Article.cs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace GestionArticle.Model
    {
      public class Article
        {
            private string _nom;
     
            public string Nom
            {
                get { return _nom; }
                set { _nom = value; }
            }
        }
    }
    et pour le dossier ViewModel :
    IMainViewModel.cs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;
    using GestionArticle.Model;
     
    namespace GestionArticle.ViewModel
    {
      public  interface IMainViewModel
        {
            string Titre { get; set; }
            ObservableCollection<Article> Articles { get; }
            ICommand ChargerArticleCommand { get; }
        }
    }
    MainViewModel.cs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    using GalaSoft.MvvmLight;
    using System.Collections.ObjectModel;
    using GestionArticle.Model;
    using GalaSoft.MvvmLight.Command;
    using System.Windows.Input;
    namespace GestionArticle.ViewModel
    {
        /// <summary>
        /// This class contains properties that a View can data bind to.
        /// <para>
        /// See http://www.galasoft.ch/mvvm
        /// </para>
        /// </summary>
        public class MainViewModel : ViewModelBase, IMainViewModel
        {
            /// <summary>
            /// Initializes a new instance of the MainViewModel class.
            /// </summary>
     
            private readonly ObservableCollection<Article> article;
            public MainViewModel()
            {
     
                article = new ObservableCollection<Article>();
                article.Add(new Article { Nom = "article 1" });
                ChargerArticleCommand = new RelayCommand(ChargerArticles);
            }
     
            private void ChargerArticles()
            {
                this.article.Add(new Article { Nom = "Article 2" });
            }
     
            private string _titre;
            public string Titre
            {
                get { return _titre; }
                set
                {
                    _titre = value;
                    RaisePropertyChanged("Titre");
                }
            }
     
            public ObservableCollection<Article> Articles
            {
                get { return this.article; }
            }
     
            public ICommand ChargerArticleCommand
            {
                get;
                private set;
            }
        }
    }
    ViewModelLocator.cs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    /*
      In App.xaml:
      <Application.Resources>
          <vm:ViewModelLocator xmlns:vm="clr-namespace:GestionArticle.ViewModel"
                                       x:Key="Locator" />
      </Application.Resources>
      
      In the View:
      DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    */
     
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Ioc;
    using Microsoft.Practices.ServiceLocation;
     
    namespace GestionArticle.ViewModel
    {
        /// <summary>
        /// This class contains static references to all the view models in the
        /// application and provides an entry point for the bindings.
        /// <para>
        /// See http://www.galasoft.ch/mvvm
        /// </para>
        /// </summary>
        public class ViewModelLocator
        {
            static ViewModelLocator()
            {
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
     
                SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();
            }
            public static IMainViewModel MainVM
            {
                get { return ServiceLocator.Current.GetInstance<IMainViewModel>(); }
            }
     
     
            /// <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 ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }*/
            public static void CleanMain()
            {
                SimpleIoc.Default.Unregister<IMainViewModel>();
                SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();
            }
            public static void Cleanup()
            {
                CleanMain();
            }
        }
    }
    App.xaml

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <Application x:Class="GestionArticle.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:viewModel="clr-namespace:GestionArticle.ViewModel"
                 StartupUri="MainWindow.xaml"
                 mc:Ignorable="d">
     
        <Application.Resources>
            <!--Global View Model Locator-->
            <viewModel:ViewModelLocator x:Key="Locator"
                                 d:IsDataSource="True" />
        </Application.Resources>
     
    </Application>
    MainWindow.xaml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <Window x:Class="GestionArticle.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:ignore="http://www.ignore.com"
            mc:Ignorable="d ignore"
            Height="410"
            Width="613"
            Title="MVVM Light Application"
     
            DataContext="{Binding MainVM, Source={StaticResource Locator}}">
     
        <!-- <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Skins/MainSkin.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>-->
     
        <Grid x:Name="LayoutRoot">
     
            <TextBlock FontSize="36"
                       FontWeight="Bold"
                       Foreground="#FFF3ACF3"
                       Text="{Binding WelcomeTitle}"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       TextWrapping="Wrap" Margin="317.5,138,76.5,194" Width="211" Background="#FF7FE6FD" />
            <ListView HorizontalAlignment="Left" Height="225" Margin="59,31,0,0" VerticalAlignment="Top" Width="185" ItemsSource="{Binding Articles, Mode=OneWay}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Content="Ajouter" HorizontalAlignment="Left" Height="40" Margin="308,292,0,0" VerticalAlignment="Top" Width="162" Command="{Binding ChargerArticleCommand, Mode=OneWay}"/>
     
        </Grid>
    </Window>
    lorsque j’exécute je ne trouve pas l'objet Article insérer par défaut:
    Nom : listeview.JPG
Affichages : 432
Taille : 26,1 Ko

    je trouve pourquoi il m'affiche dans le premier ligne de listview GestionArticle.Model.Article? et comment je peux remédier ce problème?

    remarque:Tous le projet est attaché en pièces jointes.

    Merci
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. Réponses: 7
    Dernier message: 16/01/2015, 18h59
  2. [Kylix] Portage application avec les compos Apros
    Par cedricgirard dans le forum EDI
    Réponses: 1
    Dernier message: 21/10/2004, 16h40
  3. Réponses: 10
    Dernier message: 30/06/2004, 13h00
  4. Conseils pour developper une application avec Oracle
    Par belugha dans le forum Langages de programmation
    Réponses: 5
    Dernier message: 02/06/2003, 16h03
  5. [VB6]Fermer une application avec VB
    Par Mylou dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 04/04/2003, 21h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo