1
votes

I am attempting to choose a specific item upon the selectionchanged event of the ListPicker for WP7. I believe I have implemented the ListPicker properly and have binded an array of text to the ListPicker, but my issue arises when attempting to select a specific item in the ListPicker. My ListPicker is in my SettingsPage.xaml and I would like to pass a certain string to my MainPage.xaml object for use there. My code is as follows

SettingsPage.xaml

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="SearchProviderItemTemplate">
        <TextBlock Text="{Binding SearchProvider}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<!--Pivot Control-->
    <controls:Pivot Title="QUEST">
        <!--Pivot item one-->
        <controls:PivotItem Header="browser">
            <ScrollViewer x:Name="ContentPanel_Browser" Margin="12,0,12,0">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="70"/>
                        <RowDefinition Height="70"/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    ...

                    <!-- Search Provider -->
                    <TextBlock Text="Search provider" Margin="12,7,12,8"
                       Grid.Row="3" VerticalAlignment="Bottom"
                       Foreground="{StaticResource PhoneSubtleBrush}"/>
                    <toolkit:ListPicker x:Name="SearchProviderListPicker" Grid.Row="4" Grid.ColumnSpan="2" Margin="12,0,12,0"  
                                        ItemTemplate="{Binding SearchProviderItemTemplate}" 
                                        SelectionChanged="SearchProviderListPicker_SelectionChanged" />
                </Grid>
            </ScrollViewer>                
            <!--<Grid/>-->
        </controls:PivotItem>

SettingsPage.xaml.cs

    string searchProvider;

    String[] SearchProvider = 
    {
        "Google", 
        "Bing",
        "Yahoo",
        "Ask",
        "AOL"
    };


    private void SearchProviderListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems.Equals("Google"))
        {
            searchProvider = "http://www.google.com/search?q=";
        }
        else if(e.AddedItems.Equals("Bing"))
        {
            searchProvider = "http://www.bing.com/search?q=";
        }


        //if (SearchProvider.Equals("Google"))
        //{
        //    searchProvider = "http://www.google.com/search?q=";
        //}
        //else if(SearchProvider.Equals("Bing"))
        //{
        //    searchProvider = "http://www.bing.com/search?q=";
        //}

    }        

    public SettingsPage()
    {
        InitializeComponent();

        this.SearchProviderListPicker.ItemsSource = SearchProvider;
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        //Send searchProvider to MainPage.xaml
        if (searchProvider != null)
        {
            //this.NavigationService.Navigate(new Uri("/MainPage.xaml?curProvider=" + searchProvider, UriKind.Relative));
            NavigationContext.Equals("curProvider=" + searchProvider);
        }            
    }

MainPage.xaml.cs

string _searchProvider;

//OnNavigatedTo method
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        //for Search Provider (from SettingsPage.xaml)
        NavigationContext.QueryString.TryGetValue("curProvider", out _searchProvider);


    }

private void SearchBar_Click(object sender, KeyEventArgs e)
    {
        string search;
        string _search = SearchBar.Text;
        search =  HttpUtility.UrlEncode(_search);

        //search whats in the searchbar
        //use inputscope enter key to start search!
        if (e.Key == Key.Enter)
        {
            //check to ensure absolute navigation uri, else search through bing?
            if (String.IsNullOrEmpty(search)) 
                return;
            if (search.Equals("about:blank")) 
                return;

            if (_searchProvider != null)
            {                  
                TheBrowser.Navigate(_searchProvider + search);
            }

            //Google
            //TheBrowser.Navigate("http://www.google.com/search?q=" + search);

            //Bing
            //TheBrowser.Navigate("http://www.bing.com/search?q=" + search);

            //Yahoo
            //TheBrowser.Navigate("http://search.yahoo.com/search?p=" + search);

            //Ask
            //TheBrowser.Navigate("http://www.ask.com/web?q=" + search);

            //AOL
            //TheBrowser.Navigate("http://search.aol.com/search?q=" + search);


            //lose focus of textbox while navigating
            this.Focus();
        }
    }

essentially I would like to pass the selected ListPicker item to the MainPage so that it may be used as the default search provider of a webbrowser control. Any ideas on how this may be implemented? Any code assistance would be greatly appreciated.. I am really unsure of how to quite accomplish this correctly (as of now I do not have any debugging or runtime errors but the webbrowser does not search when attempting to use the selected ListPicker item)? Thanks in advance!

2
AddedItems returns a list of selected items. Doing a direct Equals("somestring") will not work. You'll have get the specific item object and them try to use a comparision. - abhinav

2 Answers

0
votes

The following will give you the selected item.


private void SearchProviderListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedItem = e.AddedItems[0] as string; //I'm not sure, whether it'll be string. Please check the type of objects in the list. But the logic will be the same.
        if(selectedItem == "Google")
        {
            searchProvider = "http://www.google.com/search?q=";
        }
        else if(selectedItem == "Bing")
        {
            searchProvider = "http://www.bing.com/search?q=";
        }
        //Continue the comparison

    }
0
votes
ListPickerItem Item = SearchProviderListPicker.SelectedItem as ListPickerItem;
 string type = Item.Content.ToString();

Gets the item string.