我在Windows Phone 8中制作天气应用程序。我使用地理定位来获取我的坐标并使用reverseCoordinates将其转换为城市名称,稍后我打算将其用于我用于获取的网址我的天气数据。
这是我的reverseCoordinates的代码,它位于我的mainpage.xaml.cs
中public static string City;
public void reverseGeocode_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
MapAddress geoAddress = e.Result[0].Information.Address;
City = geoAddress.City;
txtStad.Text = City;
}
在我的MainViewModel中,(我必须启用我的城市),我使用的代码是
string City = WeatherApp1.MainPage.City;
现在的问题是,当我调试时,我可以看到解释器首先使用我的mainviewmodel,但是当我使用我的代码时,City的值为null,因为它还没有在Mainpage.xaml中完成的.cs。我能做些什么来解决这个问题吗?
答案 0 :(得分:0)
您需要做的是打开WMAppManifest.xml
并将导航页面的值更改为您需要的页面。例如。的Page1.xaml
答案 1 :(得分:0)
您需要确保当依赖City值的方法仅在具有值时才执行。另一种解决方案是确定一些默认值。你可以利用??像这样的运算符:
string City = WeatherApp1.MainPage.City ?? "London";
它是如何工作的?当WeatherApp1.MainPage.City为null然后??将把城市设置为伦敦。
另请参阅本文,了解如何在页面之间传递值: http://www.geekchamp.com/tips/how-to-pass-data-between-pages-in-windows-phone-alternatives