将参数从一个页面传递到另一个页面,使用图钉和对象



我正在为Windows Phone 7构建一个应用程序,我需要在地图上打印图钉,所以我做到了。我从数据库响应中读取数据。

问题是...每个图钉都意味着一个地方,但我不知道如何将地点添加到图钉或将业务属性发送到另一个页面,我将在那里将它们写在一些文本块中。

void app_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        Place business = new Place();
        if (e.Error == null)
        {
            XDocument feedXml = XDocument.Parse(e.Result);
            foreach (var properties in feedXml.Descendants("RESULT"))
            {
                Pushpin p = new Pushpin();
                p.Location = new GeoCoordinate((double)properties.Element("LATITUDE"), (double)properties.Element("LONGITUDE"));
                p.Content = properties.Element("NAME").Value;
                p.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(pushPin_Tap);
                p.DoubleTap += new EventHandler<System.Windows.Input.GestureEventArgs>(pushPin_DoubleTap);
                business.Id = (int)properties.Element("ID");
                business.Category = properties.Element("CATEGORY").Value;
                business.Name = properties.Element("NAME").Value;
                business.Street = properties.Element("STREET").Value;
                business.City = properties.Element("CITY").Value;
                business.Neighborhood = properties.Element("NEIGHBORHOOD").Value;
                business.State = properties.Element("STATE").Value;
                business.ZipCode = properties.Element("ZIPCODE").Value;
                business.Country = properties.Element("COUNTRY").Value;
                business.Hours = properties.Element("HOURS").Value;
                business.Description = properties.Element("DESCRIPTION").Value;
                business.Images = properties.Element("IMAGES").Value;
                business.Latitude = (double)properties.Element("LATITUDE");
                business.Longitude = (double)properties.Element("LONGITUDE");
                business.Payment = "Nope";
                business.Distance = properties.Element("DISTANCE").Value;

                map1.Children.Add(p);

            }
        }
    }

然后我使用该方法将一些参数发送到其他页面,但需要的是图钉值,而不是业务。

void pushPin_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        e.Handled = true;

        var businessinfo = ((FrameworkElement) sender).Tag as Place;
        /*if (businessinfo != null)
        {
            string name = businessinfo.Name;
            string category = businessinfo.Category;
            string address = businessinfo.Street;
            string distance = businessinfo.Distance;
            string payment = businessinfo.Payment;
            string hours = businessinfo.Hours;
            NavigationService.Navigate(new Uri(
            "/BusinessInfoPage.xaml?name=" + name + 
            "&category=" + category +
            "&address=" + address + 
            "&distance=" + distance + 
            "&payment=" + payment + 
            "&hours=" + hours, UriKind.Relative));
        }
         */
        NavigationService.Navigate(new Uri("/BusinessInfoPage.xaml?name=Oxxo&category=Convenience&address=Somewhere&distance=.005km&payment=Nothing&hours=8am to 9pm", UriKind.Relative));

    }

有什么帮助或建议吗?谢谢:)

您可以使用PhoneApplicationService.Current.State在页面之间存储和检索数据。例如,在页面 A 中:

Place loc = new Place();
//fill the Place properties
PhoneApplicationService.Current.State["place"]=loc;
//Navigate to Page B

在 B 页的"导航至"中:

Place place = PhoneApplicationService.Current.State["place"];

您可能希望在其中添加一些异常捕获,并确保键"引脚"存在于状态集合中。

Yo 还可以在 App.xaml.cs 中使用公共属性。

public Place place {get;set;}

在 A 页中:

var obj = App.Current as App;
obj.place = aPlace; //assumming aPlace is already created

在 B 页中,导航到:

var obj = App.Current as App;
aPlace = obj.place; //assuming you have aPlace declared before

相关内容

  • 没有找到相关文章

最新更新