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));

项目资源结构图:

效果图:

转载于:https://www.cnblogs.com/wrl-wuqingxue/p/3171985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值