最近碰到一个问题。
两个自定义控件,其中用到图片,即<Image Source="图片路径" />。
控件1:
控件2:
调用该控件:
区别在于第一个控件的Source="TestImageSource;component/Images/laugh.gif" 是手动填写。第二个则是,属性-source-选择图片。
结果造成,作为控件使用时,控件1无法显示图片,控件2正常显示图片。如下:
两个自定义控件,其中用到图片,即<Image Source="图片路径" />。
控件1:
<UserControl x:Class="TestImageSource.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image Source="TestImageSource;component/Images/laugh.gif" />
</Grid>
</UserControl>
控件2:
<UserControl x:Class="TestImageSource.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image Source="/TestImageSource;component/Images/laugh.gif" />
</Grid>
</UserControl>
调用该控件:
<Window x:Class="TestImageSource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="520" Width="1022" xmlns:my="clr-namespace:TestImageSource">
<Grid>
<my:UserControl1 HorizontalAlignment="Left" Margin="21,12,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="267" Width="423" />
<my:UserControl2 HorizontalAlignment="Left" Margin="540,58,0,0" x:Name="userControl21" VerticalAlignment="Top" Height="320" Width="411" />
</Grid>
</Window>
区别在于第一个控件的Source="TestImageSource;component/Images/laugh.gif" 是手动填写。第二个则是,属性-source-选择图片。
结果造成,作为控件使用时,控件1无法显示图片,控件2正常显示图片。如下:
![](https://img-my.csdn.net/uploads/201304/08/1365415380_3126.jpg)
不知是本地环境有问题还是原本如此,所以同样使用的需要注意了,还是手动选择图片吧。
代码下载:http://download.csdn.net/detail/yysyangyangyangshan/5234502