2013-05-22 53 views
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; 
     }    
    } 
} 

會有人能夠幫助我們嗎?

+0

我建議你修改你的問題是關於一個特定的錯誤/問題,並將它指向你的代碼並刪除額外的代碼。 –

+0

我無法理解您的代碼。您正在註冊時將其寫入獨立存儲。但是在主頁面中,你如何比較人?當它沒有找到對象時肯定會拋出錯誤?所以我要求你重寫你的主頁代碼。 – Mani

回答

1

如果您的系統一次只有一個用戶,爲什麼不使用隔離存儲應用程序設置

你可以做登錄頁面是這樣的:

using System.IO.IsolatedStorage; 

private void loginButton_Click(object sender, RoutedEventArgs e) 
{ 
    if (IsolatedStorageSettings.ApplicationSettings.Contains("email") && IsolatedStorageSettings.ApplicationSettings.Contains("password")) 
    { 
     if((CType(appSettings("email"),String) == txtEmail.Text) && (CType(appSettings("password"),String) == txtPassword.Text) 
     { 
      //Successful Login 
     } 
     else 
     { 
      //Redirect to registration page 
     } 
    } 
    else //This means he is a first time user as settings have not been stored even once yet 
    { 
     //Redirect to registration page 
    }  
} 

註冊頁面你可以這樣做:

using System.IO.IsolatedStorage; 

IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings; 
appSettings.Add("email", txtEmail.Text.ToString()); 
appSettings.Add("password", txtPassword.Text.ToString()); 

嘗試了這一點。我沒有通過運行來測試它,但是這肯定會起作用。請讓我知道。