我试图在ListPicker for WP7的selectionchanged事件上选择一个特定项目。我相信我已经正确实现了ListPicker,并将一组文本绑定到ListPicker,但是在尝试选择ListPicker中的特定项时出现了问题。我的ListPicker在我的SettingsPage.xaml中,我想将某个字符串传递给我的MainPage.xaml对象以供在那里使用。我的代码如下
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();
}
}
基本上我想将选定的ListPicker项传递给MainPage,以便它可以用作webbrowser控件的默认搜索提供程序。关于如何实施这一点的任何想法?任何代码帮助将不胜感激..我真的不确定如何正确完成这一点(截至目前我没有任何调试或运行时错误,但webbrowser在尝试使用选定的ListPicker项时不搜索)?提前谢谢!
答案 0 :(得分:0)
以下内容将为您提供所选项目。
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
}
答案 1 :(得分:0)
ListPickerItem Item = SearchProviderListPicker.SelectedItem as ListPickerItem;
string type = Item.Content.ToString();
获取项目字符串。