【问题标题】:how to bind the model to view from viewmodel in windows phone app如何在 Windows Phone 应用程序中将模型绑定到视图模型
【发布时间】:2013-11-21 05:13:32
【问题描述】:

我正在使用 MVVM 模型创建应用程序。我的应用程序是具有 2 个全景项模板的全景应用程序。
我已经创建了一个视图、视图模型和模型文件夹,并且在这些文件夹中分别有 Item.xaml 和 Item1ViewModel.cs,Item2ViewModel 和 Item1Model.cs,Item2Model.cs 文件。

在 veiw Item.xaml 我有以下控件:
当用户单击 PanoramaItem1 页面中的“GetDetails”按钮时,我将在PanoramaItem2 页面中显示ReadOperationStatus、DateOfRegistration 和PointsEarned、LastTimePlayed、Name 的值。

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

ViewModel 的代码如下:

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 吗?

非常感谢您对代码的任何帮助

【问题讨论】:

    标签: c# windows-phone-7 mvvm windows-phone-8 mvvm-light


    【解决方案1】:

    我认为你错过了 MVVM 的重点......

    您的视图不应绑定、关心或了解您的模型。

    INotifyProperty 用于让绑定知道发生了变化。

    理想情况下,您的模型将执行逻辑操作... ViewModel 将具有您将绑定到视图上的属性,并且它们将具有通知更改界面。

    如果您直接绑定到您的模型,则违反了 MVVM 的全部目的(您可以摆脱 ViewModel,因为您没有按应有的方式使用它)。

    话虽如此,如果你四处搜索,你会发现有些人会在 Model 中实现 INPC 接口,并且不会因此而杀死任何小猫。

    【讨论】:

    • 很高兴这对您有所帮助...涉及大量阅读:)。你可以点赞它,你觉得它很有用:)
    【解决方案2】:

    是的,如果您想直接绑定到模型而不是视图模型,模型必须实现 INotifyPropertyChanged。

    【讨论】:

    • 那么在 ViewModel GetDetailsClickCommand 中如何读取 UI 中设置的 EmailID、MobileNumber 值?
    • UI 绑定到模型,视图模型管理模型。在您的虚拟机中,您应该有一个模型属性来跟踪模型(无论如何都需要一种将模型公开给视图的方法)。见catelproject.atlassian.net/wiki/display/CTL/…,你使用的是“快捷解释”。
    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多