Petit Probleme MVVM SIlverlight
Bonjour à tous!
J'ai repris un pseudo modèle MVVM C# que j'ai trouvé sur le net et passé tant bien que mal en VB (On ne se moque pas), j'arrive à exposer ma Collection à l'UI , mais rien en se passe
lorsque j'ajoute une element à celle ci, voici le code:
Ma classe PLACE
Code:
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
| Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class Place
Private m_Titre As String
Private m_Description As String
Public Property Titre() As String
Get
Return m_Titre
End Get
Set(ByVal value As String)
m_Titre = value
End Set
End Property
Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
Public Sub New(ByVal Titre As String, ByVal Description As String)
m_Titre = Titre
m_Description = Description
End Sub
End Class |
Ma classe ViewModelBase
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Imports System.ComponentModel
Public Class ViewModelBase(Of T As ViewModelBase(Of T))
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
End Class |
Ma classe PlacesService
Code:
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
| Public Class PlacesService
Public Function GetAll() As IEnumerable(Of Place)
Dim Places As New List(Of Place)
Places.Add(New Place("Test1", "Description "))
Places.Add(New Place("Test 2", "Description 2"))
Return Places
End Function
End Class |
Ma classe PlacesViewModel
Code:
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| Imports System.Collections.ObjectModel
Public Class PlacesViewModel
Inherits ViewModelBase(Of PlacesViewModel)
Private ReadOnly PlaceService As New PlacesService()
Public Sub New()
AvailablePlaces = New ObservableCollection(Of Place)()
Dim recipes = PlaceService.GetAll()
AvailablePlaces.AddRange(recipes)
End Sub
Private _AvailablePlaces As ObservableCollection(Of Place)
Public Property AvailablePlaces() As ObservableCollection(Of Place)
Get
Return _AvailablePlaces
End Get
Set(ByVal value As ObservableCollection(Of Place))
_AvailablePlaces = value
End Set
End Property
End Class
Public Module ObservableCollectionsExtensions
Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub AddRange(Of T)(ByVal collection As ObservableCollection(Of T), ByVal items As IEnumerable(Of T))
If collection Is Nothing Then
Throw New ArgumentNullException("collection")
End If
If items Is Nothing Then
Throw New ArgumentNullException("items")
End If
For Each item In items
collection.Add(item)
Next
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub Replace(Of T)(ByVal collection As ObservableCollection(Of T), ByVal items As IEnumerable(Of T))
If collection Is Nothing Then
Throw New ArgumentNullException("collection")
End If
If items Is Nothing Then
Throw New ArgumentNullException("items")
End If
collection.Clear()
collection.AddRange(items)
End Sub
End Module |
Dans mon App.xaml
Code:
<local:PlacesViewModel x:Key="ViewModelDataSource2"></local:PlacesViewModel>
L'affichage des données se passe bien, cependant, il ne se passe rien lorsque j'ajoute un objet à ma collection. Je pense qu'il me manque
quelque chose mais là j'ai du mal à cerner. Quelqu'un aurait-il une idée? Merci