我有以下代码(更改对象名称,因此忽略语法/拼写错误)。

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

这是我页面CodeBehind中的代码。

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

最后,这是我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>

我这里有什么错吗?

有帮助吗?

解决方案

我认为您的ViewModel应该实现InotifyPropertychanged接口:

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

然后,您的财产看起来像这样:

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

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

迈克尔

其他提示

好吧,我张贴它后10分钟,我弄清楚了。

我缺少InotifyProperty实现。感谢有人在看这个。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top