2013-10-10 2 views
0

과일 애플 리케이션을위한 목록 상자가 있습니다. 목록 상자의 각 항목을 선택하면 사용자에게 과일에 대해 더 많이 알려주는 다른 페이지로 이동하게합니다. 한 페이지로 이동하는 코드를 알고 있지만 여러 페이지로 이동하기 위해 switch case 문을 사용하는 데 문제가 있습니다.목록 상자의 각 항목을 다른 페이지로 이동합니다.

에서 MainPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,10,14,-10"> 
<phone:LongListSelector HorizontalAlignment="Left" Height="432" VerticalAlignment="Top" Width="446" Margin="7,129,0,0"/> 
     <ListBox Margin="0,-12,0,52" Name="FruitListbox" SelectionChanged="fruitList_SelectionChanged" SelectionMode="Extended" ItemsSource="{Binding}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Height="230"> 
         <Image Name="Image" Width="100" Height="250" Source="{Binding Image}" VerticalAlignment="Top" Margin= "0,0,10,0"></Image> 
         <StackPanel x:Name="lBtn" Width="370" HorizontalAlignment="Left" Height="350"> 
          <TextBlock Text="{Binding Name}" Foreground="Red"></TextBlock> 
          <TextBlock Text="{Binding Description}" Foreground="Aqua"></TextBlock> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

MainPage.xaml.cs를

namespace Carifruits 
{ 
public partial class MainPage : PhoneApplicationPage 
{ 
    ObservableCollection<Fruit> fruitiness = new ObservableCollection<Fruit>(); 

public MainPage() 
{ 

    InitializeComponent(); 
    Fruit fruit1 = new Fruit 
    { 
     Name = "Abricot", 
     Description = "Classified with the Plum", 
     Image = "http://img.xooimage.com/files68/5/1/1/abricot-gp14-1--30f6578.jpg", 

    }; 
    Fruit fruit2 = new Fruit 
    { 
     Name = "Breadfruit", 
     Description = "Species of flowering tree", 
     Image = "http://nickandmiri.files.wordpress.com/2010/06/breadfruit2.jpg", 



    }; 
    Fruit fruit3 = new Fruit 
    { 
     Name = "Coconut", 
     Description = "Can refer to the entire Coconut Palm", 
     Image = "http://www.internationalcoconut.com/coconuts.jpg", 


    }; 
    Fruit fruit4 = new Fruit 
    { 
     Name = "Hog Plum", 
     Description = "Yellowish plum, related to the Mango", 
     Image = "http://streetsmartbrazil.com/userfiles/image/caj%C3%A1.jpg", 


    }; 

    Fruit fruit5 = new Fruit 
    { 
     Name = "Padoo", 
     Description = "Tree-growing bean pods ", 
     Image = "http://caribfruits.cirad.fr/var/caribfruits/storage/images/fruits_des_antilles/pois_doux/2376-3-fre-FR/pois_doux.jpg", 


    }; 

    fruitiness.Add(fruit1); 
    fruitiness.Add(fruit2); 
    fruitiness.Add(fruit3); 
    fruitiness.Add(fruit4); 
    fruitiness.Add(fruit5); 

    LayoutRoot.DataContext = new CollectionViewSource { Source = fruitiness }; 

     } 

public class Fruit 
{ 

    public Fruit() { } 

    public string Image { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 


} 

private void fruitList_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    { 
     if (FruitListbox.SelectedIndex == -1) return; 
     Fruit data = FruitListbox.SelectedItem as Fruit; 

     switch (data.Description) 
     { 
      case "fruit1": 
       NavigationService.Navigate(new Uri("/Pano.xaml?selectedItem=" + FruitListbox.SelectedIndex, UriKind.Relative)); 
       break; 
      case "fruit2": 
       NavigationService.Navigate(new Uri("/Pano2.xaml?selectedItem=" + FruitListbox.SelectedIndex, UriKind.Relative)); 
       break; 
      case "fruit3": 
       NavigationService.Navigate(new Uri("/Pano3.xaml?selectedItem=" + FruitListbox.SelectedIndex, UriKind.Relative)); 
       break; 
      case "fruit4": 
       NavigationService.Navigate(new Uri("/Pano4.xaml?selectedItem=" + FruitListbox.SelectedIndex, UriKind.Relative)); 
       break; 
      case "fruit5": 
       NavigationService.Navigate(new Uri("/Pano5.xaml?selectedItem=" + FruitListbox.SelectedIndex, UriKind.Relative)); 
       break; 
      default: 
       MessageBox.Show("Please Select From the list!"); 
       break; 




     } 


     FruitListbox.SelectedIndex = -1; 
    } 
} 
} 
} 

사람이 도와주세요 수

다음은 코드?

+0

코드가 전혀 작동합니까? 어떤 페이지로 리디렉션 되었습니까? –

+0

아니요 항목을 클릭 할 때 기본 메시지로 곧바로 이동하지 않았습니다 – user2303699

답변

1

switch 문은 Fruit 개체의 설명을 전환하지만 case 비교 값은 ... 정확히 무엇입니까? 변수 이름 인 것처럼 보입니다.

일반적으로 열거 형 또는 정수로 전환하는 것이 좋습니다.

귀하의 경우 과일 이름을 바꾸거나 data.Name 대신 사용해보십시오.

switch (data.Description) 
    { 
     case "Abricot": 
      ... 
      break; 
     case "Breadfruit": 
      ... 
      break; 
     case "Coconut": 
      ... 
      break; 
     case "Hog Plum": 
      ... 
      break; 
     case "Padoo": 
      ... 
      break; 
     default: 
      MessageBox.Show("Please Select From the list!"); 
      break; 
    } 

그러나 더 나은 방법은 과일 목록의 과일 품목 색인을 사용하는 것입니다.

switch (fruitiness.IndexOf(data)) 
    { 
     case 0: 
      ... 
      break; 
     case 1: 
      ... 
      break; 
     case 2: 
      ... 
      break; 
     case 3: 
      ... 
      break; 
     case 4: 
      ... 
      break; 
     default: 
      MessageBox.Show("Please Select From the list!"); 
      break; 
    } 

물론 더 좋은 방법이 있습니다. 스위치를 완전히 사용하지 않아도됩니다.

var navigateUrlIndex = fruitiness.IndexOf(data) + 1; 
var navigateUrl = String.Format("/Pano{0}.xaml?selectedItem={1}", navigateUrlIndex != 1 ? "" : navigateUrlIndex, FruitListbox.SelectedIndex); 

if (FruitListbox.SelectedIndex != -1) { 
    NavigationService.Navigate(new Uri(navigateUrl, UriKind.Relative)); 
} 

올바른 방향으로 가리켜 주어야 할 것이 있습니다. (테스트하지 않음)

+0

두 번째 옵션을 시도해 보았습니다. 정말 고맙습니다! – user2303699

관련 문제