2010-03-24 3 views
1

J'ai le code suivant (noms d'objets changés, donc syntaxe/erreurs d'orthographe ignorées).Obtenir MVVM ViewModel pour lier à la vue

public class ViewModel 
{ 
    ViewModelSource m_vSource; 

    public ViewModel(IViewModelSource source) 
    { 
     m_vSource= source; 
     m_vSource.ItemArrived += new Action<Item>(m_vSource_ItemArrived); 
    } 

    void m_vSource_ItemArrived(Item obj) 
    { 
     Title = obj.Title; 
     Subitems = obj.items; 
     Description = obj.Description; 
    } 

    public void GetFeed(string serviceUrl) 
    { 
     m_vFeedSource.GetFeed(serviceUrl); 
    } 

    public string Title { get; set; } 
    public IEnumerable<Subitems> Subitems { get; set; } 
    public string Description { get; set; } 
} 

Voici le code que j'ai dans le codebehind de ma page.

ViewModel m_vViewModel; 

public MainPage() 
{ 
    InitializeComponent(); 

    m_vViewModel = new ViewModel(new ViewModelSource()); 
    this.Loaded += new RoutedEventHandler(MainPage_Loaded); 

    this.DataContext = m_vViewModel; 
} 

void MainPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    m_vViewModel.GetItems("http://www.myserviceurl.com"); 
} 

Enfin, voici un exemple de ce à quoi ressemble mon xaml.

<!--TitleGrid is the name of the application and page title--> 
<Grid x:Name="TitleGrid" Grid.Row="0"> 
    <TextBlock Text="My Super Title" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/> 
    <TextBlock Text="{Binding Path=Title}" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/> 
</Grid> 

Y at-il quelque chose que je fais mal ici?

Répondre

1

Je pense que votre ViewModel devrait implémenter l'interface INotifyPropertyChanged:

public virtual event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void RaisePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

Alors votre propriété ressemblerait que:

private title; 
    public string Title 
    { 
     get 
     { 
      return this.title; 
     } 

     set 
     { 
      if (this.title!= value) 
      { 
       this.title= value; 
       this.RaisePropertyChanged("Title"); 
      } 
     } 
    } 

Michael

+0

Eh oui qui est la réponse. Je l'ai compris 10 minutes après que j'ai posté. –

1

Eh bien, allez comprendre, 10 minutes après je l'ai posté, je le découvre.

Il me manquait l'implémentation INotifyProperty. Merci si quelqu'un regarde ça.

Questions connexes