WPF MVVM光 - 工作之前,显示通知完成 [英] WPF MVVM Light - Show notification before work is done

查看:333
本文介绍了WPF MVVM光 - 工作之前,显示通知完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVVM光应用程序,我想显示一个通知我做一些部分的同步的工作,需要两三秒钟之前。我不想让用户在工作正在做,所以没有必要异步,任务和IProgress或backgroundworkers等做任何事情。



在一个视图模型我有这样的代码。 (注意,这是不会位于XAML文件的代码隐藏,但在数据绑定视图模型)

 无效MyCommand(工程项目)
{
NavigationService.AddNotification(做就行了);
GetTheJobDone(项目);

}



的NavigationService添加通知文本数据绑定的ListView在客户端窗口的顶部。



我的问题是,上来的是显示的载入项目无论多长时间了。



一个例子是,当我想加载和显示的项目。如果负载部分需要几秒钟的界面冻结,而不对正在发生的事情,用户的任何信息。



修改



我添加一些更多的代码,由于请求。此代码工作正常。



这显示了通知视图有此XAML

 < ListView控件的ItemsSource ={结合}通知> 
< ListView.ItemTemplate>
<&DataTemplate的GT;
<标签前景=浅灰色CONTENT ={绑定消息}/>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;



持有通知的视图模型具有此代码



 公众诠释AddNotification(字符串消息)
{
...
通知注=新通知{消息=消息};
Notifications.Add(注);

}


解决方案

下面是根据 AxelEckenbergers我自己的解决方案回答。作为建议我只好让它异步工作。现在我不锁的图形用户界面,并能正常工作。



在我所有的ViewModels我添加一个名为AddTask方法的基类。

 公共任务AddTask(行动工作,
串通知,
串workDoneNotification,
动作<任务> continueWith)
{
INT notificationKey = NavigationService.AddNotification(通知,
的autoremove:假);

任务task = Task.Factory.StartNew(()=>
{
work.Invoke();
});

task.ContinueWith(T =>
{
NavigationService.RemoveNotification(notificationKey);
NavigationService.AddNotification(workDoneNotification);
如果(continueWith != NULL)
{
continueWith.Invoke(T);
}
},TaskScheduler.FromCurrentSynchronizationContext());

返回任务;
}



调用看起来像这样

  AddTask(()=> GetTheJobDone(),
上来的,
它的完成,
T => LoadProjects());


In my MVVM Light application i want to show a notification before i do some some synchronous work that takes two or three seconds. I don't want the user to do anything while the work is being done so there is no need for async, Task and IProgress or backgroundworkers etc.

In a ViewModel i have this code. (Note that this is not located in the code-behind of the XAML-file but in a databound ViewModel)

void MyCommand(Project project)
{
   NavigationService.AddNotification("Doin' it");
   GetTheJobDone(project);
   ...
}

NavigationService adds the notification text to a databound ListView at the top of the client window.

My problem is that "Doin' it" show up after the project is loaded no matter how long time it took.

One example could be when I want to load and show a project. If the load part takes a few seconds the interface freezes without any information for the user what is happening.

EDIT

I add some more code due to requests. This code works fine.

The View that shows the notifications has this XAML

<ListView ItemsSource="{Binding Notifications}">
   <ListView.ItemTemplate>
      <DataTemplate>
         <Label Foreground="LightGray" Content="{Binding Message}" />
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

The ViewModel that holds the notifications has this code

public int AddNotification(string message)
{
   ...
   Notification note = new Notification { Message = message };
   Notifications.Add(note);
   ...
}

解决方案

Here's my own solution based on AxelEckenbergers answer. As suggested I had to make it asynchronous to work. For now i don't lock the GUI, and it works fine.

In the base class for all my ViewModels i add a method called AddTask.

public Task AddTask(Action work, 
   string notification, 
   string workDoneNotification,       
   Action<Task> continueWith)
{
   int notificationKey = NavigationService.AddNotification(notification,
                             autoRemove:false);

   Task task = Task.Factory.StartNew(() =>
   {
       work.Invoke();
   });

   task.ContinueWith(t =>
   {
       NavigationService.RemoveNotification(notificationKey);
       NavigationService.AddNotification(workDoneNotification);
       if (continueWith != null)
       {
           continueWith.Invoke(t);
       }
   }, TaskScheduler.FromCurrentSynchronizationContext());

   return task;
}

The call looks like this

AddTask(() => GetTheJobDone(), 
   "Doin' it",
   "It's done",
   t => LoadProjects());

这篇关于WPF MVVM光 - 工作之前,显示通知完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆