好的,我已经为这个问题苦苦挣扎了很长时间,并阅读了一篇又一篇的文章,试图对这个问题有所了解。我正在尝试实现繁忙指示器,但只有部分成功。我有一个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相同的线程上,但我认为既然我的逻辑发生在视图模型中,就不应该成为问题。我错了吗?有什么想法或思路吗?还有一件可能有帮助的事情:我甚至无法使指示器显示并创建了这个已解决的帖子:如何在应用程序壳中实现繁忙指示器。