0
您好我目前有一個登錄屏幕的主頁上,讓用戶輸入他們的用戶名和密碼登錄和註冊頁面爲用戶註冊他們的用戶名和登錄時系統密碼不正確。我希望這樣,當用戶嘗試登錄主頁面時,程序會告訴用戶他們的用戶名和密碼不能識別,他們必須先到註冊頁面並先註冊。現在只有當用戶在用戶名和密碼框中輸入空格時,但只要他們在系統崩潰中放置文本時,該功能才起作用。Windows Phone 7登錄錯誤與孤立存儲c#
當用戶導航到註冊頁面並輸入用戶名和密碼並註冊成功時,程序會告訴他們他們已經成功註冊,然後他們回到主頁登錄,但他們可以登錄,但可以正常登錄由於某種原因,無論用戶在沒有註冊的情況下在主頁面輸入用戶名或密碼時嘗試登錄,系統都會崩潰。我該怎麼做,以便當用戶在註冊程序之前嘗試使用用戶名和密碼登錄時說他們的用戶名和密碼無法識別?
這裏是我的主網頁代碼:
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;
}
}
}
會有人能夠幫助我們嗎?
我建議你修改你的問題是關於一個特定的錯誤/問題,並將它指向你的代碼並刪除額外的代碼。 –
我無法理解您的代碼。您正在註冊時將其寫入獨立存儲。但是在主頁面中,你如何比較人?當它沒有找到對象時肯定會拋出錯誤?所以我要求你重寫你的主頁代碼。 – Mani