正如您能够通过您的ResourceDirectonary
引用您的图像,表明您的图像能够被找到。
将假设您使用BuildAction="Resource"
将图像添加到您的项目。
看着这条线,我认为你错误地使用了UriKind.Relative
而不是UriKind.Absolute
。
事实上,它通常不需要使用第二UriKind
参数,因为如果你的Uri
字符串是的“包://”的品种,那么无论是相对或绝对的定位器编码...或者,如果你的字符串有一个“/”前缀,这意味着“绝对”,而其他任何东西通常都是相对的......如果你想通过使用“./”,“../”等,你可以更明显。
(除非你让它解释,否则,这是你似乎做了...这就是为什么它不工作)。
// Uri uri = new Uri("pack://application:,,,/Images/globe.png", UriKind.Relative);
上使用辅助 “包://” URI来引用图像...我想出了一个矩阵来展示一些不同的组合,以防你遇到了一个陷阱。
这显示了引用图像“资源”的一些不同组合,具体取决于您如何将该资源提供给应用程序以及如何引用它(并非所有选项)。
创建了4个图像,并将其添加为:image1.bmp,image2.bmp,image3.bmp,image4.bmp,直接作为“项目”节点下的文件。构建操作被设置为4个不同的值。
然后探索一些引用“图像”的不同方法。
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="1200">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="4"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<BitmapImage x:Key="bitmap1" UriSource="Image1.bmp"/>
<BitmapImage x:Key="bitmap2" UriSource="Image2.bmp"/>
<BitmapImage x:Key="bitmap3" UriSource="Image3.bmp"/>
<BitmapImage x:Key="bitmap4" UriSource="Image4.bmp"/>
<Image x:Key="image1" Source="Image1.bmp"/>
<Image x:Key="image2" Source="Image2.bmp"/>
<Image x:Key="image3" Source="Image3.bmp"/>
<Image x:Key="image4" Source="Image4.bmp"/>
</Window.Resources>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="1">BuildAction=<LineBreak/>"Resource"</TextBlock>
<TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="2">BuildAction=<LineBreak/>"Embedded Resource"</TextBlock>
<TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="3">BuildAction=<LineBreak/>"Content"</TextBlock>
<TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="4">BuildAction=<LineBreak/>"Content (copied to output)"</TextBlock>
<TextBlock Background="PeachPuff" Grid.Column="1" Grid.Row="0">pack://application:,,,/</TextBlock>
<TextBlock Background="PeachPuff" Grid.Column="2" Grid.Row="0">pack://application:,,,/WpfApplication4;component/</TextBlock>
<TextBlock Background="PeachPuff" Grid.Column="3" Grid.Row="0">pack://siteoforigin:,,,/</TextBlock>
<TextBlock Background="PeachPuff" Grid.Column="4" Grid.Row="0">Image<LineBreak/>referencing BitmapImage<LineBreak/>via {StaticResource}<LineBreak/>referencing "Resource"</TextBlock>
<TextBlock Background="PeachPuff" Grid.Column="5" Grid.Row="0">ContentPresenter<LineBreak/>referencing Image<LineBreak/>via {StaticResource}<LineBreak/>referencing "Resource"</TextBlock>
<Image Grid.Column="1" Grid.Row="1" Source="pack://application:,,,/Image1.bmp"/>
<Image Grid.Column="1" Grid.Row="2" Source="pack://application:,,,/Image2.bmp"/>
<Image Grid.Column="1" Grid.Row="3" Source="pack://application:,,,/Image3.bmp"/>
<Image Grid.Column="1" Grid.Row="4" Source="pack://application:,,,/Image4.bmp"/>
<Image Grid.Column="2" Grid.Row="1" Source="pack://application:,,,/WpfApplication4;component/Image1.bmp"/>
<Image Grid.Column="2" Grid.Row="2" Source="pack://application:,,,/WpfApplication4;component/Image2.bmp"/>
<Image Grid.Column="2" Grid.Row="3" Source="pack://application:,,,/WpfApplication4;component/Image3.bmp"/>
<Image Grid.Column="2" Grid.Row="4" Source="pack://application:,,,/WpfApplication4;component/Image4.bmp"/>
<Image Grid.Column="3" Grid.Row="1" Source="pack://siteoforigin:,,,/Image1.bmp"/>
<Image Grid.Column="3" Grid.Row="2" Source="pack://siteoforigin:,,,/Image2.bmp"/>
<Image Grid.Column="3" Grid.Row="3" Source="pack://siteoforigin:,,,/Image3.bmp"/>
<Image Grid.Column="3" Grid.Row="4" Source="pack://siteoforigin:,,,/Image4.bmp"/>
<Image Grid.Column="4" Grid.Row="1" Source="{StaticResource bitmap1}"/>
<Image Grid.Column="4" Grid.Row="2" Source="{StaticResource bitmap2}"/>
<Image Grid.Column="4" Grid.Row="3" Source="{StaticResource bitmap3}"/>
<Image Grid.Column="4" Grid.Row="4" Source="{StaticResource bitmap4}"/>
<ContentPresenter Grid.Column="5" Grid.Row="1" Content="{StaticResource image1}"/>
<ContentPresenter Grid.Column="5" Grid.Row="2" Content="{StaticResource image2}"/>
<ContentPresenter Grid.Column="5" Grid.Row="3" Content="{StaticResource image3}"/>
<ContentPresenter Grid.Column="5" Grid.Row="4" Content="{StaticResource image4}"/>
</Grid>
</Window>
你有没有尝试使用新的URI( “包://应用:,,, /图片/ globe.png”,UriKind.Absolute); ?....亲戚看起来不对。 –