1
votes

I have a file in IsolatedStorage. If the file exists, I want to redirect to the login page or Create Account page.

If the file doesnt exist, the app goes to the Create page, a password is created and saved, and the app redirects to the Login page. However, if the file in IsolatedStorage exists, it won't direct.

private void fileExists()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists("passwordFile"))
            {
                //NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
                MessageBox.Show("Should be redirecting here");
            }

            else
            {
                MessageBox.Show("Welcome. Please create an account. Ensure that you remember your password!");
            }
        }

The actual message does show so it's being called and if a file does not exist, the else is executed so my logic is sound.

The FileExists() function is called here.

public MainPage()
        {
            InitializeComponent();
            fileExists();
        }

The other redirect happens here

if ((password1.Password == password2.Password) & (password1.Password.Trim().Length > 0 || password2.Password.Trim().Length > 0))
            {
                byte[] PasswordByte = Encoding.UTF8.GetBytes(password1.Password);
                byte[] ProtectedPassword = ProtectedData.Protect(PasswordByte, null);
                this.WritePasswordToFile(ProtectedPassword);

                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
            }

The error is a System.NullReferenceException but was not handled in user code.

3
real offtopic here, please hash or encrypt your passwordFile, with some fancy coding and modded WP8 it easily to access the isolatedStorage - EaterOfCode
I will be, it's literally just the start! I just started C# and WP yesterday and encryption is a big part of the project, but not something that I will be looking at for a few weeks. Thanks though! - Chris O'Brien

3 Answers

1
votes

Have tried to call the file exist check on MainPage load ? That can be storage preparing issue even if it is executing. Secondly if you can mention where the exact exception is occuring. Also do check this link that might help you.

1
votes

The problem is that the NavigationService is still null, you can verify that by putting a breakpoint on the redirect line, put the same code in the MainPage.Loaded event and it will work then, (I expect it to work then)

as I expect this is only a redirect page you can check the file in the initialization and save uri to redirect in the class and redirect when the page is loaded

0
votes

I needed to move the fileExists() from the constructor to a new function.

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            fileExists();
        }