views:

211

answers:

3

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?

+1  A: 

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;
        }
    }
}
avidgator
I think that won't work since I am in a UserControl and not aPhoneApplicationPage as you are referring to in the class definition header. That's the trick. I can't seem to get access to anything on NavigationContent once inside a UserControl. Page/XAML level, no problemo. In the UserControl, nothing.
ryan maas
A: 

Sorry about that - I started to get there, and thanks for the clarification. Your best bet then is to walk up the visual tree from your control to find the Page, then you can have at the NavigationContext. In my sample below, I have a button on a custom control within the page, whose click event finds the nav context and looks for a certain navigation parameter - I couldn't tell from the question or your follow-up what would drive the control to "want" to find the content of the query string.

(Note about getting info from the ctor follows the code below)

public partial class WindowsPhoneControl1 : UserControl
{
    public WindowsPhoneControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        GetTheQueryString();
    }

    private void GetTheQueryString()
    {
        var result = "No Joy";
        var page = FindRootPage(this);
        if (page != null)
        {
            if (page.NavigationContext.QueryString.ContainsKey("Param"))
            {
                result = page.NavigationContext.QueryString["Param"];
            }
        }
        queryStringText.Text = result;
    }

    private static PhoneApplicationPage FindRootPage(FrameworkElement item)
    {
        if (item != null && !(item is PhoneApplicationPage))
        {
            item = FindRootPage(item.Parent as FrameworkElement);
        }
        return item as PhoneApplicationPage;
    }
}

Note that this won't work from the ctor because of how Xaml works...the Xml tag drives the ctor to be called, then properties are set as indicated, then it is added as a child/item/etc in its container. If you do need to get at the context ASAP using this "walk up the tree" technique, handle the Control's Loaded event, by which time the control does have a parent and a tree that can be walked...

    public WindowsPhoneControl1()
    {
        InitializeComponent();
        Loaded += WindowsPhoneControl1_Loaded;
    }

    private void WindowsPhoneControl1_Loaded(Object sender, RoutedEventArgs e)
    {
        GetTheQueryString();
    }
avidgator
A: 

I would add a property to the UserControl subclass that would be set by the page in its OnNavigatedTo() method.

Andréas Saudemont