c# - Windows 8 App - Pass Data to MainPage -
i want pass data child page of mainpage in app. problem, can tell there splash loading screen navigates mainpage , want write "if navigated child page" block include in onnavigatedto.
here mainpage.xaml.cs code:
protected override void onnavigatedto(navigationeventargs args) { if (base.onnavigatedfrom(args) = childpage) { // code for: if navigated child page returndata returndata = args.parameter returndata; this.mynewstring = returndata.mystring; } base.onnavigatedto(args); }
i found out cannot return data way used passing data page windows 8 apps. cannot return data way pass i.e. this.frame.navigate(typeof(mainpage), passdata);
for solution, created 'completed' event , and called mainpage c# code behind.
mainpage.xaml.cs:
protected override void onnavigatedfrom(navigationeventargs args) { if (args.sourcepagetype.equals(typeof(childpage))) (args.content childpage).completed += onchildpagecompleted; base.onnavigatedfrom(args); } void onchildpagecompleted(object sender, returndata args) { // code run if returned mainpage childpage this.mynewstring = returndata.mystring; (sender childpage).completed -= onchildpagecompleted; }
returndata.cs:
namespace mytestapp { class returndata { public string mystring { set; get; } } }
childpage.xaml.cs:
public event eventhandler<returndata> completed;
childpage.xaml.cs:
protected override void onnavigatedfrom(navigationeventargs args) { if (completed != null) { // create returndata object returndata returndata = new returndata(); returndata.mystring = newstringchanged; // fire completed event completed(this, returndata); } base.onnavigatedfrom(args); }