Domanda

Ho il seguente codice (i nomi degli oggetti modificati, quindi gli errori di sintassi / ortografia ignorano).

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; }
 }

Ecco il codice che ho in codebehind della mia pagina.

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");
}

Infine, ecco un esempio di ciò che il mio XAML assomiglia.

<!--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>

C'è qualcosa che sto facendo male qui?

È stato utile?

Soluzione

Credo che la vostra ViewModel dovrebbe implementare l'interfaccia INotifyPropertyChanged:

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

Poi la vostra proprietà sarà simile che:

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

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

Michael

Altri suggerimenti

Bene, vai a capire, 10 minuti dopo ho posto, ho capirlo.

mi mancava l'attuazione inotify proprietà. Grazie se qualcuno sta guardando questo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top