如何在页面之间传输数据?

时间:2018-05-18 14:25:09

标签: c# xaml uwp

我知道这个问题已被问到,但我的情况有点不同。 我的所有数据都保存在C#页面中,如下所示:

    public class RestaurantsDataInfo
{
    public string name { get; set; }
    public string picture { get; set; }
    public string openclosetime { get; set; }
    public string state { get; set; }
    public string address { get; set; }
}
public class RestaurantManager
{
    public static List<RestaurantsDataInfo> GetRestaurants()
    {
        var restaurant = new List<RestaurantsDataInfo>();
        restaurant.Add(new RestaurantsDataInfo { name = "Pizzeria", address = "22 - Vo Van Ngan Str", openclosetime = "8 AM to 9 PM", picture = "Assets/Cover7.jpg", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "BBQ Kings", address = "55 - Vo Van Ngan Str", openclosetime = "8 AM to 10 PM", picture = "Assets/Cover6.jpg", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "Lotteria", address = "200 - Vo Van Ngan Str", openclosetime = "8 AM to 8:30 PM", picture = "Assets/Cover5.png", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "KFC", address = "90 - Vo Van Ngan Str", openclosetime = "8 AM to 8 PM", picture = "Assets/Cover4.png", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "Mugsy Dogs", address = "112 - Linh Trung Str", openclosetime = "8 AM to 8:30 PM", picture = "Assets/Cover9.png", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "American Hot Dog", address = "04 - Duong So 5 Str", openclosetime = "8 AM to 8:30 PM", picture = "Assets/Cover10.jpg", state = "Online" });
        restaurant.Add(new RestaurantsDataInfo { name = "Burger King", address = "404 - Duong So 6 Str", openclosetime = "24 hours", picture = "Assets/Cover11.png", state = "Online" });
        return restaurant;
    }
}

以下是绑定所有数据的页面:

            <GridView Grid.Row="1" x:Name="ListOfRestaurant" IsItemClickEnabled="True" ItemClick="ListOfRestaurant_ItemClick" ScrollViewer.IsScrollInertiaEnabled="True" ItemsSource="{x:Bind RES}">
            <GridView.ItemTemplate>
                <DataTemplate x:DataType="data:RestaurantsDataInfo">
                    <StackPanel Width="197" Height="240">
                        <Image x:Name="ResPic" Source="{x:Bind picture}" Stretch="Fill" Height="150" Width="150"/>
                        <TextBlock x:Name="RestaurantName" Text="{x:Bind name}" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Brown"/>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{x:Bind state}" Margin="3" Foreground="Green"/>
                            <TextBlock Text="{x:Bind address}" Margin="3" Foreground="BlueViolet"/>
                        </StackPanel>
                        <TextBlock Text="{x:Bind openclosetime}" HorizontalAlignment="Center" Margin="3" Foreground="Salmon"/>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

以下是代码的后端:

        private void ListOfRestaurant_ItemClick(object sender, ItemClickEventArgs e)
    {
        var item = (RestaurantsDataInfo)e.ClickedItem;
        var title = item.name;
        var container = ListOfRestaurant.ContainerFromItem(e.ClickedItem) as GridViewItem;
        if (container != null)
        {
            //find the image
            var root = (FrameworkElement)container.ContentTemplateRoot;
            var image = (UIElement)root.FindName("ResPic");



            //prepare the animation
            ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("Image", image);
        }
        switch (title)
        {
            case "KFC":

                Frame.Navigate(typeof(DetailRestaurant), _storeditem = item.picture);
                break;
        }
    }

现在我想导航到“DetailRestaurant”并向该页面发送RestaurantDataInfo中的信息,以便详细信息页面可以显示我点击的餐厅的所有内容

1 个答案:

答案 0 :(得分:0)

最好的解决方案是传递&#34; ID&#34;您想要显示以下数据的餐厅:

Frame.Navigate(typeof(DetailRestaurant),"KFC");

现在在另一个页面上OnNavigatedTo访问参数,然后访问static集合中的详细信息:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var info = RestaurantManager.GetRestaurants().
        FirstOrDefault( r => r.name == e.Parameter?.ToString());
}