Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows 10中共享目标通用应用程序模板10方法_C#_Win Universal App_Windows 10_Template10 - Fatal编程技术网

C# 在Windows 10中共享目标通用应用程序模板10方法

C# 在Windows 10中共享目标通用应用程序模板10方法,c#,win-universal-app,windows-10,template10,C#,Win Universal App,Windows 10,Template10,我的应用程序是用于共享的目标应用程序,当应用程序正在运行且用户想要共享内容时,我会遇到问题。我无法使用正在运行的应用程序中的帧,因为这样会出现“封送线程”异常 应用程序调用了为不同线程编组的接口。\r\n\r\n未能初始化应用程序的根visual App.xaml.cs中的OnStartAsync方法如下所示 public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args) { s

我的应用程序是用于共享的目标应用程序,当应用程序正在运行且用户想要共享内容时,我会遇到问题。我无法使用正在运行的应用程序中的帧,因为这样会出现“封送线程”异常

应用程序调用了为不同线程编组的接口。\r\n\r\n未能初始化应用程序的根visual

App.xaml.cs中的
OnStartAsync
方法如下所示

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条件,即应用程序已经运行的情况。我找到了一个,但它不使用Template10库。如何使用Template10库处理此场景

当您在UWP上使用share target时,当前应用程序正在“运行”(实际上已暂停并正在恢复-此事件首先触发),新应用程序将被创建。这是一件很酷的事情,但必须正确处理-您可以使用新的dispatcher获得新窗口,如果您尝试运行上一个窗口中的某些内容,则会出现“错误线程”异常(尤其要注意INotifyPropertyChanged)。您的应用程序现在至少运行两个线程,您将获得多线程应用程序的所有结果

最好的是,你可以在新窗口中放入任何你想要的东西——新页面、框架或其他。一切都取决于你的设计。但是如果你想运行一些东西/修改你的跑步应用程序,那么你必须使用它自己的调度程序。例如,您可以通过列出视图并获取一个视图来获得它:

wait CoreApplication.Views.First().Dispatcher.RunAsync(coredipatcherpriority.Normal,()=>{/*执行一些代码*/});
您还可以在变量中记住原始窗口的调度程序,然后在共享后使用它。若您的代码是异步的,那个么在离开ShareTarget部分之前,您必须注意并确保它已经完成

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


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

谢谢您的回答。现在我在
else
块中使用
Dispatcher
Window.Current.Content
设置为某个视图。这是正确的方法吗?@VivekMaskara代码有效吗?我不确定您的第一个
如果(shareArgs.PreviousExecutionState!=ApplicationExecutionState.Running)
-不确定这将如何运行应用程序之前已暂停/终止。您还可以检查应用程序是否有多个视图,然后执行适当的操作。至于您在dispatcher上运行的代码,我不确定是否理解您想要实现的目标,但从我看到的情况来看,您正试图通过以前的dispatcher设置新窗口(新应用程序视图)的内容,不确定这是否是您想要做的。@VivekMaskara记住,在新应用程序视图中,
窗口。当前的
指的是新窗口,而不是旧窗口。