默认情况下,引用的项目不会添加到主项目中。这并不像看起来那么明显,这就是为什么我花了将近一周的时间来找出这个。所以线索是:检查参考项目可访问性。
更新:
正如我在开发过程中发现的那样,还有更多的事情需要处理。其中一些并不像他们需要的那样清楚。这是我所做的列表:
- 将后台项目添加到主项目的引用中(右键单击解决方案浏览器中的引用节点)。
- 检查主项目清单是否包含正确的声明(带控制通道的后台任务,带有完整包的正确后台入口点名称,$targetnametoken$.exe 作为可执行文件)
- 从 #1 引出的一件事:您计划在后台使用的所有实体都应该放在解决方案中的单独项目中。然后这个项目被主项目和后台项目引用。
- 请注意在注册 ControlChannelTrigger 之前要调用 BackgroundExecutionManager.RequestAccessAsync()
- 我在示例项目的一个小评论中发现了一件关键的事情:
// IMPORTANT: When using winRT based transports such as StreamWebSocket with the ControlChannelTrigger,
// we have to use the raw async pattern for handling reads instead of the await model.
// Using the raw async pattern allows Windows to synchronize the PushNotification task's
// IBackgroundTask::Run method with the return of the receive completion callback.
// The Run method is invoked after the completion callback returns. This ensures that the app has
// received the data/errors before the Run method is invoked.
// It is important to note that the app has to post another read before it returns control from the completion callback.
// It is also important to note that the DataReader is not directly used with the
// StreamWebSocket transport since that breaks the synchronization described above.
// It is not supported to use DataReader's LoadAsync method directly on top of the transport. Instead,
// the IBuffer returned by the transport's ReadAsync method can be later passed to DataReader::FromBuffer()
// for further processing.
更多信息在这里 - http://code.msdn.microsoft.com/windowsapps/ControlChannelTrigger-91f6bed8/sourcecode?fileId=57961&pathId=2085431229
如果你做的一切都正确,后台任务的调试就很简单了。只需设置断点并继续,不要管主项目正在运行或暂停。
ps - 如果项目被挂起,请注意调用 UI 线程(尤其是等待的东西) - 它们在应用程序运行之前不会运行,并且会等待。