我试图让我的第一个Silverlight应用程序有史以来,但我不能让LogOn函数工作,你能帮我吗?这对你们来说应该是非常简单的,我会告诉你我的两个文件:LogOn.xaml.cs和LogOnViewModel.cs非常非常简单的MVVM问题
显然问题在于UserId的设置不够早,无法在LogOn.xaml中使用.CX我需要的时候,你能不能帮我做它的工作,这将提升我的时刻了不少:-)
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));
}
}
}
嗨Abdou Moumen,非常感谢您的所有工作,我只需要一段时间来看看它是否可以实现它的工作。我真的很失落和沮丧,但也许在查看你的代码并获得MVVMLight之后,我可以开始工作,我希望如此,以后我会回来的。 – rune007 2011-05-15 18:45:56
@ rune007 silverlight是一个伟大的技术,MVVM是一个非常有趣的模式,所以保持它,祝你好运) – AbdouMoumen 2011-05-15 19:44:29
再次感谢您的详细答案Abdou Moumen先生,您的文章是有用的,因为我了解MVVM和MVVMLight: - ) – rune007 2011-05-16 07:42:32