【问题标题】:Show RadBusyIndicator during Frame Navigation WPF在框架导航 WPF 期间显示 RadBusyIndi​​cator
【发布时间】:2015-09-13 07:39:56
【问题描述】:

对于我们的应用程序,我们需要在导航期间显示忙碌指示符,因为某些控件需要时间来加载。通常对于长时间运行的操作,我会创建一个单独的任务,并会在 UI 线程中触发忙碌指示器,但在这种情况下我不能这样做。

一旦忙碌指示器开始旋转,它就会受到同样在 ui 线程中执行的 Frame.Source 或 Frame.Navigate 的干扰。忙把指标藏了起来。

以下是我用于导航的一段代码。这是作为单独的任务执行的。

public virtual void NavigateTo(string pageKey, object parameter)
        {


                Frame frame = null;
                Action actionToExecuteOnUIContext = () =>
                    {

                        frame = GetDescendantFromName(Application.Current.MainWindow, "ContentFrame") as Frame;
                        if (frame != null)
                        {

                            frame.LoadCompleted -= frame_LoadCompleted;
                            frame.LoadCompleted += frame_LoadCompleted;

                                frame.Source = _pagesByKey[pageKey];
                                frame.DataContext = parameter;



                        }


                    };

                Application.Current.Dispatcher.BeginInvoke(actionToExecuteOnUIContext, DispatcherPriority.Normal);



            }

        }

任何解决此问题的方法或替代方法

WPF & RadBusyIndi​​cator & .Net 4.5

【问题讨论】:

    标签: wpf task ui-thread busyindicator


    【解决方案1】:

    为了显示忙碌指示符,您必须使用后台线程,因为导航也需要 UI 线程。

    我建议使用这样的东西:

        protected async void Navigation(string pageKey, object parameter)
        {
    
            await NavigateTo(pageKey,parameter).ContinueWith((t) =>
            {// do UI stuff
                Dispatcher.CurrentDispatcher.InvokeAsync(delegate
                {
                    IsBusy = false;
                });
    
            },
                //sync with UI thread
            TaskScheduler.FromCurrentSynchronizationContext());
        }
    
        /// <summary>
        /// This method needs to be async to perform async binding
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private async Task NavigateTo(string pageKey, object parameter)
        {
            //your async method
        }
    

    其中 IsBusy 是 BusyIndi​​cator 的值。您必须在导航开始时将其设置为 true。

    希望这篇文章对你有所帮助@!

    【讨论】:

    • 没有帮助。我的帖子有所不同,当您在 UI 线程中运行忙碌指示器时,在 UI 线程上执行一些其他操作会再次阻塞忙碌指示器。那么如何在不阻塞繁忙指示器的情况下加载帧
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多