首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果在不使用MessageDialog的情况下加载页面时隐藏几个控件,应用程序就会崩溃

如果在不使用MessageDialog的情况下加载页面时隐藏几个控件,应用程序就会崩溃
EN

Stack Overflow用户
提问于 2015-07-04 14:41:35
回答 1查看 57关注 0票数 1

我有一个应用程序,在其中我有一个设置,允许用户停止应用程序访问他们的位置。它存储在Windows.Storage.ApplicationData.Current.RoamingSettings.Values"location".中如果位置服务+此设置允许访问,那么我将加载一个打开地图的页面。如果设置允许访问并关闭位置服务,则会显示一条消息,并在页面加载时隐藏一些控件。如果设置是关闭的,那么我只想隐藏没有任何消息的控件。

代码语言:javascript
复制
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的情况下隐藏控件。我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-04 15:29:31

您能更改以下一行吗?

代码语言:javascript
复制
setloc(locator);

至:

代码语言:javascript
复制
await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await setloc(locator); });

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

在我看来,它看起来页面还没有加载,MessageDialog无法显示。Dispatcher.RunAsync应该对此操作进行排队,并且应该在正确的页面初始化之后进行处理。

也是基本的.OnNavigatedTo(..)应在您的位置之前打电话

那是我的猜测-你能提供坠机追踪吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31221869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档