locked
How to use a variable from one page to another c#

    Question

  • Hey!

    I have 2 Pages.

    FirstPage.xaml has 2 textboxes ( textbox1  and textbox2 ) and a button. When you click the button, the second page appear.

    In the second page, i have a button . When i click the button, the program creates an text file. Until here, works perfect.

    In that file i want to write for example  (" Hello" + textbox1.Text + itextbox2.Text + "!" )

    How can i use variables from another page?

    I tried with this in First Page:

    Windows.Storage.ApplicationDataContainer var1 = Windows.Storage.ApplicationData.Current.LocalSettings;
                    var1.Values["Text1"] = textbox1.Text;
                    this.Frame.Navigate(typeof(SecondPage));
                    
    Windows.Storage.ApplicationDataContainer var2 = Windows.Storage.ApplicationData.Current.LocalSettings;
                    var2.Values["Text2"] = textbox2.Text;
                    this.Frame.Navigate(typeof(SecondPage));


    and this in the second:

    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                variable1.Text = Windows.Storage.ApplicationData.Current.LocalSettings.Values["Text1"].ToString();
    
    variable2.Text = Windows.Storage.ApplicationData.Current.LocalSettings.Values["Text2"].ToString();
               
            }
    
      private async void Button_Click_2(object sender, RoutedEventArgs e)
            {
             
                FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                // Dropdown of file types the user can save the file as
                savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
                // Default file name if the user does not type one in or select a file to replace
                savePicker.SuggestedFileName = "New Document";
                StorageFile file = await savePicker.PickSaveFileAsync();
                await FileIO.WriteTextAsync(file,"Hello, " + page6_1.Text + page6_2.Text + "!");
      }


    But doesn't work :(

    I got this error:

    An exception of type 'System.NullReferenceException' occurred in App1.exe but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    If there is a handler for this exception, the program may be safely continued.

    Any solution? or another method to use a variable to another pages.


    Thursday, July 03, 2014 11:49 AM

Answers

  • in the first page you call twice navigate method, just do it once after the the last one is set:

    Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;
                    settings.Values["Text1"] = textbox1.Text;
    
                    settings.Values["Text2"] = textbox2.Text;
                    this.Frame.Navigate(typeof(SecondPage));


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Torge Adelin Thursday, July 03, 2014 1:02 PM
    Thursday, July 03, 2014 12:58 PM

All replies

  • in the first page you call twice navigate method, just do it once after the the last one is set:

    Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;
                    settings.Values["Text1"] = textbox1.Text;
    
                    settings.Values["Text2"] = textbox2.Text;
                    this.Frame.Navigate(typeof(SecondPage));


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Torge Adelin Thursday, July 03, 2014 1:02 PM
    Thursday, July 03, 2014 12:58 PM
  • OMG ,Thank you SO MUCH!
    Thursday, July 03, 2014 1:03 PM