Windows phone 7 Windows Phone MVVM登录页面设计模式?

Windows phone 7 Windows Phone MVVM登录页面设计模式?,windows-phone-7,Windows Phone 7,我想创建一个登录页面,用户在其中输入用户名/密码,然后web服务进行身份验证并保存从服务器检索到的身份验证令牌 我希望在身份验证成功完成时通知页面视图 我的问题是:如何在MVVM模式中实现这一点?我为模型创建了一个类,为模型视图创建了一个类,为web服务的调用和解析创建了一个类 我无法将我的模型视图设置为页面的数据上下文,因为没有绑定到模型数据的控件 这是一种过度的模式,还是可以用另一种方式实现?请建议 谢谢您需要在ViewModel中放置指向执行web服务调用的方法的ICommands,并且视

我想创建一个登录页面,用户在其中输入用户名/密码,然后web服务进行身份验证并保存从服务器检索到的身份验证令牌

我希望在身份验证成功完成时通知页面视图

我的问题是:如何在MVVM模式中实现这一点?我为模型创建了一个类,为模型视图创建了一个类,为web服务的调用和解析创建了一个类

我无法将我的模型视图设置为页面的数据上下文,因为没有绑定到模型数据的控件

这是一种过度的模式,还是可以用另一种方式实现?请建议


谢谢

您需要在ViewModel中放置指向执行web服务调用的方法的
ICommand
s,并且视图中的元素应该绑定到这些命令以执行操作

在viewmodel中还需要一个布尔属性:IsLoggedIn,当对Web服务的登录调用返回成功时,将该属性设置为true

然后在您的视图中,您可以绑定到IsLoggedIn以向用户提供反馈


注意:不要忘记在其setter中为IsLoggedIn提升PropertyChanged。

您需要在ViewModel中放置指向执行web服务调用的方法的
ICommand
s,并且视图中的元素应该绑定到这些命令以执行操作

在viewmodel中还需要一个布尔属性:IsLoggedIn,当对Web服务的登录调用返回成功时,将该属性设置为true

然后在您的视图中,您可以绑定到IsLoggedIn以向用户提供反馈


注意:不要忘记在其setter中为IsLoggedIn提升PropertyChanged。

我有一个实现为的登录页面。登录页面本身没有viewmodel,但它使用了我编写的一个服务,该服务在登录完成时包含回调。该服务还包含有关用户的其他有用信息。我认为MVVM在这里会被过度使用

    private void LoginButton_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(EmailTextBox.Text)) return;
        if (string.IsNullOrEmpty(PasswordTextBox.Password)) return;

        Login();
    }

    private void Login()
    {
        if (DeviceNetworkInformation.IsNetworkAvailable == false)
        {
            MessageBox.Show("I'm having trouble connecting to the internet." + Environment.NewLine + "Make sure you have cell service or are connected to WiFi then try again");
        }
        else
        {
            LoginButton.Focus(); // Removes the keyboard
            UserProfile.Name = EmailTextBox.Text;
            UserProfile.Password = PasswordTextBox.Password;

            UserProfile.Current.Login(result =>
                {
                    // callback could be on another thread
                    Dispatcher.BeginInvoke(() =>
                        {
                            // Did the login succeed?
                            if (result.Result)
                            {
                                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                            }
                            else
                            {
                                string message = "Sorry, but I was not able to log in that user. Please make sure the name and password were entered correctly.";
                                MessageBox.Show(message, "Login failed");
                            }
                        });
                });
        }
    }

我有一个实现为的登录页面。登录页面本身没有viewmodel,但它使用了我编写的一个服务,该服务在登录完成时包含回调。该服务还包含有关用户的其他有用信息。我认为MVVM在这里会被过度使用

    private void LoginButton_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(EmailTextBox.Text)) return;
        if (string.IsNullOrEmpty(PasswordTextBox.Password)) return;

        Login();
    }

    private void Login()
    {
        if (DeviceNetworkInformation.IsNetworkAvailable == false)
        {
            MessageBox.Show("I'm having trouble connecting to the internet." + Environment.NewLine + "Make sure you have cell service or are connected to WiFi then try again");
        }
        else
        {
            LoginButton.Focus(); // Removes the keyboard
            UserProfile.Name = EmailTextBox.Text;
            UserProfile.Password = PasswordTextBox.Password;

            UserProfile.Current.Login(result =>
                {
                    // callback could be on another thread
                    Dispatcher.BeginInvoke(() =>
                        {
                            // Did the login succeed?
                            if (result.Result)
                            {
                                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                            }
                            else
                            {
                                string message = "Sorry, but I was not able to log in that user. Please make sure the name and password were entered correctly.";
                                MessageBox.Show(message, "Login failed");
                            }
                        });
                });
        }
    }