In my WPF application i have multiple pages. One page is called "startup" and this is the one shown most of the time. An NFC-Reader is running in the background on a thread and everytime you scan a card, it executes code.
My navigation works by having a class called "InstanceContainer.cs" which basically just holds all the instances to my pages which i then navigate to using (for example) NavigationService.Navigate(InstanceContainer.startup());
However i want it to only execute said code, when "startup" is shown while a card is scanned. My idea would be to just set a public bool like public bool startupShown;
inside of "startup" which is then checked for in the thread that runs the NFC-Scanning.
But how do i neatly update this bool? Of course it could be updated manually on every buttonclick that leads away and to "startup", but there has to be a better way than that.
I found this but didn't quite understand it nor could i get it to work. Thanks in advance
CodePudding user response:
You could use the Page
's IsLoaded
property to determine whether it's currently loaded and displayed in the Frame
.
This property can however not be accessed directly from a background thread so you could either add your own field or property to the start page and handle the Loaded
and Unloaded
events to set it...:
public partial class StartupPage : Page
{
public bool startupShown;
public Page1()
{
InitializeComponent();
Loaded = (s, e) => startupShown = true;
Unloaded = (s, e) => startupShown = false;
}
}
...or use the dispatcher to access the built-in Loaded
property from the background thread:
Page page = InstanceContainer.startup();
bool isloaded = page.Dispatcher.Invoke(() => page.IsLoaded);