2010-10-22 105 views
0

在wp7中製作選項菜單的最佳方法是什麼? (不是一個上下文菜單或應用程序欄。)目前我正在使用字符串ListBox,但我不確定是否要這樣。Windows Phone 7:推薦的菜單控件?

我也不完全確定如何使ListBox條目響應點擊,以便他們導航到另一個頁面。

回答

0

您可以將偵聽器添加到列表框上的選擇更改事件,然後使用NavigationService根據所選項目導航到任何需要的位置。

1

對於一個簡單的菜單,我已經使用了幾次以下幾個。

<ListBox SelectionChanged="LinkSelected"> 
    <ListBoxItem Name="EnterCode" > 
     <TextBlock Text="Enter Code" /> 
    </ListBoxItem> 
    <ListBoxItem Name="Login" > 
     <TextBlock Text="Login" /> 
    </ListBoxItem> 
    <ListBoxItem Name="Register" > 
     <TextBlock Text="Register" /> 
    </ListBoxItem> 
</ListBox> 

然後像這樣的事件處理程序:

private void WelcomeLinkSelected(object sender, SelectionChangedEventArgs e) 
{ 
    if (sender is ListBox) 
    { 
     if ((sender as ListBox).SelectedIndex < 0) 
     { 
      return; 
     } 

     if ((sender as ListBox).SelectedIndex == 0) 
     { 
      NavigationService.Navigate(new Uri("/EnterCode.xaml", UriKind.Relative)); 
     } 

     if ((sender as ListBox).SelectedIndex == 1) 
     { 
      NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative)); 
     } 

     if ((sender as ListBox).SelectedIndex == 2) 
     { 
      NavigationService.Navigate(new Uri("/Register.xaml", UriKind.Relative)); 
     } 

     // This is very important! 
     (sender as ListBox).SelectedIndex = -1; 
    } 
} 

如果你這樣做,在事件處理程序,其重置SelectedIndex的最後一行,是非常重要的,因爲它允許在同一菜單項要連續多次選擇,而不必先選擇另一個選項。

+0

爲什麼所有的'(發件人爲列表框)'而不是隻鑄造一次? – 2010-10-24 14:26:14

+0

@Rosarch是的,你可以投一個。例子被從一些舊代碼中剔除,這些代碼也在爲其他事情做很多事情。 – 2010-10-25 08:14:14