Very simple MVVM problem

I am trying to create my first Silverlight application, but I cannot get the LogOn function to work, can you help me? It should be super easy for all of you, I will show you two of my files: LogOn.xaml.cs and LogOnViewModel.cs

Apparently, the problem is that UserId is not installed early enough to be available in LogOn.xaml.cx when I need it, could you help me get it to work to raise my point a bit :-)

public partial class LogOn : PhoneApplicationPage
{
    public LogOn()
    {
        InitializeComponent();
        this.DataContext = LogOnViewModel.Instance;
    }

    private void btnLogOn_Click(object sender, RoutedEventArgs e)
    {
        if ((!string.IsNullOrEmpty(txtEmailAddress.Text)) && (!string.IsNullOrEmpty(txtPassword.Password)))
        {
            txbLogonMessage.Text = "";
            LogOnViewModel.Instance.UserLogin(txtEmailAddress.Text, txtPassword.Password);

            if (LogOnViewModel.Instance.UserId > 0)
                NavigationService.Navigate(new Uri("/_2HandApp;component/Views/Main.xaml", UriKind.Relative));
            else
                txbLogonMessage.Text = "Login was unsuccessful. The user name or password provided is incorrect. Please correct the errors and try again. ";
        }
    }
}


public sealed class LogOnViewModel : INotifyPropertyChanged
{
    public static LogOnViewModel Instance = new LogOnViewModel();
    //public static int userId;

    private SHAServiceClient WS;

private int userId;
    public int UserId
    {
        get
        {
            return userId;
        }

        set
        {
            userId = value;
            this.RaisePropertyChanged("UserId");
        }
    }


private LogOnViewModel()
    {
        WS = new SHAServiceClient();
        WS.UserLoginCompleted += new EventHandler<UserLoginCompletedEventArgs>(WS_UserLoginCompleted);
    }

    void WS_UserLoginCompleted(object sender, UserLoginCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            this.UserId = e.Result;
        }
    }


    public void UserLogin(string email, string password)
    {
        WS.UserLoginAsync(email, password);
    }

/* Implementing the INotifyPropertyChanged interface. */
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
+3
source share
2 answers

, @flq. , , ( UserId ), , subscibe Completed ( ) , .

" MVVM", (, , , ), : , MVVM Light! MVVM, . ViewModel ViewModelBase MVVMLight, , . , xaml, MVVMLight RelayCommand. , , , , ( ), .

:

public class LogOnViewModel : ViewModelBase
{
    private SHAServiceClient WS;
    public LogOnViewModel()
    {
       WS = new SHAServiceClient();
       WS.UserLoginCompleted += new EventHandler<UserLoginCompletedEventArgs>(WS_UserLoginCompleted);
       LoginCommand = new RelayCommand(UserLogin);
    }
    private int userId;
    public int UserId
    {
       get { return userId; }
       set
       {
          userId = value;
          RaisePropertyChanged(()=>UserId);
       }
    }
    private int password;
    public int Password
    {
       get { return password; }
       set
       {
          password = value;
          RaisePropertyChanged(()=>Password);
       }
    }
    private int username;
    public int Username
    {
       get { return username; }
       set
       {
          username = value;
          RaisePropertyChanged(()=>Username);
       }
    }
    private int loginErrorMessage;
    public int LoginErrorMessage
    {
       get { return loginErrorMessage; }
       set
       {
          loginErrorMessage = value;
          RaisePropertyChanged(()=>LoginErrorMessage);
       }
    }
    void WS_UserLoginCompleted(object sender, UserLoginCompletedEventArgs e)
    {
       if (e.Error == null)
       {
          this.UserId = e.Result;
          // send a message to indicate that the login operation has completed
          Messenger.Default.Send(new LoginCompleteMessage());
       }
    }
    public RelayCommand LoginCommand {get; private set;}
    void UserLogin()
    {
       WS.UserLoginAsync(email, password);
    }
}

xaml:

<TextBox Text="{Binding Username, Mode=TwoWay}"/>
<TextBox Text="{Binding Password, Mode=TwoWay}"/>
<Button Command="{Binding LoginCommand}"/>
<TextBlock Text="{Binding LoginErrorMessage}"/>    

:

public partial class LogOn : PhoneApplicationPage
{
    public LogOn()
    {
        InitializeComponent();
        this.DataContext = new LogOnViewModel();
        Messenger.Default.Register<LoginCompletedMessage>(
                            this,
                            msg=> NavigationService.Navigate(
                                    new Uri("/_2HandApp;component/Views/Main.xaml",
                                    UriKind.Relative) );
    }
  ....
}

, ViewModel ( ) . DataBinding, MVVM.

, :)

PS: LoginCompletedMessage ( ), ( , UserId )

+4

, WS.UserLoginAsync, , , , .

MVVVM , , , . "Viewmodel", (WS_UserLoginCompleted). .

+1

All Articles