WP7 弹出窗口在与位置一起使用时会延迟打开

发布于 2024-10-19 17:46:41 字数 752 浏览 1 评论 0原文

我有以下代码:

ShowPoup(); 
if (_watcher == null)
{
    _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
    _watcher.MovementThreshold = 15; // use MovementThreshold to ignore noise in the signal
    _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}

if (!_watcher.TryStart(true, TimeSpan.FromSeconds(3)))
{
    MessageBox.Show("Please turn on location services on device under Settings.");
    //HidePopup();
}

我的问题是,直到 _watcher.TryStart() 方法返回后才会出现弹出窗口。弹出窗口的目的是显示一个加载覆盖层,告诉用户应用程序正在执行某些操作。在工作完成后显示它是没有意义的,此时我隐藏弹出窗口,因此用户永远看不到任何内容。

我的整个应用程序中都有此弹出代码,这是我第一次遇到此问题。即使我在调用当前方法之前在单独的方法中调用 ShowPopup() ,它仍然不会显示,直到 _watcher 启动之后。我不知道为什么会发生这种情况。

I have the following code:

ShowPoup(); 
if (_watcher == null)
{
    _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
    _watcher.MovementThreshold = 15; // use MovementThreshold to ignore noise in the signal
    _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}

if (!_watcher.TryStart(true, TimeSpan.FromSeconds(3)))
{
    MessageBox.Show("Please turn on location services on device under Settings.");
    //HidePopup();
}

My problem is that the popup doesn't appear until after the _watcher.TryStart() method returns. The point of the popup is to show a loading overlay to tell the user the app is doing something. It's pointless to have it show after the work is done, at which point I hide the popup, so the user never sees anything.

I have this popup code throughout the app and this is the first time I've encountered this issue. Even if I call ShowPopup() in a separate method before calling the current method, it still doesn't show until after _watcher starts. I'm not sure why this is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡離愁欲言轉身 2024-10-26 17:46:41

看起来您在 TryStart 期间阻塞了 UI 线程。如果您可以将观察者初始化移动到后台线程(例如线程池),那么您可以保持显示“活动”。

像这样的东西:

ShowPoup(); 
if (_watcher == null)
{
    _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
    _watcher.MovementThreshold = 15; // use MovementThreshold to ignore noise in the signal
    _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}

System.Threading.ThreadPool.QueueUserWorkItem((ignored) =>
{
    if (!_watcher.TryStart(true, TimeSpan.FromSeconds(3)))
    {
        Dispatcher.BeginInvoke(() =>
        {
            HidePopup();
            MessageBox.Show("Please turn on location services on device under Settings.");
        }
    });
});

It looks like you are blocking the UI thread during the TryStart. If you can move the watcher initialization to a background thread (e.g. to the threadpool) then you can keep the display "alive".

Something like:

ShowPoup(); 
if (_watcher == null)
{
    _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
    _watcher.MovementThreshold = 15; // use MovementThreshold to ignore noise in the signal
    _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}

System.Threading.ThreadPool.QueueUserWorkItem((ignored) =>
{
    if (!_watcher.TryStart(true, TimeSpan.FromSeconds(3)))
    {
        Dispatcher.BeginInvoke(() =>
        {
            HidePopup();
            MessageBox.Show("Please turn on location services on device under Settings.");
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文