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

时间:2014-05-05 02:52:04

标签: c# xaml windows-phone-8 data-binding datacontext

我正在创建一个应用程序,其中我们有一个建筑物名称的数据模板列表,当点击“Thomas Gosnell Hall”时,它将转到一个新页面,其中TextBlock已更改为所选建筑物名称“Thomas Gosnell”大厅”。我知道数据绑定用于执行此操作,但如何在两个不同的页面上执行此操作?

MainPage.xaml中

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

MainPage.xaml.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.xaml.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中,但不存在于MapLocation.xaml.cs中。我如何根据用户点击选择构建到下一页来绑定建筑物名称?

2 个答案:

答案 0 :(得分:1)

我建议通过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
    }
}

答案 1 :(得分:0)

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

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

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

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