0

我有一个水果应用程序的列表框。我希望列表框中的每个项目在被选中时都能将您带到一个不同的页面,该页面告诉用户更多关于水果的信息。我知道导航到一页的代码,但是在使用 switch case 语句导航到几页时遇到了麻烦。这是代码:

主页.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;
    }
}
}
}

有人可以帮忙吗?

4

1 回答 1

1

您的 switch 语句切换了 Fruit 对象的描述,但大小写比较值是……究竟是什么?看起来它们是变量名。

通常,最好使用枚举或整数进行切换。

在您的情况下,您可以尝试使用切换您的水果名称,或者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));
 }

类似的事情应该为您指明正确的方向。(未测试)

于 2013-10-10T18:40:26.473 回答