我在WPF(MVVM)中有一个应用程序,在我的viewmodel中我需要保存一些新条目。这是一个包含多个选项卡的大型应用程序,它使用1个中央数据库上下文(EF 5)。
保存新条目时,每个条目都以编程方式验证,然后通过DBContext在数据库中插入或更新。
每次按下“保存”按钮,此操作将执行超过一百个条目并需要几秒钟......
保存时我想显示一个加载窗口....我虽然通过后台工作线程这样做... 像这里的东西: Show loading window
不幸的是我正在使用Entity Framework获取AccessViolationExceptions,因为我只使用同一个DBContext ...
是否有可能在保留1个DBContext的同时显示加载窗口?
谢谢!
答案 0 :(得分:0)
我想告诉你我是怎么做的:
EstaOcupado = true;
var taskSalvar = Task.Run(() =>
{
var proxyFactory = ServiceLocator.Current.GetInstance<IServiceClientFactory>();
var proxy = proxyFactory.GetCadastrosGeraisService();
proxy.Open();
caixa = Boot.Mapeador.Map<CaixaModel, Caixa>(Caixa);
caixa = proxy.SalvarCaixa(caixa);
proxy.Close();
});
try
{
taskSalvar.Wait();
Caixa.EndEdit();
EstaEmEdicao = false;
}
catch (AggregateException ae)
{
var msg = WcfProxy.HandleServiceExceptions(ae);
Resolve<IMessageVisualizer>()
.Show("Atenção", msg,
new CustomMessageVisualizerOptions() {Dialog = DialogType.Alert, Prompt = MessageButtons.OK});
}
finally
{
EstaOcupado = false;
Caixa = Boot.Mapeador.Map<Caixa, CaixaModel>(caixa);
}
我的&#34; EstaOcupado&#34; (IsBusy)属性绑定到控件矿,显示加载动画。当我使用Task时,我的taskSalvar在后台运行,允许我的UI更新。请试试这个结构。