如何正确实现WPF扩展工具包中的忙碌指示器。

4

好的,我已经为这个问题苦苦挣扎了很长时间,并阅读了一篇又一篇的文章,试图对这个问题有所了解。我正在尝试实现繁忙指示器,但只有部分成功。我有一个Shell视图,我已经用BusyIndicator包装了它,如下所示:

<Window x:Class="Foundation.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Library.Controls.Views;assembly=Library"
    xmlns:l="clr-namespace:Library.StaticClasses"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    Name="ShellView"
    Style="{StaticResource BusyWindowStyle}"
    SourceInitialized="Window_SourceInitialized"
    DataContext="{StaticResource ShellVM}">

    <xctk:BusyIndicator x:Name="ShellBusyIndicator" IsBusy="{Binding IsBusy}"  DisplayAfter="0">
        <Grid>
            <!--Content Here -->
        </Grid>
    </xctk:BusyIndicator>

遵循MVVM模式,我有一个看起来像这样的ShellViewModel:

public class ShellViewModel : ViewModelBase
{
    #region constructor(s)

    public ShellViewModel()
    {
        StateManager.IsBusyChange += new StateManager.IsBusyHandler(IsBusyEventAction);
    }

    #endregion constructor(s)

    #region properties

    private bool _IsBusy;
    public bool IsBusy
    {
        get
        {
            return _IsBusy;
        }
        set
        {
            if (_IsBusy != value)
            {
                _IsBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }
    }

    private string _IsBusyMessage;
    public string IsBusyMessage
    {
        get
        {
            return _IsBusyMessage;
        }
        set
        {
            if (_IsBusyMessage != value)
            {
                _IsBusyMessage = value;
                OnPropertyChanged("IsBusyMessage");
            }
        }
    }

    #endregion properties

    #region actions, functions, and methods

    private void IsBusyEventAction(object sender, EventArgs e)
    {
        if (StateManager.IsBusy)
        {
            this.IsBusy = true;
            this.IsBusyMessage = StateManager.IsBusyMessage;
        }
        else
        {
            this.IsBusy = false;
        }
    }

    #endregion actions, functions, and methods
}

下一步,我正在使用一个静态类叫做StateManager,我可以从任何地方调用它来触发忙指示器:
public static class StateManager
{
    public static String IsBusyMessage { get; set; }

    public static void IsBusyProcess(Action action)
    {
        IsBusyProcess(action, "Processing...");
    }

    public static void IsBusyProcess(Action action, String isBusyMessage)
    {
        IsBusyMessage = isBusyMessage;
        IsBusy = true;
        Application.Current.Dispatcher.Invoke(action, System.Windows.Threading.DispatcherPriority.Background);
        IsBusy = false;
    }

    private static bool _IsBusy;
    public static bool IsBusy
    {
        get
        {
            return _IsBusy;
        }
        set
        {
            if (_IsBusy != value)
            {
                _IsBusy = value;

                IsBusyChange(null, null);
            }
        }
    }

    public delegate void IsBusyHandler(object sender, EventArgs e);

    public static event IsBusyHandler IsBusyChange;
}

最后,我会在代码中这样触发繁忙指示器:
StateManager.IsBusyProcess(() =>
        {
            this.Validate();

            if (!HasValidationErrors)
            {
                if (this.CustomerControlVM.SaveCustomer() != 0)
                {
                    VehicleControlVM.VehicleModel.CustomerID = this.CustomerControlVM.CustomerModel.CustomerID;
                    this.VehicleControlVM.SaveVehicle();

                    ComplaintsView complaintsControl = new ComplaintsView();

                    (complaintsControl.DataContext as ComplaintsViewModel).CurrentVehicle = this.VehicleControlVM.VehicleModel;
                    (complaintsControl.DataContext as ComplaintsViewModel).CurrentCustomer = this.CustomerControlVM.CustomerModel;
                    StateManager.LoadView(complaintsControl, PageTransitionType.SlideLeft);
                }
            }

            StateManager.IsBusy = false;
        });

总之,StateManager可在应用程序的任何地方调用。当StateManager.IsBusy属性被更改时,将触发一个事件,ShellViewModel中订阅此事件。当这种情况发生时,ShellViewModel中的IsBusy属性设置为True,假定会导致IsBusy指示器显示。从某种意义上说,逻辑是正常工作的,因为ShellViewModel中的IsBusy被设置了,并且繁忙指示器也按预期显示出来了。不正确的是繁忙指示器没有动画效果。它只是停滞不前。我觉得这是线程问题,但我的UI线程理解可能有误或者我没有看到一些对您来说很明显的东西。我知道IsBusy逻辑不能在与UI相同的线程上,但我认为既然我的逻辑发生在视图模型中,就不应该成为问题。我错了吗?有什么想法或思路吗?还有一件可能有帮助的事情:我甚至无法使指示器显示并创建了这个已解决的帖子:如何在应用程序壳中实现繁忙指示器

那么正在运行需要繁忙指示器的另一个进程,它也在单独的线程上吗? - Felix Castor
嗨Felix,感谢您的回复。长时间运行的进程应该在与ShellViewModel相同的线程上,除非我忽略了什么。我尝试使用线程窗口(我以前从未使用过),但它似乎表明我的ShellView和ShellViewModel都在主线程中。我不确定这是如何可能的,所以我不信任它,更不信任自己的观点。 - flyNflip
还有一篇不错的文章在这里:http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-busyindicator/ - George Birbilis
1个回答

5
好的,我已经实现了繁忙指示器。 这是一个相当艰巨的任务,但是我在这个过程中学到了一些东西。 至少我有点可以展示的东西。 虽然我没有弄清楚为什么我的代码不像我发布的那样工作,但我发现我试图在动作中执行UI相关代码肯定会出问题。 我不是100%确定,因为我进行了重写,而且我不够自信去恢复我的代码并进行测试。 但是,问题得到了解答,并且以下是我实现繁忙指示器的方法:

首先,我的视图中除了属性设置和绑定的IsBusy之外,没有任何改变。 我最终将事件订阅从视图模型移动到视图的代码后面。 我的视图的代码后面看起来像这样:

public partial class Shell : Window
{
    #region Constructor(s)

    public Shell()
    {
        InitializeComponent();
        StateManager.IsBusyChange += new StateManager.IsBusyHandler(InitiateIsBusy);
    }

    #endregion Constructor(s)

    #region Event Actions

    public void InitiateIsBusy(object sender, BusyActionEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();

        worker.DoWork += (o, ea) =>
        {
            e.IsBusyAction.Invoke();
            //Dispatcher.Invoke((Action)(() => e.IsBusyAction()));
        };
        worker.RunWorkerCompleted += (o, ea) =>
        {
            this.ShellBusyIndicator.IsBusy = false;
        };

        this.ShellBusyIndicator.BusyContent = e.BusyMessage;
        this.ShellBusyIndicator.IsBusy = true;

        worker.RunWorkerAsync();
    }

    #endregion Event Actions
}

首先要注意,我创建了一个继承自EventArgs的BusyActionEventArgs类。该类位于我的Library项目中,它是解决方案中每个项目的依赖项:

public class BusyActionEventArgs : EventArgs
{
    public Action IsBusyAction { get; set; }
    public string BusyMessage { get; set; }
}

最后,我的StateManager现在看起来像这样:
public static class StateManager
{
    public static void IsBusyProcess(Action action)
    {
        IsBusyProcess(action, "Processing...");
    }

    public static void IsBusyProcess(Action action, String isBusyMessage)
    {
        BusyActionEventArgs args = new BusyActionEventArgs()
        {
            IsBusyAction = action,
            BusyMessage = isBusyMessage
        };

        IsBusyChange(null, args);
    }

    public delegate void IsBusyHandler(object sender, BusyActionEventArgs e);

    public static event IsBusyHandler IsBusyChange;
}

请注意,我创建了一个IsBusyProcess方法的过载,这样我就可以发送自定义的繁忙消息。现在,如果我修改我的问题代码片段中实际使用繁忙指示器的代码,它看起来像这样:
StateManager.IsBusyProcess(() =>
        {
            this.Validate();

            if (!HasValidationErrors)
            {
                if (this.CustomerControlVM.SaveCustomer() != 0)
                {
                    VehicleControlVM.VehicleModel.CustomerID = this.CustomerControlVM.CustomerModel.CustomerID;
                    this.VehicleControlVM.SaveVehicle();
                }
            }
        });

        ComplaintsView complaintsControl = new ComplaintsView();

        (complaintsControl.DataContext as ComplaintsViewModel).CurrentVehicle = this.VehicleControlVM.VehicleModel;
        (complaintsControl.DataContext as ComplaintsViewModel).CurrentCustomer = this.CustomerControlVM.CustomerModel;
        StateManager.LoadView(complaintsControl, PageTransitionType.SlideLeft);

请注意我从操作范围中删除的代码。这段代码是UI代码,如果在范围内会导致错误。
希望这能帮助其他想要实现类似功能或遇到问题的人。感谢stackoverflow存在。有时候说出来(或打出来)就有所不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接