Reputation: 33
In a UWP project, I'm trying to pass information between two windows. Clicking an item will essentially open another XAML page with more detailed information. I'm not navigating because I don't want the main page to disappear.
Code is below, and it works as expected. The XAML opens, I can see all the controls and the code runs as expected. Essentially there are a few textblocks I would like to pre-populate in the Detail.xaml file.
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(Detail), null);
Window.Current.Content = frame;
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
code was taken from https://learn.microsoft.com/en-us/windows/uwp/design/layout/show-multiple-views
Upvotes: 2
Views: 730
Reputation: 39102
If you have the data you want to display available already before you open the secondary view, you can pass them to the Detail
view easily during navigation:
string data = ...;
frame.Navigate(typeof(Detail), data);
When you need to actually communicate between the views however, things get more tricky. Each view in UWP has its own UI thread and its own Dispatcher
. This means all code dealing with the UI (like populating controls, changing the page background, etc.) needs to run on the thread of the given view. One way to do this "communication" is to store the reference to the newly created application view, or access it through the CoreApplication.Views
property. Whenever you need to manipulate the view's UI, you use the dispatcher.
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
//do something with the secondary view's UI
//this code now runs on the secondary view's thread
//Window.Current here refers to the secondary view's Window
//the following example changes the background color of the page
var frame = ( Frame )Window.Current.Content;
var detail = ( Detail )frame.Content;
var grid = ( Grid )detail.Content;
grid.Background = new SolidColorBrush(Colors.Red);
}
Upvotes: 2