有一個朋友問過我這樣一個問題,他在應用程序中調用圖片卻不顯示。下面介紹一下資源之間的調用以及關系。
flash圖片的生成操作為Resource,ps圖片的生成操作為內容。
編譯打包之后生成的結構如下圖。
Images文件夾里面只有ps圖片,因為ps圖片的生成操作為內容。
程序中的其他資源在ResDemo.dll中。
頁面文件以及生成操作為Resource的flash圖片都都打包到ResDemo.dll中了,結構如上圖。
下面我們就開始調用
6張圖片,左邊豎着數分別為image1、Image2、image3。右邊豎着數分別為image4、image5、image6。
布局代碼如下:
1: <Grid x:Name="ContentPanel" Margin="12,0,12,0">
2: <Image Height="200" HorizontalAlignment="Left" Margin="9,21,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/ResDemo;component/Images/flash.png" />
3: <Image Height="200" HorizontalAlignment="Left" Margin="9,247,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="Images/flash.png" />
4: <Image Height="200" HorizontalAlignment="Left" Margin="9,482,0,0" Name="image3" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/Images/ps.png" />
5: <Image Height="200" HorizontalAlignment="Left" Margin="231,21,0,0" Name="image4" Stretch="Fill" VerticalAlignment="Top" Width="200" />
6: <Image Height="200" HorizontalAlignment="Left" Margin="231,247,0,0" Name="image5" Stretch="Fill" VerticalAlignment="Top" Width="200" />
7: <Image Height="200" HorizontalAlignment="Left" Margin="231,482,0,0" Name="image6" Stretch="Fill" VerticalAlignment="Top" Width="200" />
8:</Grid>
image1通過"/ResDemo;component/Images/flash.png"訪問Resource資源的flash圖片。
因為xaml頁面也是Resource,所以也可以通過相對路徑Images/flash.png來調用flash圖片。
ps圖片是內容,所以以文件形式存放在ResDemo.dll,用"/Images/ps.png調用。
下面三個圖片為后台代碼賦值。(路徑跟xaml頁面一樣)
1: image4.Source = new BitmapImage(new Uri("/ResDemo;component/Images/flash.png", UriKind.RelativeOrAbsolute));
2: image5.Source = new BitmapImage(new Uri("Images/flash.png", UriKind.RelativeOrAbsolute));
3: image6.Source = new BitmapImage(new Uri("/Images/ps.png", UriKind.RelativeOrAbsolute));