WPF Pack URI路径相关

WPF使用pack URI语法殉职编译过的资源。通常情况下我们只用相对URI来引用资源,如下:images/img_name.png

这和下面这个绝对URI是等效的:

pack://application:,,,/images/img_name.png

当为一幅图像设置源时可以使用这种绝对URI:

Img.Source=new BitmapImage(new Uri(“pack://application:,,,/images/img_name.png”));

使用pack URI还可以检索嵌入到另一个库中的资源(换句话说,在应用程序中使用的DLL程序集中的资源)对于这种情况,需要使用下面的语法:

pack://application:,,,/AssemblyName;component/ResourceName

例如调用程序集ImageLibrary中的图像image_name.png,需要使用到如下所示的URI:

Img.Source=new BirmapImage(

new Uri(“pack://application:,,,/ ImageLibrary;component/images/image_name.png”));

或者可以从更实用的角度使用等价的相对URI:

Img.Source=new BitmapImage(

new Uri(“ImageLibiary;component/images/image_name.png”,UriKind.Relavite));

事例代码:

<Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/BG_02.png"/>
        </Grid.Background>
        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <TextBlock Text="第二个窗体" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Image Source="pack://application:,,,/ImagesLibrary;component/BG.jpg"
                   Height="150"/>
            <Image x:Name="img1" Height="150"/>
            <Image x:Name="img2" Height="150"/>
        </StackPanel>
img1.Source = new BitmapImage(new Uri(@"/ImagesLibrary;v1.0.0.0;component/BG_01.png", UriKind.Relative));
            img2.Source = new BitmapImage(new Uri(@"/ImagesLibrary;component/BG_03.png", UriKind.Relative));

项目资源结构图:

效果图:

posted @ 2013-07-04 17:29  无晴雪  阅读(593)  评论(0编辑  收藏  举报