在Windows 10中共享目标Universal Apps Template10方法 [英] Sharing target Universal Apps Template10 approach in Windows 10

查看:114
本文介绍了在Windows 10中共享目标Universal Apps Template10方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是用于共享的目标应用程序,并且在该应用程序运行且用户希望共享内容时遇到问题.我无法使用正在运行的应用程序中的框架,因为这样我会收到编组线程"异常.

My app is a target app for sharing and am facing issues when the app is running and the user wants to share content. I can't use a frame from the running application because then i get a "marshalling thread" exception.

该应用程序调用了一个已编组到另一个线程的接口.\ r \ n \ r \ n无法初始化应用程序的根外观

我在 App.xaml.cs 中的OnStartAsync方法看起来像这样.

My OnStartAsync method in App.xaml.cs looks like this.

public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
    switch (DetermineStartCause(args))
    {
        // other cases
        case AdditionalKinds.Other:
            if (args is ShareTargetActivatedEventArgs)
            {
                var shareArgs = args as ShareTargetActivatedEventArgs;

                if (shareArgs.PreviousExecutionState != ApplicationExecutionState.Running)
                {
                    Uri webUrl = await shareArgs.ShareOperation.Data.GetWebLinkAsync();
                    Debug.WriteLine(webUrl.AbsoluteUri);

                    //shareArgs.ShareOperation.ReportStarted();
                    NavigationService.Navigate(typeof(Views.MainPage), webUrl.AbsoluteUri);
                }
                else
                {
                        await CoreApplication.Views.First().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                        {
                            Uri webUrl = await shareArgs.ShareOperation.Data.GetWebLinkAsync();
                            var nav = NavigationServiceFactory(BackButton.Attach, ExistingContent.Exclude);
                            Window.Current.Content = new Views.ShareLaunch();
                            Window.Current.Activate();
                        });
                }
            }
            break;
    }
}

我不确定如何处理ShareTargetActivatedEventArgs的else条件,即应用程序已经在运行的情况.我在Stackoverlow上发现了一个类似的问题,但是它没有使用Template10图书馆.如何使用Template10库处理这种情况.

I am not sure how to handle the else condition for ShareTargetActivatedEventArgs ie the case in which the application is already running. I found a similar question on Stackoverlow but it doesn't use Template10 library. How to handle this scenario using Template10 library.

推荐答案

当您在UWP上使用共享目标并且当前应用正在运行"时(实际上已被暂停并且正在恢复-该事件首先被触发),新的 INotifyPropertyChanged ).您的应用程序现在至少要运行两个线程,并且会得到 multithreaded 应用程序的所有后果.

When you use share target on UWP and you current app is 'running' (in fact is was suspended and is being resumed - this event is fired first), the new ApplicationView is being created. This is a cool thing, but must be handled correctly - you get new window with new dispatcher and if you try to run something from previous one you will get 'wrong thread' exception (especially look out for INotifyPropertyChanged). Your app is running now at least two threads and you get all consequences of multithreaded application.

最好的是,您可以在新窗口中放置任何所需的内容-新页面,框架或其他.一切都取决于您的设计.但是,如果您想运行某些内容/修改您的正在运行的应用程序,则必须使用其自己的 Dispatcher .例如,您可以通过列出视图并采取一个视图来获取它:

The best of it is that you can put in new window anything you want - new page, frame or other. Everything depends on your design. But if you want to run something/modify your running app, then you will have to use its own Dispatcher. You can for example obtain it by listing views and taking a one:

await CoreApplication.Views.First().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { /*do some code*/ });

您还可以在变量中记住原始窗口的调度程序,然后在共享后使用它.如果您的代码是 async ,则在离开共享目标部分之前,您必须先检查并确保代码完成.

You can also remember your original window's dispatcher in variable and then after sharing, use it. If your code is async, you will have to look out and ensure it gets finished, before leaving share target section.

请注意,您可以使用 ApplicationViewSwitcher 显示原始窗口(但这不会改变共享代码正在不同线程上运行的事实).

Note also that you can use ApplicationViewSwitcher to show your original window (but that doesn't change the fact that the code from sharing is being run on different thread).

如果您需要有关多视图编程的更多信息,请查看在MSDN .

If you need more information about multiple view programming, take a look at MSDN.

这篇关于在Windows 10中共享目标Universal Apps Template10方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆