我有一个应用程序,在其中我有一个设置,允许用户停止应用程序访问他们的位置。它存储在Windows.Storage.ApplicationData.Current.RoamingSettings.Values"location".中如果位置服务+此设置允许访问,那么我将加载一个打开地图的页面。如果设置允许访问并关闭位置服务,则会显示一条消息,并在页面加载时隐藏一些控件。如果设置是关闭的,那么我只想隐藏没有任何消息的控件。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
.....
// MUST ENABLE THE LOCATION CAPABILITY!!!
var locator = new Geolocator();
locator.DesiredAccuracyInMeters = 50;
locator.ReportInterval = (uint)TimeSpan.FromSeconds(15).TotalMilliseconds;
setloc(locator);
this.navigationHelper.OnNavigatedTo(e);
}
public async void setloc(Geolocator locator)
{
if (locator.LocationStatus != PositionStatus.Disabled && (bool)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["location"]==true)
{
var position = await locator.GetGeopositionAsync();
await MyMap.TrySetViewAsync(position.Coordinate.Point, 16D);
....
return;
}
else if (locator.LocationStatus == PositionStatus.Disabled && (bool)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["location"] == true)
{
MessageDialog msgbox = new MessageDialog("Location Services are turned off. Please turn them on to save Location while saving a Tip", "Location Unavailable");
await msgbox.ShowAsync();
savebutton.Visibility = Visibility.Collapsed;
myMapBlock.Visibility = Visibility.Collapsed;
return;
}
***// MessageDialog msgbox1 = new MessageDialog("Location Services are turned off. Please turn them on to save Location while saving a Tip", "Location Unavailable");
// await msgbox1.ShowAsync();***
savebutton.Visibility = Visibility.Collapsed;
myMapBlock.Visibility = Visibility.Collapsed;
}当设置为真(真)时,一切都很好,但当它关闭(假)时,会发生一些奇怪的事情。上面的代码不工作。这会导致应用程序崩溃,但是当我取消注释代码中的*部分时,消息就会显示出来,页面就会被正确加载。如果我只是试图隐藏myMapBlock和保存按钮而不使用MessageDialog,它就会崩溃。我想在不使用MessageDialog的情况下隐藏控件。我怎么能这么做?
发布于 2015-07-04 15:29:31
您能更改以下一行吗?
setloc(locator);至:
await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await setloc(locator); });(将void方法的签名更改为Task)
在我看来,它看起来页面还没有加载,MessageDialog无法显示。Dispatcher.RunAsync应该对此操作进行排队,并且应该在正确的页面初始化之后进行处理。
也是基本的.OnNavigatedTo(..)应在您的位置之前打电话
那是我的猜测-你能提供坠机追踪吗?
https://stackoverflow.com/questions/31221869
复制相似问题