如果在不使用MessageDialog加载页面时隐藏了一些控件,则应用程序崩溃

本文关键字:控件 崩溃 应用程序 隐藏 MessageDialog 加载 如果 | 更新日期: 2023-09-27 18:00:58

我有一个应用程序,其中有一个设置,允许用户停止应用程序对其位置的访问。这存储在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;
    }

当该设置打开(true(时一切都很好,但当它关闭(false(时,会发生一些奇怪的事情。上面的代码不起作用。它会导致应用程序崩溃,但当我取消注释代码中***内的部分时,会显示消息,页面也会正确加载。如果我只是试图在不使用MessageDialog的情况下隐藏myMapBlock和保存按钮,它就会崩溃。我想在不使用MessageDialog的情况下隐藏控件。我该怎么做?

如果在不使用MessageDialog加载页面时隐藏了一些控件,则应用程序崩溃

您能更改以下行吗:

setloc(locator);

至:

await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await setloc(locator); });

(将void setloc方法的签名更改为Task(

在我看来,页面似乎还没有加载,MessageDialog无法显示。Dispatcher.RunAsync应将此操作排入队列,并应在正确的页面初始化后进行处理。

此外,基本.OnNavigatedTo(..(调用应该在您的位置消息对话框代码之前进行。

这是我的猜测——你能提供一个碰撞堆叠比赛吗?