RonaldMunodawafa
RonaldMunodawafa

Reputation: 223

How do I access MainPage class non-static members from the App class?

I have added an application bar to my blank Windows Phone 8.1 Silverlight app as per MSDN documentation https://msdn.microsoft.com/en-us/library/windows/apps/hh394038(v=vs.105).aspx. I have added event handlers encapsulated within the App class to each of the buttons in the application bar. I need a way of accessing the current instance of MainPage class from the App class so I may call non-static methods in the MainPage class to update the UI. I have Binged for hours but I have not come across a solution. I came across this answer https://stackoverflow.com/a/13314364/3998051 but the question it was answering is sort of the reverse of my question. Sadly, the solution isn't. Would you please assist?

Upvotes: 1

Views: 135

Answers (1)

Contango
Contango

Reputation: 80342

I would solve this problem using Reactive Extensions (RX).

You can add Reactive Extensions (RX) to your project using NuGet, search for rx-main and install Reactive Extensions - Main Library.

Both of your pages would access a common class which would have a Subject<T>. One page would publish events to this Subject<T> using OnNext, and the other page would subscribe to these events using Subscribe.

For a complete worked example, see Rx how to create a sequence from a pub/sub pattern.

Upvotes: 1

Related Questions