Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get access to the QueryString in Windows Phone 7 from a user control

I have a simple user control in Windows Phone 7 and I want to get access to the querystring collection from the user controls Constructor. I have tried many ways and cannot seem to get acess to the containing XAML's querystring collection.

Essentially I am navigating to the page and the my user control is going to access the querystring value to write the value back to the interface.

Am I missing adding an assembly or reference or something?

like image 681
ryan maas Avatar asked Dec 12 '25 07:12

ryan maas


1 Answers

I am not sure you should be trying to get at the information from the page's constructor, as it won't necessairly get called every time you land on this page. A better approach is to override the OnNavigatedTo method inherited from PhoneApplicationPage. Looking a little more carefully at your question, you may be trying to do this within a control embedded in the page, in which case you need to get at the Page in order to obtain the navigation information.

Regardless, the NavigationContext property from the page has a QueryString parameter that you can use to access the information you're after.

The following example assumes I have a parameter named "Message" in the query string when navigating to this page:

public partial class MyPage : PhoneApplicationPage
{
    // Constructor
    public MyPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        String navigationMessage;
        if (NavigationContext.QueryString.TryGetValue("Message", out navigationMessage))
        {
            this.textBlock1.Text = navigationMessage;
        }
    }
}
like image 83
John Garland Avatar answered Dec 15 '25 03:12

John Garland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!