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);
}
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?
Edit: I guess it's important to mention that I call ReportStarted() when I send message in ShareViewModel and ReportCompleted() when I am done.
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
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();
}
来源:https://stackoverflow.com/questions/32416209/sharing-target-universal-apps-windows-10-approach