0

MVVM モデルを使用してアプリケーションを作成しています。私のアプリケーションは、2 つの panoramaItemTemplates を持つ PanoramaApplication です。
ビュー、ビューモデル、モデル フォルダーを作成し、これらのフォルダーにそれぞれItem.xamlItem1ViewModel.cs、 、Item2ViewModel およびItem1Model.csItem2Model.csファイルを作成しました。

Item.xamlビューには、次のコントロール
があります。ユーザーが PanoramaItem1 ページで「GetDetails」ボタンをクリックすると、、、、、の値がページに表示さReadOperationStatusれます。DateOfRegistrationPointsEarnedLastTimePlayedNamePanoramaItem2

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

Model にも INotifyPropertyChanged を実装する必要がありますか?

コードの助けをいただければ幸いです

4

2 に答える 2

1

あなたはMVVMの要点を見逃していると思います...

ビューは、モデルをバインドしたり、気にしたり、知ったりしてはいけません。

INotifyProperty は、何かが変更されたことをバインディングに知らせるために使用されます。

理想的には、モデルがそのロジックを実行します... ViewModel には、View でバインドするプロパティがあり、通知変更インターフェイスがあります。

モデルに直接バインドしている場合は、MVVM の目的全体に違反しています (また、必要に応じて使用していないため、ViewModel を削除できます)。

そうは言っても、周りを検索すると、モデルに INPC インターフェイスを実装する人がいることがわかりますが、そのために子猫が殺されることはありません。

于 2013-11-12T08:22:46.927 に答える