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

fly*_*lip 4 c# wpf mvvm wpftoolkit

好吧,我已经在这个问题上挣扎了相当长一段时间,并且阅读了一篇又一篇文章,试图了解这个问题。我正在尝试实施忙碌指示器,但只取得了部分成功。我有一个用 BusyIndi​​cator 包装的 Shell 视图,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

遵循 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
}
Run Code Online (Sandbox Code Playgroud)

接下来,我使用一个名为 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;
}
Run Code Online (Sandbox Code Playgroud)

最后,我在代码中触发忙碌指示器,如下所示:

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;
        });
Run Code Online (Sandbox Code Playgroud)

总而言之,可以从应用程序中的任何位置调用 StateManager。当 StateManager.IsBusy 属性更改时,将触发在 ShellViewModel 中订阅的事件。发生这种情况时,ShellViewModel 中的 IsBusy 属性将设置为 true,从而导致显示 IsBusy 指示器。从 ShellViewModel 中的 IsBusy 正在设置并且繁忙指示器甚至按预期显示的意义上来说,逻辑工作正常。不正常的是忙碌指示器没有动画。它只是保持停滞状态。我觉得这是一个线程问题,但要么是我对 UI 线程的理解发生了倾斜,要么是我没有看到对您来说显而易见的东西。我意识到 IsBusy 逻辑不能与 UI 位于同一线程上,但我认为由于我的逻辑发生在视图模型中,所以它不应该成为问题。我错了吗?有什么想法或想法吗?另一件可能有用的事情是:我什至无法让指示器显示并创建了这篇已解决的帖子:How to Implement busy Indicator in application shell

fly*_*lip 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
}
Run Code Online (Sandbox Code Playgroud)

首先,请注意,我创建了扩展 EventArgs 的 BusyActionEventArgs 类。它被放入我的库项目中,该项目依赖于我的解决方案中的每个项目:

public class BusyActionEventArgs : EventArgs
{
    public Action IsBusyAction { get; set; }
    public string BusyMessage { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

最后,我的 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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我为 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);
Run Code Online (Sandbox Code Playgroud)

请注意我从操作范围中删除的代码。此代码是 UI 代码,如果在范围内,将会导致错误。

我希望这可以帮助那些可能想要实施类似的事情或遇到麻烦的人。感谢 stackoverflow 就存在于此。有时说出来(或打字)会产生不同的效果。