locked
Passing Data between Second and First Page

    Question

  • Hello,

    i wonder if there is a possibility to pass Data between the Second and the First Page.

    Frame.GoBack() takes no parameters and i really need to pass data, the only way to make it work is a Singleton Pattern in my small but rising programmer mind. I thought about Observer Pattern too, but i am not sure if it is possible since i work with Pages. I currently have a MainPage which can Scan a Barcode or Play a Video or do some other fun stuff. 

    For Example:

    When I Scan a Barcode I Navigate to my BarcodeScanner Page. When it has found the Barcode and decoded it, BarcodeScanner Page should GoBack(), but pass the Result String to MainPage.


    PS: Oh I forgot to mention that I refuse to use a Singleton Pattern, if there is another workaround.


    • Edited by B.Ke Thursday, October 09, 2014 12:11 PM
    Thursday, October 09, 2014 12:09 PM

Answers

  • I haven't don't anything with Windows Store Apps yet, but in a WPF C# application I have two methods.

    First, I would pass the instance of one window to the second window it's like deriving a class but passed as a variable though the constructor(s) of that class.

    public class Window1
    {
         public Window1()
         {
              // Stuff
              new Window2(this);
         }
    
         // Stuff
    }
    
    public class Window2
    {
         public Window2(Window1 parent)
         {
              // Now you have full control over what happens
              // in Window1 and Window2 in Window2.
         }
    }

    Second, I would pass the data back and forwards between them.

    public class Window1
    {
         public Window1()
         {
              // Stuff
         }
         public Window1(string text)
         {
              // Stuff
         }
    }
    
    public class Window2
    {
         private Text
         public Window2(string text)
         {
              this.Text = text;
              // Stuff
         }
    
         private void Close()
         {
              new Window1(this.Text).Show();
         }
    }
    

    But I think the first one is more of what you are looking for.


    MSDN jbeen19

    • Marked as answer by B.Ke Friday, October 10, 2014 8:52 AM
    Thursday, October 09, 2014 2:11 PM
  • Hallo,

    you can ...

    1) Using a global variable

    define in the App.xaml.cs :

    public string MyString  { get; set; }

    use in Page.xaml.cs :

    string myString  =  (Application.Current as App).MyString

    2) Using a parameter between pages

    navigate from FirstPage.xaml.cs :

    this.Frame.Navigate(typeof(SecondPage), myString);

    to SecondPage.xaml.cs :

    private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        string myString = (string)e.NavigationParameter;
    }

    3) Using local app data

    set in FirstPage :

    var localSettings = ApplicationData.Current.LocalSettings;
    localSettings.Values["myString"] = myString;

    use in SecondPage :

    if (localSettings.Values.ContainsKey("myString"))
        string myString = (string)localSettings.Values["myString"];

    Regards
    Torsten

    • Marked as answer by B.Ke Friday, October 10, 2014 8:52 AM
    Thursday, October 09, 2014 6:26 PM

All replies

  • You can simply do a regular page navigation to your MainPage which allows you to pass data. Of course you should then clear the Back Stack (when arriving back on your MainPage) so that the previous version of MainPage and the BarcodeScannerPage no longer are listed there.

    Another approach would be to pass an object to the BarcodeScannerPage as a parameter into which you insert the result data upon scanning it. That would require your MainPage to hold on to a reference of the object that is being passed to the BarcodeScannerPage.

    Thursday, October 09, 2014 12:17 PM
  • In Frame.Navigate() please pass the arguments also implement this in Onbackkey pressed event.

    Hope this will solve your problem

    Muhammad Asad.

    Student Partner.


    Asad

    Thursday, October 09, 2014 1:43 PM
  • I haven't don't anything with Windows Store Apps yet, but in a WPF C# application I have two methods.

    First, I would pass the instance of one window to the second window it's like deriving a class but passed as a variable though the constructor(s) of that class.

    public class Window1
    {
         public Window1()
         {
              // Stuff
              new Window2(this);
         }
    
         // Stuff
    }
    
    public class Window2
    {
         public Window2(Window1 parent)
         {
              // Now you have full control over what happens
              // in Window1 and Window2 in Window2.
         }
    }

    Second, I would pass the data back and forwards between them.

    public class Window1
    {
         public Window1()
         {
              // Stuff
         }
         public Window1(string text)
         {
              // Stuff
         }
    }
    
    public class Window2
    {
         private Text
         public Window2(string text)
         {
              this.Text = text;
              // Stuff
         }
    
         private void Close()
         {
              new Window1(this.Text).Show();
         }
    }
    

    But I think the first one is more of what you are looking for.


    MSDN jbeen19

    • Marked as answer by B.Ke Friday, October 10, 2014 8:52 AM
    Thursday, October 09, 2014 2:11 PM
  • Your first Idea is not possible. I could do that, but i do have more than one "Features". Every feature has different data to pass. I could wrap the whole data into a object, but then i need to make a big (if/elseif/else)-Block or (Switch-Case)-Block for each different Feature.

    I will give your second approach a try. Just arguing with the NavigationHelper Holy Moly...

    Thursday, October 09, 2014 3:04 PM
  • You can use PhoneApplicationService.Current.State to pass data between any page at any time in your app.

    lets say that your bar code is a string. you can do this on a page

    string barcode = readBarCode();

    PhoneApplicationService.Current.State["BarCode"] = barcode;

    and then at any time, and in any page, you can do this:

    if (PhoneApplicationService.Current.State.ContainsKey("BarCode"))
    {

    barcode = (string)PhoneApplicationService.Current.State["BarCode"];

    PhoneApplicationService.Current.State.Remove("BarCode");

    }

    you can do that to pretty much any type of data.

    Hope that helps


    • Edited by Rickrvo Thursday, October 09, 2014 3:11 PM
    Thursday, October 09, 2014 3:09 PM
  • Hallo,

    you can ...

    1) Using a global variable

    define in the App.xaml.cs :

    public string MyString  { get; set; }

    use in Page.xaml.cs :

    string myString  =  (Application.Current as App).MyString

    2) Using a parameter between pages

    navigate from FirstPage.xaml.cs :

    this.Frame.Navigate(typeof(SecondPage), myString);

    to SecondPage.xaml.cs :

    private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        string myString = (string)e.NavigationParameter;
    }

    3) Using local app data

    set in FirstPage :

    var localSettings = ApplicationData.Current.LocalSettings;
    localSettings.Values["myString"] = myString;

    use in SecondPage :

    if (localSettings.Values.ContainsKey("myString"))
        string myString = (string)localSettings.Values["myString"];

    Regards
    Torsten

    • Marked as answer by B.Ke Friday, October 10, 2014 8:52 AM
    Thursday, October 09, 2014 6:26 PM
  • Ye your first approach seems to be working, but i have a new problem *sigh*.

    I need to set

    this.NavigationCacheMode = NavigationCacheMode.Enabled;

    to 

    this.NavigationCacheMode = NavigationCacheMode.Required;

    Everytime i do this, when I Frame.GoBack() on the Second Page it invokes Frame.GoBack() 2 Times.

    It seems like my First Page has 2 instances.

    When I change it back to Enabled, it goes back to the First Page once, but without result.

    Maybe gotta watch some tutorials on how to navigate between pages. I don't really get it.

    Friday, October 10, 2014 8:10 AM
  • Only Silverlight Apps support PhoneApplicationService. There is no equivalent for Windows Runtime Apps
    Friday, October 10, 2014 8:14 AM
  • Local Settings seem like what i was searching for. 

    Well not really, but i think it actually is the best way for newbies like me.

    Friday, October 10, 2014 8:31 AM