You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在UWP页面间传递RestaurantsDataInfo实体数据?

Pass Full RestaurantsDataInfo Object to DetailRestaurant Page

Got it, let's adjust your code to pass the entire RestaurantsDataInfo object to the detail page—this way you'll have all the restaurant's data available to display, no need to pass individual properties. Here's the breakdown:

Step 1: Update the ItemClick Navigation Logic

Your current code only passes the picture property, but we can send the whole selected restaurant object. We can also remove that unnecessary switch statement since the navigation logic is the same for all restaurants:

private void ListOfRestaurant_ItemClick(object sender, ItemClickEventArgs e)
{
    // Get the full selected restaurant object
    var selectedRestaurant = (RestaurantsDataInfo)e.ClickedItem;
    
    // Keep your existing connected animation setup
    var container = ListOfRestaurant.ContainerFromItem(selectedRestaurant) as GridViewItem;
    if (container != null)
    {
        var root = (FrameworkElement)container.ContentTemplateRoot;
        var image = (UIElement)root.FindName("ResPic");
        ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("Image", image);
    }

    // Navigate to DetailRestaurant and pass the entire object
    Frame.Navigate(typeof(DetailRestaurant), selectedRestaurant);
}

Step 2: Receive the Object in DetailRestaurant Page

In your DetailRestaurant code-behind, retrieve the passed object in the OnNavigatedTo method. You can then bind it to your UI elements either via x:Bind or setting the DataContext:

using Windows.UI.Xaml.Navigation;

public sealed partial class DetailRestaurant : Page
{
    // Property for x:Bind support (make it public)
    public RestaurantsDataInfo CurrentRestaurant { get; private set; }

    public DetailRestaurant()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        
        // Check if the parameter is the expected object type
        if (e.Parameter is RestaurantsDataInfo restaurant)
        {
            CurrentRestaurant = restaurant;
            
            // Optional: Start the connected animation
            var animation = ConnectedAnimationService.GetForCurrentView().GetAnimation("Image");
            if (animation != null)
            {
                // Replace "DetailResPic" with the name of your Image control in the detail page
                animation.TryStart(DetailResPic);
            }
        }
    }
}

Step 3: Bind UI Elements in DetailRestaurant XAML

Now you can bind all the restaurant properties to your detail page's UI elements using x:Bind (or traditional binding if you prefer):

<StackPanel Padding="20">
    <Image x:Name="DetailResPic" 
           Source="{x:Bind CurrentRestaurant.picture}" 
           Stretch="Fill" 
           Height="250" 
           Width="250"
           Margin="0 0 0 10"/>
    
    <TextBlock Text="{x:Bind CurrentRestaurant.name}" 
               FontSize="28" 
               Foreground="Brown"
               Margin="0 0 0 5"/>
    
    <StackPanel Orientation="Horizontal" Margin="0 0 0 5">
        <TextBlock Text="{x:Bind CurrentRestaurant.state}" 
                   Foreground="Green"
                   Margin="0 0 10 0"/>
        <TextBlock Text="{x:Bind CurrentRestaurant.address}" 
                   Foreground="BlueViolet"/>
    </StackPanel>
    
    <TextBlock Text="{x:Bind CurrentRestaurant.openclosetime}" 
               Foreground="Salmon"/>
</StackPanel>

Key Notes

  • Flexibility: Passing the entire object means you won't have to update navigation code if you add new properties to RestaurantsDataInfo later.
  • Type Safety: Casting the e.Parameter to RestaurantsDataInfo ensures you're working with the correct data type.
  • Connected Animation: We kept your existing animation setup—just make sure the Image control name in the detail page matches what you use in animation.TryStart().

内容的提问来源于stack exchange,提问作者Boommer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:37:41