如何在数据绑定上下文中将值从一页传递到另一页

本文关键字:一页 数据绑定 上下文 | 更新日期: 2023-09-27 18:01:44

我正在创建一个应用程序,我们有一个建筑名称的数据模板列表,当点击说"托马斯·戈斯内尔大厅",它会去一个新的页面,TextBlock更改为所选的建筑名称"托马斯·戈斯内尔大厅"。我知道数据绑定可以做到这一点,但是我如何跨两个不同的页面做到这一点呢?

MainPage.xaml

<TextBlock Tap="TextBlock_Tap" Text="{Binding LineOne}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

mainpage . example .cs(当用户点击建筑名称时,它将导航到新页面)

    public void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var building = ((TextBlock)sender).Text; // gets information based on the tapped building
        //perform action based on information about the tapped building 
        if(building == "Thomas Gosnell Hall")
        {
            //MessageBox.Show("08 - (GOS)");
            NavigationService.Navigate(new Uri("/MapLocation.xaml?building=" + building, UriKind.Relative)); // pass the string value to destination page through Uri parameter
        }
        else if(building == "Lyndon Baines Johnson Hall")
        {
            MessageBox.Show("060 - (LBJ)");
        }
    }

MapLocation.xaml

 <TextBlock x:Name="buildingName" Text="Building Name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

maplocation . xml .cs(用户选择建筑物名称后的新页面)

    /**
     * How to: Create the Binding (behind code)
     * Source: http://msdn.microsoft.com/en-us/library/cc838207%28v=vs.95%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
     */
    // Define source object
    public class Building
    {
        public string BuildingName { get; set; }
    }
    public MapLocation()
    {
        InitializeComponent();
        Loaded += MapLocation_Loaded;
       /* // create an instance of the source object
        Building bldg = new Building();
        bldg.BuildingName = building; // value to change depending on user's click
        // create a binding object
        Binding MyBinding = new Binding();
        // set the binding properties on the binding object
        MyBinding.Path = new PropertyPath("BuildingName");
        MyBinding.Mode = BindingMode.OneTime;
        // set the source of the binding by setting the DataContext property
        buildingName.DataContext = bldg;
        // attach the binding to the property of the FrameworkElement
        buildingName.SetBinding(TextBlock.TextProperty, MyBinding);*/
    }
    private void MapLocation_Loaded(object sender, RoutedEventArgs e)
    {
        //throw new NotImplementedException();
        string building;
        if(NavigationContext.QueryString.TryGetValue("building", out building))
        {  
            //load information based on building parameter value
            buildingName.Text = building;
        }
    }
    /*public MapLocation_Loaded()
    {
        string building;
        if(NavigationContext.QueryString.TryGetValue("building", out building))
        {  
            //load information based on building parameter value
        }
    }*/

问题在于这一行bldg.BuildingName = building;,因为它说The name building does not exist in the current context.它存在于MainPage.xaml.cs中,但不在malocation .xaml.cs中。如何根据用户点击的建筑选择将建筑名称绑定到下一页?

如何在数据绑定上下文中将值从一页传递到另一页

我建议通过Uri参数将字符串值传递给目标页面:

public void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var building = ((TextBlock)sender).Text;
    NavigationService.Navigate(new Uri("/MapLocation.xaml?building=" + building, UriKind.Relative));
}

然后处理在目标页加载正确的信息,例如在Loaded页事件处理程序中:

public MapLocation_Loaded()
{
    string building;
    if(NavigationContext.QueryString.TryGetValue("building", out building))
    {  
        //load information based on building parameter value
    }
}

一种选择是将所选值作为公共属性公开在MainPage上。然后其他页面可以读取所设置的值。

另一个选项是在导航方法中将其作为状态传递:

NavigationService.Navigate(new Uri("/MapLocation.xaml", UriKind.Relative), building);

请看这里:http://msdn.microsoft.com/en-us/library/ms591042(v=vs.110).aspx