C# 如何正确实现WPF扩展工具包中的忙指示器
好的,我已经为这个问题挣扎了相当长的一段时间,并且已经阅读了一篇又一篇文章,试图对这个问题有一个想法。我正在尝试实施繁忙指标,但只取得了部分成功。我有一个Shell视图,我用BusyIndicator包装了它,如下所示:C# 如何正确实现WPF扩展工具包中的忙指示器,c#,wpf,mvvm,wpftoolkit,C#,Wpf,Mvvm,Wpftoolkit,好的,我已经为这个问题挣扎了相当长的一段时间,并且已经阅读了一篇又一篇文章,试图对这个问题有一个想法。我正在尝试实施繁忙指标,但只取得了部分成功。我有一个Shell视图,我用BusyIndicator包装了它,如下所示: <Window x:Class="Foundation.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic
<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>
接下来,我将使用一个名为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正在设置中,busy指示器按预期显示。工作不正常的是“忙碌”指示器没有设置动画。它只是停滞不前。我觉得这是一个线程问题,但要么我对UI线程的理解有偏差,要么我没有看到一些对您来说很明显的东西。我意识到IsBusy逻辑不能与UI在同一个线程上,但我认为,由于我的逻辑发生在视图模型中,所以这不应该是一个问题。我错了吗?有什么想法吗?另一件可能有用的事情是:我甚至在显示指示器时遇到了麻烦,于是创建了这篇文章,这篇文章得到了解决:好了,我终于实现了忙碌指示器。这是一项相当艰巨的任务,但我从中学到了一些东西。至少我有东西要展示。虽然我没有弄清楚为什么我的代码在发布时无法工作,但我确实发现我试图在操作中执行与UI相关的代码,这肯定是一个问题。我不是100%确定,因为我做了一点重新编写,我没有足够的青蛙恢复我的代码来玩它。但是,问题得到了回答,下面是我如何实现繁忙指标的:
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;
}
首先,除了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
}
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;
}
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);
首先,请注意,我创建了扩展EventArgs的BusyActionEventArgs类。它放在我的库项目中,该项目依赖于我的解决方案中的每个项目:
public class BusyActionEventArgs : EventArgs
{
public Action IsBusyAction { get; set; }
public string BusyMessage { get; set; }
}
最后,我的StateManager现在如下所示:
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
}
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;
}
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);
注意,我为IsBusyProcess方法创建了一个重载,因此如果愿意,我可以发送一条自定义的忙消息
现在,如果我修改问题代码段中的代码,我实际使用的是忙碌指示器,它如下所示:
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
}
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;
}
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刚刚出现在这个页面上。有时说出来(或打字)会有所不同。因此,正在运行的另一个进程需要一个忙碌的指示器,它也在一个单独的线程上吗?嗨,Felix。谢谢你的回复。长时间运行的进程应该与ShellViewModel在同一个线程上,除非我忽略了什么。我试图使用Threads窗口(我以前从未使用过),但它似乎表明我的ShellView和ShellViewModel都在主线程中。我不确定这是怎么可能的,所以我不相信它,我更不相信我自己的观点