嗨,我目前在主页上有一个登录屏幕,允许用户输入他们的用户名和密码进行登录,以及一个注册页面,供用户在登录不正确时向系统注册他们的用户名和密码。我想要它,以便当用户尝试在主页上登录时,程序会告诉用户他们的用户名和密码无法识别,他们必须转到注册页面并首先注册。现在,只有当用户在用户名和密码框中输入空格时才有效,但只要他们在系统中输入文本就会崩溃。
当用户导航到注册页面并输入他们的用户名和密码并成功注册时,程序告诉他们他们已经注册成功,然后他们返回主页登录,他们可以正常登录,但由于某种原因每当用户在未注册的情况下在主页上输入用户名或密码时尝试登录,系统就会崩溃。我该怎么做才能让用户在注册程序之前尝试使用用户名和密码登录时说无法识别他们的用户名和密码?
这是我的主页代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;
namespace TimeSheetRecordingSystem
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
textBox1.Text = "";
//textBox2.Text = "";
}
public class UserInformation
{
string username;
string password;
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text) && !String.IsNullOrEmpty(passwordBox1.Password))
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<UserInformation>));
using (XmlReader xmlReader = XmlReader.Create(stream))
{
List<UserInformation> users = (List<UserInformation>)serializer.Deserialize(xmlReader);
NavigationService.Navigate(new Uri("/timesheet.xaml", UriKind.Relative));
}
}
}
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<UserInformation>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, GeneratePersonData());
}
}
}
}
else if (textBox1.Text == "" || passwordBox1.Password == "")
{
MessageBox.Show("Username or Password is not recognised");
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/registration.xaml", UriKind.Relative));
}
private List<UserInformation> GeneratePersonData()
{
List<UserInformation> data = new List<UserInformation>();
UserInformation ui = new UserInformation();
ui.Username = textBox1.Text;
ui.Password = passwordBox1.Password;
data.Add(ui);
return data;
}
}
}
这是我的注册页面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;
namespace TimeSheetRecordingSystem
{
public partial class registration : PhoneApplicationPage
{
public registration()
{
InitializeComponent();
textBox1.Text = "";
}
public class UserInformation
{
string username;
string password;
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text) && !String.IsNullOrEmpty(passwordBox1.Password))
{
////Write to Isolated Storage
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<UserInformation>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, GeneratePersonData());
NavigationService.Navigate(new Uri("/account successful.xaml", UriKind.Relative));
}
}
}
}
else if (textBox1.Text == "" || passwordBox1.Password == "")
{
MessageBox.Show("Username or Password is not recognised");
}
}
private void saveText(string filename, string text)
{
using (IsolatedStorageFile isif = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream rawStream = isif.CreateFile(filename))
{
StreamWriter writer = new StreamWriter(rawStream);
writer.Write(text);
writer.Close();
}
}
}
private List<UserInformation> GeneratePersonData()
{
List<UserInformation> data = new List<UserInformation>();
UserInformation ui = new UserInformation();
ui.Username = textBox1.Text;
ui.Password = passwordBox1.Password;
data.Add(ui);
return data;
}
}
}
有人能帮忙吗?