Launcher.LaunchFileAsync(...) 不工作 [英] Launcher.LaunchFileAsync(...) not working

查看:13
本文介绍了Launcher.LaunchFileAsync(...) 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发适用于 Win8 的应用程序,其中应包含保存联系信息的可能性.我使用的服务提供 VCard 支持,所以我决定使用它们.我可以成功下载并保存它们,只有自动打开它们不起作用.这些文件是正确的",可以毫无问题地从资源管理器中打开.为什么 LaunchFileAsync 不起作用的任何想法?

I'm working on an App for Win8 which should include the possibility of saving contact information. The service I use offers VCard support so I decided on using them. I can succesfully download and save them, only opening them automatically doesn't work. The files are "correct", and can be opened from the explorer without any problem. Any ideas why LaunchFileAsync isn't working?

这是代码的转储:

    private async void SaveContactSelected()
    {
        IRandomAccessStreamReference img = RandomAccessStreamReference.CreateFromUri(SelectedItem.Image.UriSource);
        StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(SelectedItem.Title.Replace(' ', '_')+".vcf", new Uri(SelectedItem.VCardUrl), img);

        var stream = await file.OpenReadAsync();
        uint size = Convert.ToUInt32(stream.Size);
        IBuffer buffer = await stream.ReadAsync(new Windows.Storage.Streams.Buffer(size), size, InputStreamOptions.None);
        if(!stream.CanRead)
        {
            //TODO: Error handling
            return;
        }

        FileSavePicker savePicker = new FileSavePicker();
        savePicker.FileTypeChoices.Add("vcard", new List<string> { ".vcf" });
        savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        savePicker.SuggestedFileName = SelectedItem.Title.Replace(' ', '_') + ".vcf";
        StorageFile saveDestination = await savePicker.PickSaveFileAsync();

        if (saveDestination != null)
        {
            CachedFileManager.DeferUpdates(saveDestination);
            await FileIO.WriteBufferAsync(saveDestination, buffer);
            FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            if (status == FileUpdateStatus.Complete)
            {
                bool succ = await Launcher.LaunchFileAsync(saveDestination, new LauncherOptions() { DisplayApplicationPicker=true });
                return;
            }
            else
            {
                //TODO: Error handling
            }
        } 
    }

这里是清单相关部分的转储:

An here a dump of the relevant parts of the manifest:

<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="MyApp.App">
  <VisualElements DisplayName="MyApp.com" Logo="Assets\Images\Logo.png" SmallLogo="Assets\Images\SmallLogo.png" Description="MyApp.com Description TODO" ForegroundText="dark" BackgroundColor="#464646">
    <DefaultTile ShowName="allLogos" WideLogo="Assets\Images\LogoWide.jpg" />
    <SplashScreen Image="Assets\Images\SplashScreen.png" BackgroundColor="#FFFFFF" />
    <InitialRotationPreference>
      <Rotation Preference="landscape" />
    </InitialRotationPreference>
  </VisualElements>
  <Extensions>
    <Extension Category="windows.search" />
    <Extension Category="windows.fileTypeAssociation">
      <FileTypeAssociation Name="vcard">
        <DisplayName>V-Card</DisplayName>
        <EditFlags OpenIsSafe="true" />
        <SupportedFileTypes>
          <FileType ContentType="text/x-vcard">.vcf</FileType>
        </SupportedFileTypes>
      </FileTypeAssociation>
    </Extension>
  </Extensions>
</Application>

为了更清晰,添加了清单转储.

Added manifest dump for more clarity.

推荐答案

在处理这个错误时:

  • 将文件写入我的 W8.1 应用程序的临时文件夹
  • 双击该文件即可正常打开(在本例中为 pptx)
  • Lau​​nchFileAsync 始终返回 false.

我发现了两件事.首先,参考了 Windows 完成的一些奇怪的安全检查(这不是我的原因:https://social.msdn.microsoft.com/Forums/en-US/c0625345-5d57-4e40-b446-85db4c6ef355/why-does-windowssystemlauncherlaunchfileasync-fail-with-file-from-my-local-folder?forum=winappswithhtml5).

Two things I discovered. First, a reference to some odd safety check done by Windows (which wasn't what caused it for me: https://social.msdn.microsoft.com/Forums/en-US/c0625345-5d57-4e40-b446-85db4c6ef355/why-does-windowssystemlauncherlaunchfileasync-fail-with-file-from-my-local-folder?forum=winappswithhtml5).

第二,这是我的问题,我不在 UI 线程上!切换到 LaunchFileAsync 在 UI 线程上使它突然工作得很好.

Second, which was my problem, was that I wasn't on the UI thread! Switching so LaunchFileAsync was on the UI thread made it suddenly work just fine.

这篇关于Launcher.LaunchFileAsync(...) 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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