0

嗨,我目前在主页上有一个登录屏幕,允许用户输入他们的用户名和密码进行登录,以及一个注册页面,供用户在登录不正确时向系统注册他们的用户名和密码。我想要它,以便当用户尝试在主页上登录时,程序会告诉用户他们的用户名和密码无法识别,他们必须转到注册页面并首先注册。现在,只有当用户在用户名和密码框中输入空格时才有效,但只要他们在系统中输入文本就会崩溃。

当用户导航到注册页面并输入他们的用户名和密码并成功注册时,程序告诉他们他们已经注册成功,然后他们返回主页登录,他们可以正常登录,但由于某种原因每当用户在未注册的情况下在主页上输入用户名或密码时尝试登录,系统就会崩溃。我该怎么做才能让用户在注册程序之前尝试使用用户名和密码登录时说无法识别他们的用户名和密码?

这是我的主页代码:

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;
        }              
    }
}

有人能帮忙吗?

4

1 回答 1

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());

试试这个。我没有通过运行它来测试它,但这肯定可以工作。请让我知道。

于 2013-05-23T06:09:47.533 回答