Sharing target Universal Apps Windows 10 approach

浪子不回头ぞ 提交于 2019-12-07 13:01:02

问题


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"}


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!