10

I was wondering what the best practice is for sending variables like 'selectedItem' and so on between pages in UWP? Is it a good idea to just create a static global variable class that every Page knows of?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
stonecompass
  • 547
  • 9
  • 27

2 Answers2

12

I'm going to sum up Microsofts Best Practice here:

For simple data (like strings):
Use the Frame.Navigate(TypeName, Object) method, where as the second argument should always be a string (even if it allows objects). The second argument can then be extracted from the NavigationEventArgs.Parameter in the Frame.Navigated event handler.

For complex data (anything besides strings):
You may choose from two options here, depending on the size and complexity of your app:

  • Either manage a reference to any complex data inside your App class directly
  • Or keep a reference to them in any kind of Manager class, that is a member of your App class. (e.g. NavigationDataManager).
Stefan Over
  • 5,851
  • 2
  • 35
  • 61
  • Thanks for the great answer! Can you maybe give me a link to where you found these best practices, because I'm interested in reading about them all? :) – stonecompass Oct 21 '15 at 06:58
  • @DanielBross It's in a _Microsoft Virtual Academy_ course. It's at least in this [German course](https://www.microsoftvirtualacademy.com/de-de/training-courses/grundlagen-f-r-windows-10-f-r-entwickler-11538), but should also be in this [English one](https://www.microsoftvirtualacademy.com/en-us/training-courses/a-developers-guide-to-windows-10-12618?l=IV8HDBpRB_9005095281). – Stefan Over Oct 21 '15 at 07:54
  • Okay, thanks! Luckily I'm German too, so I should be able to understand both! :) – stonecompass Oct 21 '15 at 08:04
  • @Herdo, for the Frame.Navigate case, is there a similar alternative for passing multiple strings during navigation? Or can only *one* simple type be passed per navigation? Thx! – bunkerdive Feb 10 '16 at 06:38
  • @bunkerdive It's stated by Microsoft that only `string` should be used. I gonna try if `string[]` works as well, later today. – Stefan Over Feb 10 '16 at 07:13
  • 1
    @Herdo You saved me so much coding work... Thanks :) – Twometer Sep 30 '17 at 15:22
1

Well in fact if you use MVVM approach you have all necessary info in ModelView class(es). In case you do not use MVVM just use a singleton class or even a static global class.

Sergiu Cojocaru
  • 687
  • 6
  • 16