如何在数据绑定的上下文中将值从一个页面传递到另一个页面? [英] How to pass value from one page to another page in the context of data binding?

查看:229
本文介绍了如何在数据绑定的上下文中将值从一个页面传递到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



MainPage.xaml

 < TextBlock Tap =TextBlock_TapText ={Binding LineOne}TextWrapping =NoWrapStyle ={StaticResource PhoneTextExtraLargeStyle}/ > 

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

  public void TextBlock_Tap(object sender,System.Windows.Input.GestureEventArgs e)
{

var building =((TextBlock)sender).Text; //获取基于轻敲建筑物的信息

//根据有关轻敲的建筑物的信息执行操作
if(building ==Thomas Gosnell Hall)
{
//MessageBox.Show(\"08 - (GOS));
NavigationService.Navigate(new Uri(/ MapLocation.xaml?building =+ building,UriKind.Relative)); //通过Uri参数
}
将字符串值传递给目标页面$ else else(建筑==Lyndon Baines Johnson Hall)
{
MessageBox.Show(060 - (LBJ));
}

}

MapLocation.xaml

 < TextBlock x:Name =buildingNameText =Building NameMargin =9,-7 ,0,0Style ={StaticResource PhoneTextTitle1Style}/> 

MapLocation.xaml.cs (用户选择的新页面建筑名称)

  / ** 
*如何:创建绑定(代码后面)
*来源:http://msdn.microsoft.com/en-us/library/cc838207%28v=vs.95%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
* /
//定义源对象
public class Building
{
public string BuildingName {get;组; }
}

public MapLocation()
{
InitializeComponent();
加载+ = MapLocation_Loaded;

/ * //创建源对象的实例
构建bldg = new Building();
bldg.BuildingName = building; //根据用户的点击更改值

//创建绑定对象
绑定MyBinding = new Binding();

//设置绑定对象上的绑定属性
MyBinding.Path = new PropertyPath(BuildingName);
MyBinding.Mode = BindingMode.OneTime

//通过设置DataContext属性设置绑定的源
buildingName.DataContext = bldg;

//将绑定绑定到FrameworkElement的
buildingName.SetBinding(TextBlock.TextProperty,MyBinding); * /
}

private void MapLocation_Loaded(object sender,RoutedEventArgs e)
{
// throw new NotImplementedException();
字符串构建;
if(NavigationContext.QueryString.TryGetValue(building,out building))
{
//根据建筑参数值加载信息
buildingName.Text = building;
}
}

/ * public MapLocation_Loaded()
{
string building;
if(NavigationContext.QueryString.TryGetValue(building,out building))
{
//根据建筑参数值加载信息
}
} * /

问题在于这一行 bldg.BuildingName = building; ,因为它表示名称构建在当前上下文中不存在。它存在于MainPage.xaml.cs中,但不存在于MapLocation.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));
}

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

  public MapLocation_Loaded()
{
字符串构建;
if(NavigationContext.QueryString.TryGetValue(building,out building))
{
//根据建筑参数值加载信息
}
}


I am creating an app where we have a Data Template list of building names and when clicked on say "Thomas Gosnell Hall", it will go to a new page with the TextBlock changed to that of the selected building name "Thomas Gosnell Hall". I know data binding is used to do this, but how do I do it across two different pages?

MainPage.xaml

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

MainPage.xaml.cs (When a user taps on the building name, it will navigate to new page)

    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 (The new page after the user selected the building name)

    /**
     * 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
        }
    }*/

The problem lies within this line bldg.BuildingName = building; as it says The name building does not exist in the current context. It exists in the MainPage.xaml.cs, but not in MapLocation.xaml.cs. How do I data bind the building name depending on the user's tapped choice of building onto the next page?

解决方案

I would suggest to pass the string value to destination page through Uri parameter :

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));
}

Then handle loading correct informations in the destination page, for example in page Loaded event handler :

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

这篇关于如何在数据绑定的上下文中将值从一个页面传递到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆