C# mvvm通用应用程序为什么不挂起
共享项目中的我的模型C# mvvm通用应用程序为什么不挂起,c#,xaml,mvvm,win-universal-app,C#,Xaml,Mvvm,Win Universal App,共享项目中的我的模型 class model { public string title; public string link; public string pubDate; public string description; public string imageLink; } 以及共享项目中的我的viewmodel class model { public string title; public string link; public
class model
{
public string title;
public string link;
public string pubDate;
public string description;
public string imageLink;
}
以及共享项目中的我的viewmodel
class model
{
public string title;
public string link;
public string pubDate;
public string description;
public string imageLink;
}
`类viewModel:INotifyPropertyChanged
{
公共可观察集合_posts{get;set;}
public ObservableCollection<model> posts {
get {
return _posts ;
}
set
{
_posts = value;
RaisePropertyChanged("posts");
}
}
public viewModel()
{
posts = new ObservableCollection<model>
{
new model { title="tiltle", description="description", link="link1" },
new model { title="tiltle2", description="description2", link="link1" },
new model { title="tiltle3", description="description3", link="link1" },
new model { title="tiltle4", description="description4", link="link1" },
new model { title="tiltle5", description="description5", link="link1" },
};
}
public void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}`
这是我的第一个问题,我对实现mvvm有问题。不过我对它有很好的理解
当我运行时,我没有看到任何东西一切都很好,除了您应该在
模型中使用带有getter的属性,然后您将看到一些东西
因此:
因为数据绑定使用的是属性而不是字段
旁注:
您不需要命名您的列表框
或从代码隐藏设置其数据上下文
,您已经将视图的数据上下文
设置为您的视图模型
,因此您可以直接绑定到您的项目,例如:
<ListBox ItemsSource="{Binding posts}">
即使你这样做也很管用
另外,如果属性值在初始化后发生更改,请不要忘记在您的模型中实现INotifyPropertyChanged
。它可以工作,我不知道如何感谢您,这是我在这里的第一个问题
class model
{
public string title {get;set;}
public string link {get;set;}
//etc..
}
<ListBox ItemsSource="{Binding posts}">