4

In Windows 10 UWP it is possible to create multiple windows (aka views which contain a separate window and a thread) for a single app:

But what is the right way to share data / objects among multiple views in UWP? No info on this is provided in MSDN. There is a good article on this topic: Windows 10, UWP, Multiple Windows, ViewModels and Sharing State. The difficulty is that App.xaml is separately instantiated for each view. It's interesting to see what others are doing in a similar situation?

A similar question which relates to pages but not views: (UWP) Best practice for sharing data between pages

Community
  • 1
  • 1
Bad
  • 4,967
  • 4
  • 34
  • 50
  • I totally disagree with putting shared data in app.xaml or similar hacks. You should set up a decent architecture with services or repositories to handle such complex scenarios. – Bart Dec 14 '15 at 14:45
  • @Bart Why not to use [concurrent collections](https://msdn.microsoft.com/en-us/library/dd997305(v=vs.110).aspx) and some [synchronization primitives](https://msdn.microsoft.com/en-us/library/ms228964(v=vs.110).aspx)? – Romasz Dec 14 '15 at 15:12
  • 1
    @Romasz nothing wrong with them, "au contraire", but I would still go for a decent architecture. – Bart Dec 14 '15 at 15:15

1 Answers1

4

While working with this you have multiple views, but still one application. The most important thing here is mentioned at MSDN you have linked:

Each window operates in its own thread.

This complicates a situation in case of sharing data. As you have one application, you can share some data in app class, but you must look out for few things:

  • UI elements can be accessed only via Dispatcher thread and in this case the one that owns them (so you cannot share this),
  • concurrent access to collections - as there are few threads that can simultaneously access/modify them, use Thread-Safe collections, also some help here,
  • in case of synchronization, you also may need some primitives to steer your application workflow,
  • race conditions - design your app with caution.

Some more help you can find at many blogs/posts. For example at Alabhari's one.

Romasz
  • 29,662
  • 13
  • 79
  • 154