动态XAML加载和外部图像和字体

时间:2017-02-07 17:40:24

标签: wpf xaml

我有这个目录结构:

c:\app\program.exe
c:\app\assets\layout.xaml
c:\app\assets\videos\video.mp4
c:\app\assets\image\image.png

layout.xaml包含:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Image Source="assets/image/image.png" />
    <MediaElement Source="assets/videos/video.mp4" />
</Grid>

我使用

加载layout.xaml
using (var sr = new StreamReader(@"c:\app\assets\layout.xaml"))
{
    var pnl = (Panel)XamlReader.Load(sr.BaseStream);
    SomeOtherGrid.Children.Add(pnl);
}

视频显示,但图片没有显示。如何显示图像以及为什么视频显示而图像不显示?

修改 我知道行和网格如何工作。我知道在这种情况下视频将位于图像的顶部。我要问的是:为什么视频出现而图像没有显示。它们的来源完全相同。

编辑2: 显然你必须使用这个荒谬的&#34;包&#34;外部资源格式: pack://siteoforigin:,,,/assets/videos/video.mp4

这适用于图片和视频,但它不适用于字体: pack://siteoforigin:,,,/assets/fonts/intelclear/#Intel Clear Pro

为什么他们这么复杂?为什么他们只是从二进制文件的根目录加载它是否是相对路径?

1 个答案:

答案 0 :(得分:0)

MediaElement元素添加到同一网格后,Image将最终位于Grid.Row元素之上。

您应该使用两个RowDefinitions并将MediaElement的{​​{1}}附加属性设置为1,将其放在Image下面的第二行:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Image Source="image/image.png" />
    <MediaElement Source="videos/video.mp4" Grid.Row="1" />
</Grid>

如果您发布的样本路径正确,您还应该从路径中删除尾随的“资产”。来自Layout.xaml的图像的相对路径是“image.png”。