【问题标题】:How to pass value from one page to another page in the context of data binding?如何在数据绑定的上下文中将值从一个页面传递到另一个页面?
【发布时间】:2014-05-05 02:52:04
【问题描述】:

我正在创建一个应用程序,其中我们有一个建筑物名称的数据模板列表,当单击“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 中。如何根据用户点击的建筑物选择将建筑物名称数据绑定到下一页?

【问题讨论】:

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


    【解决方案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
        }
    }
    

    【讨论】:

    • 谢谢!但并没有完全降低 MapLocation 类。我更新了上面的 MapLocation.xaml.cs。这是正确的格式吗?
    • 是的,看起来不错。是否传递了 Uri 参数:../MapLocation.xaml?building=..
    • 是的,我从主页获得!很抱歉问了这么多问题,但是我在MapLocation_Loaded 的if 语句中添加了这行代码,building = buildingName.Text; 文本块仍然没有改变。 ://
    • buildingName MapLocation.xaml 中文本块的名称吗?如果是这样,请尝试:buildingName.Text = building;
    • 终于搞定了,非常感谢!所以 NavigationService 是一种将值从一个页面传递到下一个页面的好方法。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 我将 TextBlock_Tap 属性从 private void 更改为 public void,但 MapLocation 类无法识别 var building
    • 不是 TextBlock_Tap .. 那是一种方法,而不是变量。您想公开用户选择,这在您的发件人控制中。
    • 我不确定我理解你的意思。我究竟把公共财产放在哪里?我尝试将它放在 MainPage 上的几个位置,但在您的代码中抱怨 sender 在当前上下文中不存在。
    • 对不起,我看错了这是从哪里来的。只需使用第二种方法(导航参数)
    • 我从来没有这样想过,有趣! Navigate 参数给出错误No overload method 'Navigate' takes 2 arguments. 我在这里遗漏了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2015-04-04
    相关资源
    最近更新 更多