Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果在加载页面时未使用MessageDialog隐藏一些控件,应用程序将崩溃_C#_Xaml_Windows Phone 8.1 - Fatal编程技术网

C# 如果在加载页面时未使用MessageDialog隐藏一些控件,应用程序将崩溃

C# 如果在加载页面时未使用MessageDialog隐藏一些控件,应用程序将崩溃,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,我有一个应用程序,其中我有一个允许用户停止应用程序访问其位置的设置。这存储在Windows.Storage.ApplicationData.Current.RoamingSettings.Values[“location”]中。如果位置服务+此设置允许访问,则我加载一个打开地图的页面。如果设置允许访问且位置服务关闭,则会显示一条消息,并在页面加载时隐藏一些控件。如果设置为off,那么我只想隐藏控件而不显示任何消息 protected override void OnNavigatedTo(Na

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

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;

    }
当该设置为on(真)时一切正常,但当它为off(假)时,会发生奇怪的事情。 上面的代码不起作用。它会导致应用程序崩溃,但当我在代码中取消对***内的部分的注释时,会显示消息并正确加载页面。如果我只是试图隐藏myMapBlock和savebutton而不使用MessageDialog,它就会崩溃。
我想在不使用MessageDialog的情况下隐藏控件。我怎样才能做到这一点?

您能否更改以下行:

setloc(locator);
致:

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

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

另外,base.OnNavigatedTo(..)调用应该在location messagedialog代码之前进行


这是我的猜测-你能提供一个崩溃堆栈跟踪吗?

它起作用了。谢谢您忘记将lambda表达式标记为异步。正确的代码是-。
await-Windows.UI.Core.corewindown.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,async()=>{await-setloc(locator);})@NikhilGupta已修复-我刚刚“通过内存”编写了该代码-)
await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await setloc(locator); });