【问题标题】:How to get the URI link to WAV file after compilation?编译后如何获取WAV文件的URI链接?
【发布时间】:2023-02-07 17:50:22
【问题描述】:

目前在这里试验 WPF。我正在尝试使用 URI 文件在表格中存储音乐。

它目前有效,但正如预期的那样只在我的电脑上,因为它是一个绝对路径:

private readonly Uri[] SoundsTable = new Uri[] { new Uri(@"C:\Users\damie\Desktop\repos2\Tetrics\Tetrics\Assets\music_theme.wav"), new Uri(@"C:\Users\damie\Desktop\repos2\Tetrics\Tetrics\Assets\line_clear.wav"), };

我遇到了编译后无法访问我的音乐文件的问题。我无法使用相对路径或确定它获取 Path.CurrentDirectory() (因为我的资产文件夹未在编译项目中生成)。

对于可以存储在我的 DLL 中的图像,我没有这个问题:

`private readonly ImageSource[] tileImages = new ImageSource[] {

 new BitmapImage(new Uri("Assets/TileEmpty.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileCyan.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileBlue.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileOrange.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileYellow.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileGreen.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TilePurple.png", UriKind.Relative)),
 new BitmapImage(new Uri("Assets/TileRed.png", UriKind.Relative))

};`

有谁知道在这里做什么?

我试图更改音乐文件的 IDE 属性,例如构建操作和复制到输出文件夹。 我想我的答案可能在这里,但我尝试过的都没有奏效。

非常感谢任何回应的人!

【问题讨论】:

    标签: c# wpf uri wav


    【解决方案1】:

    如果你查看你的 bin,发现编译后的 exe 并且那里没有 Assets 文件夹,那么你可能没有为你的文件设置正确的属性。

    您的项目中需要一个 Assets 文件夹。

    那里面一定有一些文件。

    它们的属性必须设置为构建操作“内容”和复制到输出目录“如果更新则复制”。

    如果你这样做,那么你将在编译后在你的 bin 中有一个包含文件的资产文件夹。

    然后,您可以使用绝对 url 引用这些文件,该 url 为:

     Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Assets", yourfilename)
    

    我认为提到 png 文件只是为了向我们展示其他适合您的东西。

    然而。

    wpf 中还有一个包表示法,如果您要在 xaml 中引用文件,您应该看一下。

    https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2021-07-06
      相关资源
      最近更新 更多