c# - 如何从 WPF Extended Toolkit 正确实现 Busy Indicator

标签 c# wpf mvvm wpftoolkit

好的,我已经为这个问题苦苦挣扎了很长一段时间,并且阅读了一篇又一篇文章,试图对这个问题有所了解。我正在尝试实现 Busy Indicator,但只取得了部分成功。我有一个 Shell View ,我已经像这样用 BusyIndi​​cator 包装了它:

<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 在同一个线程上,但我认为由于我的逻辑发生在 View 模型中,所以这应该不是问题。我错了吗?有什么想法或想法吗?另一件可能有用的事情:我什至无法显示指标并创建了这篇已解决的帖子:How to implement busy indicator in application shell

最佳答案

好吧,我终于实现了忙碌指示器。这是一项相当艰巨的任务,但我在此过程中学到了一些东西。至少我有东西可以证明。虽然我没有弄清楚为什么我的代码没有像我发布的那样工作,但我确实发现我试图在肯定会出现问题的操作中执行 UI 相关代码。我不是 100% 确定,因为我做了一些重写,而且我还没有足够的勇气恢复我的代码来玩它。但是,问题得到了回答,下面是我如何实现忙碌指示器:

首先,除了可以删除 IsBusy 的属性集和绑定(bind)之外,我认为没有任何变化。我最终将我的事件订阅从 View 模型移到了 View 后面的代码中。我的 View 背后的代码如下所示:

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 类。它被放置在我的库项目中,它依赖于我的解决方案中的每个项目:

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 方法创建了一个重载,因此我可以根据需要发送自定义忙消息。

现在,如果我修改我实际使用 Busy Indicator 的问题代码片段中的代码,它看起来像这样:

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 仅存在于此。有时说出来(或打字)会有所不同。

关于c# - 如何从 WPF Extended Toolkit 正确实现 Busy Indicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221866/

相关文章:

wpf - 在这种情况下推荐最佳方法

c# - 带有 Windows 窗体应用程序的 map

c# - PropertyChangedEvent 和 CanExecute 问题

c# - 在通用 Windows 应用程序中使用 MVVM Light 进行验证

wpf - WPF 中的闪烁按钮内容模板

.net - 如何更改WPF TabControl中TabItems的重叠顺序

c# - 如何使用ICommand在MVVM中触发KeyUp事件

c# - 如何使用 Moq 在函数中模拟谓词

c# - C# 中抽象工厂设计模式的真实世界示例

c# - ASP.NET MVC4 一 View 多模型