【问题标题】:How do I add a File Type Association in a Windows Phone 8.1 app manifest?如何在 Windows Phone 8.1 应用清单中添加文件类型关联?
【发布时间】:2014-06-25 18:46:05
【问题描述】:

我正在尝试使用以下代码访问 KnownFolders.MusicLibrary 中的文件。

    internal async void Update()
    {
        StorageFolder rootFolder = KnownFolders.MusicLibrary;
        ScanFolder(rootFolder);
    }

    private async void ScanFolder(StorageFolder storageFolder)
    {
        var items = await storageFolder.GetItemsAsync();

        foreach (var item in items)
        {
            if (item is StorageFolder)
            {
                ScanFolder(item as StorageFolder);
            }
            else if (item is StorageFile)
            {
                var fs = new FileStream(item.Path, FileMode.Open, FileAccess.Read);
                < ... snip ... >
            }
        }
    }

我检查了MusicLibrary 功能,以便我可以使用KnownFolders.MusicLibrary。 这似乎并没有授予我对库的文件访问权限,因为我收到以下错误:

+       [System.UnauthorizedAccessException]    

{System.UnauthorizedAccessException: Access to the path 'C:\Data\Users\Public\Music\tmp' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at MusicTrackerPhone.Library.<ScanFolder>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)}    System.UnauthorizedAccessException

无法通过 GUI 添加FileType Association(xml 中也不允许该节点)。

如何为我的应用添加所需的声明?

完整的 Package.appxmanifest:

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest" xmlns:m3="http://schemas.microsoft.com/appx/2014/manifest" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest">
  <Identity Name="aad31163-0d67-49ba-b569-80ff4d773fa0" Publisher="CN=Benjamin" Version="1.0.0.0" />
  <mp:PhoneIdentity PhoneProductId="2e54a5f6-5319-4dd2-9bad-1fe5290a0eeb" PhonePublisherId="1b03f58e-5a39-414f-9324-2cab834debfd" />
  <Properties>
    <DisplayName>MusicTrackerPhone</DisplayName>
    <PublisherDisplayName>Benjamin</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Prerequisites>
    <OSMinVersion>6.3.1</OSMinVersion>
    <OSMaxVersionTested>6.3.1</OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="x2e54a5f6y5319y4dd2y9bady1fe5290a0eebx" Executable="AGHost.exe" EntryPoint="MainPage.xaml">
      <m3:VisualElements DisplayName="MusicTrackerPhone" Square150x150Logo="Assets\SquareTile150x150.png" Square44x44Logo="Assets\Logo.png" Description="MusicTrackerPhone" ForegroundText="light" BackgroundColor="#464646">
        <m3:DefaultTile Square71x71Logo="Assets\SquareTile71x71.png">
        </m3:DefaultTile>
        <m3:SplashScreen Image="Assets\Splashscreen.png" />
      </m3:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name=".mp3">
            <DisplayName>mp3</DisplayName>
            <SupportedFileTypes>
              <FileType ContentType="audio/mp3">.mp3</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="musicLibrary" />
    <Capability Name="removableStorage" />
  </Capabilities>
  <Extensions>
    <Extension Category="windows.activatableClass.inProcessServer">
      <InProcessServer>
        <Path>AgHostSvcs.dll</Path>
        <ActivatableClass ActivatableClassId="AgHost.BackgroundTask" ThreadingModel="both" />
      </InProcessServer>
    </Extension>
  </Extensions>
</Package>

【问题讨论】:

    标签: xaml windows-phone-8.1


    【解决方案1】:

    奇怪的事情(它只涉及 WP8.1 Silverlight),但正如我检查过的,可以通过 xaml 编辑器添加 FileType Associations:右键单击 package.appxmanifest 文件,选择 查看代码 F7。在&lt;Application 中找到&lt;Extensions&gt; 部分(可能就在&lt;/m3:VisualElements&gt; 之后)并手动添加第一个FileType Association:

      </m3:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name=".mp3">
            <DisplayName>mp3</DisplayName>
            <SupportedFileTypes>
              <FileType ContentType="audio/mp3">.mp3</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
        // other extensions
      </Extensions>
    

    一旦您添加了第一个,下一个 FileType Associations 您将能够通过 GUI 添加 - 从现在开始,它将在 Supported Declarations 中可用。

    【讨论】:

    • 请看问题。我提到将节点添加到 xml 不起作用。添加了屏幕截图。
    • @BenjaminDiele 我刚刚检查过了。等等,我会尝试分享一个例子。
    • 我显然将节点添加到了错误的父节点。您是正确的,在&lt;Applications&gt; 节点中添加Extensions 节点是正确的方法。现在我仍然需要解决 Access Denied 异常。
    • @BenjaminDiele 添加功能音乐库、可移动存储,看看是否有帮助。
    • 请注意,在 VS 中进行调试时,如果您要更改 mainfest 文件中的 fileTypeAssociations,则必须在点击 Debug 之前在设备或模拟器上卸载应用程序,否则不会注册更改.
    【解决方案2】:

    不需要在文件关联中添加 .mp3,它是保留的,将被忽略。您只能访问不能访问手机内存的 SD 卡库。

    【讨论】:

    • 虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多