繁体   English   中英

如何选择Windows Phone 8中首先启动哪个页面

[英]How do I choose which Page starts first in Windows Phone 8

我正在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中尚未完成。 有什么我可以解决的吗?

您需要做的是打开WMAppManifest.xml并将“导航页面”的值更改为所需的任何页面。 例如。 的Page1.xaml

在此处输入图片说明

您需要确保仅在具有值的情况下执行依赖于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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM