How to pass value from one page to another page in the context of data binding?

女生的网名这么多〃 提交于 2019-12-12 01:06:27

问题


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?


回答1:


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



回答2:


One option is to expose the selected value on MainPage as a public property. Then other pages can just read whatever value was set.

The other option is to pass it as state in the navigate method:

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

see here: http://msdn.microsoft.com/en-us/library/ms591042(v=vs.110).aspx



来源:https://stackoverflow.com/questions/23464060/how-to-pass-value-from-one-page-to-another-page-in-the-context-of-data-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!