开发者

wp7: navigating dynamic pivotitems in a pivot

开发者 https://www.devze.com 2023-02-10 08:10 出处:网络
I have a pivot page, with one fixed pivotitem, and depending on data, a dynamic number of additional pivotitems. The fixed pivotitem keeps a hyperlink list of the new pivotitems created, an开发者_Stac

I have a pivot page, with one fixed pivotitem, and depending on data, a dynamic number of additional pivotitems. The fixed pivotitem keeps a hyperlink list of the new pivotitems created, an开发者_StackOverflowd the idea is to click any of the links and navigate to that pivotitem.

It would look something like this:

FixedItem | DynamicItem1 | DynamicItem2

link: DynamicItem1

link: DynamicItem2

The problem I am facing is with the hyperlink click, it doesn't take me to the respective pivotitem, but instead takes me to the pivot page with no dynamic pivotitems. I am using the following code for navigation:

hyperlink.NavigateUri = new Uri("/MainPage.xaml?name=" + p.name, UriKind.Relative);

where p.name is the name of the pivotitem. I am not sure if this is the right syntax, but what's confusing is that all the created pivotitems get lost, leaving only the fixeditem - as if it was opening a new instance of the pivot page.

Then, in the OnNavigatedTo I tried the following:

 if (NavigationContext.QueryString.ContainsKey("name"))
        {
            // URI is '/page?name=PivotItemToSelect'. 
            string selectedPivotItem = e.Uri.Query.Split('=').Last();

            // Match PivotItemToSelect with the PivotItem's Name. 
            PivotItem pivotItemToShow = pivot.Items.Cast<PivotItem>().Single(i => i.Name == selectedPivotItem);

            pivot.SelectedItem = pivotItemToShow;
            base.OnNavigatedTo(e);
        }

On the first line, I get an exception that this operation cannot be done on a "relative" URI. Any other way I could that information?

If I change the pivotitem name to a number, say i, and pass that i as the index, I got the following code to work as the _click method of the hyperlinkbutton - but in real usage the name will most likely be a text string:

pivot.SelectedIndex = int.Parse(i);

I am not sure how totally off the track I am, and would appreciate any pointers in the right direction.

Thanks.


The correct way to access the QueryString in your OnNavigatedTo event is like this:

string selectedPivotItem = this.NavigationContext.QueryString["name"];

Using a Pivot control for your navigation like this seems very strange, but since I know nothing about the context of your app, I'll assume you have chosen it for a reason.

If you are designing an app with a dynamic list of items to navigate to, it would be better to have a single page for your link list and a single detail page that can use the query string to bind to the specific object you are wanting. This will perform better and make more sense to your users. But again, I don't know the context of your app or your reasoning, so that's up to you! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号