我正在尝试将 WP7.1 Mango 中的任务启动器与最新版本的 Caliburn Micro 一起使用,但任务完成后我的代码不会被回调。可能我在某个地方做了一些愚蠢的事情,但我不知道在哪里。以下是我对示例重现应用程序所做的操作,您可以从以下位置下载:
http://www. filesonic.com/file/2750397005/PhoneTaskTest.zip
1) 创建一个新的 WP7.1 应用程序;
2)在解决方案中添加一个Lib文件夹,添加CM dll,并添加对它们的引用;
3) 按照 CM 文档的指定更改生成的文件并添加引导程序。
现在到目前为止,一切正常,应用程序启动没有任何问题。然后,我执行以下操作来拍照:
4) 在主页中向视图添加一个按钮,并在其 VM 中添加一个相应的方法,名为 TakePhoto。
5)按如下方式更改VM:
a)在构造函数中添加注入的只读IEventAggregator成员;
b) 添加 OnActivate/OnDeactivate 覆盖,让聚合器订阅和取消订阅该虚拟机;
c) 添加 TakePhoto 方法,它只是:
_aggregator.RequestTask<CameraCaptureTask>();
d) 从接口 IHandle> 派生 VM 并实现它:
public void Handle(TaskCompleted<CameraCaptureTask> message)
{
if (message.Result.TaskEventArgs.TaskResult != TaskResult.OK) return;
SetPhoto(message.Result.TaskEventArgs.ChosenPhoto);
}
现在,当我单击按钮时相机任务在模拟器中启动,我可以拍照;然后我被带回我的应用程序,但没有任何反应,并且我的 Handle 方法从未被调用。您可以在那里放置一个断点来确认这一点。
那么,我在这里做错了什么?
I'm trying to use a task launcher in WP7.1 Mango with the latest version of Caliburn Micro, but my code is not getting called back once the task has completed. Probably I'm doing something stupid somewhere, but I cannot see where. Here is what I did for a sample repro application you can download from:
http://www.filesonic.com/file/2750397005/PhoneTaskTest.zip
1) create a new WP7.1 application;
2) add a Lib folder in the solution, add there CM dll's, and add a reference to them;
3) change the generated files as specified by CM documentation and add a bootstrapper.
Now up to this point everything is OK and the application starts with no issues. I then do the following for taking a photo:
4) add a button in the main page to the view and a corresponding method in its VM, named TakePhoto.
5) change the VM as follows:
a) add a readonly IEventAggregator member injected in the constructor;
b) add OnActivate/OnDeactivate overrides to let the aggregator subscribe and unsubscribe this VM;
c) add the TakePhoto method which is just:
_aggregator.RequestTask<CameraCaptureTask>();
d) derive the VM from interface IHandle<TaskCompleted<CameraCaptureTask>> and implement it:
public void Handle(TaskCompleted<CameraCaptureTask> message)
{
if (message.Result.TaskEventArgs.TaskResult != TaskResult.OK) return;
SetPhoto(message.Result.TaskEventArgs.ChosenPhoto);
}
Now, when I click the button the camera task starts in the emulator and I can take a photo; then I'm taken back to my application, but nothing happens and my Handle method is NEVER called. You can just place a breakpoint there to confirm this.
So, what I'm doing wrong here?
发布评论
评论(1)
您需要处理
TaskCompleted
而不是TaskCompleted
。因为 Caliburn.Micro 使用任务的Completed
事件的事件参数创建TaskCompleted
消息,在CameraCaptureTask
的情况下为 < a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.photoresult%28v=vs.92%29.aspx" rel="nofollow">照片结果。因此,您应该实现IHandle>
并且您的 Handle 方法应如下所示You need to handle
TaskCompleted<PhotoResult>
instead ofTaskCompleted<CameraCaptureTask>
. Because Caliburn.Micro creates theTaskCompleted<T>
message with the event args of the Task'sCompleted
event which is in the case ofCameraCaptureTask
is PhotoResult. So you should implementIHandle<TaskCompleted<PhotoResult>>
and your Handle method should look like this