0
votes

I want to pass data from a child page of the MainPage in my app. The problem, from what I can tell is that there is a splash loading screen that navigates to the MainPage and I want to write an "If navigated from child page" block to include in the OnNavigatedTo.

Here is my MainPage.xaml.cs code:

    protected override void OnNavigatedTo(NavigationEventArgs args)
    {
        if (base.OnNavigatedFrom(args) = ChildPage)
        {
        //    Code for: If navigated from child page
            ReturnData returnData = args.Parameter as ReturnData;
            this.myNewString = returnData.myString;
        }
        base.OnNavigatedTo(args);
    }
1
I figured it out, will post my answer.ant-C

1 Answers

1
votes

I found out that you cannot return data the way it is used by passing data to another page for Windows 8 Apps. You cannot return data they way you pass it i.e. this.Frame.Navigate(typeof(MainPage), passData);

For the solution, I created a 'Completed' event and and called it from the MainPage C# code behind.

MainPage.xaml.cs:

    protected override void OnNavigatedFrom(NavigationEventArgs args)
    {
        if (args.SourcePageType.Equals(typeof(ChildPage)))
            (args.Content as ChildPage).Completed += OnChildPageCompleted;
        base.OnNavigatedFrom(args);
    }

    void OnChildPageCompleted(object sender, ReturnData args)
    {
        // Code to run if returned back to MainPage from ChildPage
        this.myNewString = returnData.myString;
        (sender as 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);
}