共享目标Universal Apps Windows 10方法 [英] Sharing target Universal Apps Windows 10 approach
问题描述
我正在努力几个小时,找不到可行的解决方案。我的应用程序是共享的目标应用程序,问题是它在运行时和用户想要共享内容时。
I am struggling with it for a few hours and can't find working solution. My app is a target app for sharing and the problem is when it's running and user wants to share content.
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
await OnInitializeAsync();
if (await CheckToken(args) != true) return;
if (args.PreviousExecutionState != ApplicationExecutionState.Running)
{
if (await LoadData(args) != true) return;
}
var frame = new Frame();
var navigationService = new NavigationService(_dispatcherService) { RootFrame = frame, };
Window.Current.Content = frame;
Window.Current.Activate();
navigationService.Navigate<ShareViewModel>(args.ShareOperation);
}
问题是我无法从正在运行的应用程序中使用框架,因为我得到了一个例外编组线程....,因此我创建了一个新框架并将其分配给Window.Current.Content。这可以正常工作,但问题是用户完成共享时。我该怎么办?看来我应该将前一帧分配给通过共享目标权限被覆盖的Window.Current.Content?在尝试执行此操作时,我再次收到编组线程异常。如果我不这样做,那么我将无法与我的应用程序进行交互,因为我会收到一个例外,说明该应用程序已关闭。成为共享目标的合适方案是什么?
The problem is that I can't use frame from running application because I get an exception "marshalling thread ...." so I create a new frame and I assign it to Window.Current.Content. This works fine but the problem is when user finishes sharing. What should I do? It seems that I should assign previous frame to Window.Current.Content which was "overriden" by sharing target right? While I try to do it I get again "marshalling thread" exception. If I don't do it then I can't interact with my application because I get an exception that app is being closed. What is the proper scenario for being a sharing target?
编辑:我想很重要的一点是,我发送邮件时要呼叫 ReportStarted()完成后,出现在ShareViewModel和 ReportCompleted()中的消息。
I guess it's important to mention that I call ReportStarted() when I send message in ShareViewModel and ReportCompleted() when I am done.
尝试分配框架后抛出异常:
{该应用程序调用了为另一个线程编组的接口。\r\n\r\n无法初始化该应用程序的根外观}
Exception thrown when I try to assign frame back: {"The application called an interface that was marshalled for a different thread.\r\n\r\nFailed to initialize the application's root visual"}
推荐答案
我正在粘贴解决该问题的解决方案。我认为这里的关键是使用
I'm pasting the solution which solved the issue. I think the key here is to use
CoreWindow.GetForCurrentThread()。Dispatcher
CoreWindow.GetForCurrentThread().Dispatcher
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
await OnInitializeAsync();
if (await CheckToken(args) != true) return;
if (args.PreviousExecutionState != ApplicationExecutionState.Running)
{
if (await LoadData(args) != true) return;
}
var frame = new Frame();
Window.Current.Content = frame;
var dispatchService = new DispatcherService() { Dispatcher = CoreWindow.GetForCurrentThread().Dispatcher };
var navigationService = new NavigationService(dispatchService) { RootFrame = frame };
navigationService.Navigate<ShareViewModel>(args.ShareOperation);
Window.Current.Activate();
}
这篇关于共享目标Universal Apps Windows 10方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!