locked
Passing variables with Navigation Helper (8.1 vs 8?)

    Question

  • I'm trying to develop an RSS reader for a very simple application as a project.

    I've completed the FeedData.cs, and then successfully added the content to a page that shows each RSS feed (only using 1 right now). 

    Now, I want to be able to click on an RSS feed and have that RSS's posts come up in a split view page. 

    For some reason, all the tutorials I see on doing this have the default method constructed that looks like this: 

    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
                // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]
                string feedTitle = (string)navigationParameter;
                FeedData feedData = FeedDataSource.GetFeed(feedTitle);
                if (feedData != null)
                {
                    this.DefaultViewModel["Feed"] = feedData;
                    this.DefaultViewModel["Items"] = feedData.Items;
                }

    But, with my project (Windows 8.1), I get this default loadstate method when I create a splitview page:

    private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // TODO: Assign a bindable group to Me.DefaultViewModel("Group")
                // TODO: Assign a collection of bindable items to Me.DefaultViewModel("Items")
    This is my current codebehind on the split view page:

     public void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // TODO: Assign a bindable group to Me.DefaultViewModel("Group")
                // TODO: Assign a collection of bindable items to Me.DefaultViewModel("Items")
                string feedTitle = (string)sender;
                FeedData feedData = FeedDataSource.GetFeed(feedTitle);
                
                if (feedData != null)
                {
                    this.DefaultViewModel["Feed"] = feedData;
                    this.DefaultViewModel["Items"] = feedData.Items;
    
                }
    This is the line in question that crashes the program:
    string feedTitle = (string)sender;
    Does anyone know why I'm unable to cast the sender object passed to the method as a string? the exact same line seems to work for everyone who uses the other LoadState method. 

    I greatly appreciate your time!!

    Jonathan Sebra




    Monday, April 28, 2014 8:48 PM

Answers

  • If you look at the implementation of NavigationHelper.cs, you will notice that the LoadState method is called with the first parameter as the object itself, which is why casting the first parameter to a string will not work. If you want to retrieve the parameter that was passed to Frame.Navigate(Page, <parameters>), then you can access the LoadStateEventArgs.NavigationParameter property to retrieve the parameters.

    For example, if I use: rootFrame.Navigate(typeof(ItemsPage), new String[]{"Apple", "Banana"});

    then in the navigationHelper_LoadState method, I can retrieve the parameters likes this:

            private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                var parameters = e.NavigationParameter;
                if (parameters.GetType() == typeof(String[]))
                {
                    foreach (String param in (String[])parameters)
                    {
                        Debug.WriteLine("Param: {0}", param);
                    }
                }
                ...// other code
    


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Monday, April 28, 2014 9:47 PM
    Moderator

All replies

  • If you look at the implementation of NavigationHelper.cs, you will notice that the LoadState method is called with the first parameter as the object itself, which is why casting the first parameter to a string will not work. If you want to retrieve the parameter that was passed to Frame.Navigate(Page, <parameters>), then you can access the LoadStateEventArgs.NavigationParameter property to retrieve the parameters.

    For example, if I use: rootFrame.Navigate(typeof(ItemsPage), new String[]{"Apple", "Banana"});

    then in the navigationHelper_LoadState method, I can retrieve the parameters likes this:

            private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                var parameters = e.NavigationParameter;
                if (parameters.GetType() == typeof(String[]))
                {
                    foreach (String param in (String[])parameters)
                    {
                        Debug.WriteLine("Param: {0}", param);
                    }
                }
                ...// other code
    


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Monday, April 28, 2014 9:47 PM
    Moderator
  • Such a simple answer! That did the trick. Thank you so much for your time.

    Jonathan
    Monday, April 28, 2014 9:50 PM
  • Cool, glad to know it helped :)

    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Monday, April 28, 2014 9:53 PM
    Moderator