[英]How to bind the ViewModel for a windows phone 8 messaging app
[英]how to bind the model to view from viewmodel in windows phone app
我正在使用MVVM模型创建应用程序。 我的应用程序是带有2个PanoramaItemTemplates的PanoramaApplication。
我创建了一个view,viewmodel和model文件夹,并分别在这些文件夹中包含Item.xaml
和Item1ViewModel.cs
, Item2ViewModel
和Item1Model.cs
, Item2Model.cs
文件。
在veiw Item.xaml
我具有以下控件:
当用户点击按钮“GetDetails”在PanoramaItem1页,我将显示该值ReadOperationStatus
, DateOfRegistration
和PointsEarned
, LastTimePlayed
, Name
中PanoramaItem2
页。
<phone:PhoneApplicationPage>
<Grid x:Name="LayoutRoot">
<phone:Panorama Title="AccountDetails">
<!--Panorama item one-->
<phone:PanoramaItem Header="item1">
<Grid>
<TextBox Text="{Binding EmailId}"></TextBox>
<TextBox Text="{Binding MobileNumber}"></TextBox>
<Button Content="GetDetails" Command="{Binding GetDetailsClickCommand}"></Button>
<TextBlock text="{Binding ReadOperationStatus}"/>
<TextBlock text="{Binding DateOFRegistration}"/>
</Grid>
</phone:PanoramaItem>
<!--Panorama item two-->
<phone:PanoramaItem Header="item2">
<Grid>
<TextBlock Text="{Binding PointsEarned}"></TextBlock>
<TextBlock Text="{Binding LastTimePlayed}"></TextBlock>
<TextBlock Text="{Binding Name}"></TextBlock>
</Grid>
</phone:PanoramaItem>
</phone:Panorama>
</Grid>
</phone:PhoneApplicationPage>
我的模型类中包含以下代码:
public class Item1Model
{
public Item1Model()
{
}
public string EmailID { get; set; }
public int MobileNumber { get; set; }
public string REadOperationStatus{ get; set; }
public DateTime DateOfRegistration { get; set; }
}
public class Item2Model
{
public Item2Model()
{
}
public int PointsEarned { get; set; }
public DateTime LastPlayed { get; set; }
public string Name { get; set; }
}
ViewModels具有以下代码:
class Item1ViewModel:InotifyPropertyChanged
{
public Item1ViewModel()
{
}
public System.Windows.Input.ICommand GetDetailsClickCommand
{
get
{
return new DelegateCommand((o) =>
{
Task.Factory.StartNew(() =>
{
GetPlayerDetails();
});
});
}
}
public static GetPlayerDetails()
{
//i want to access EmailID,MobileNumber which are inputs to the service method
//here i connect to a service and wait for the event DetailsDownload_Completed.
}
private void DetailsDownload_Completed(DetailsOwner sender, EventArgs args)
{
//here i get args.PointsEarned, args.LAstPlayed,args.Name ,
//which i want to bind them to the respective peroperties in the panoramaitem1,panoramaitem2
}
}
class Item2ViewModel
{
public Item2ViewModel ()
{
//currently i dont have anything here
}
}
the questions I have is how do I read the values of EMail, Mobilenumber which user has enterd in UI. And how do I set the values back in UI once I read the values from service..I want to know how should the binding be here..
我是否还需要在模型中实现INotifyPropertyChanged?
与代码的任何帮助将不胜感激
我认为您错过了MVVM的要点...
您的视图不应绑定或关心或了解您的模型。
INotifyProperty用于使绑定知道某些更改。
理想情况下,您的模型将执行逻辑操作……ViewModel将具有您将在View上绑定到的属性,并且它们将具有通知更改界面。
如果直接绑定到模型,那么就违反了MVVM的全部用途(并且可以摆脱ViewModel,因为未按需使用它)。
话虽如此,如果您四处搜寻,您会发现有些人将在模型中实现INPC界面,并且不会因此而杀死任何小猫。
是的,如果要直接绑定到模型而不是ViewModel,则模型必须实现INotifyPropertyChanged。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.