zoukankan      html  css  js  c++  java
  • wpfのuri(让你完全明白wpf的图片加载方式以及URI写法)

    绝对 pack WPF URI

    pack://application:,,,/是协议;“,,,”是“///”的变体

    1.资源文件 — 本地程序集

    Uri uri = new Uri("pack://application:,,,/ResourceFile.xaml", UriKind.Absolute);

    子文件夹中的资源文件 — 本地程序集(资源文件在本地程序集的子文件夹)

    Uri uri = new Uri("pack://application:,,,/Subfolder/ResourceFile.xaml", UriKind.Absolute);

          

    2.资源文件 — 所引用的程序集(资源文件在别的程序集)

    Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Absolute);

    3.所引用的程序集的子文件夹中的资源文件(资源文件别的程序的子文件夹)

    Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Absolute);

    4.所引用的版本化程序集中的资源文件(版本化在中间加入版本信息)

    Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;v1.0.0.0;component/ResourceFile.xaml", UriKind.Absolute);

    综上:路径前部分是”pack://application;,,,/程序集名字;”;后半部分”component/路径“”;合起来是一个绝对路径;如果是本地程序集,程序集名字和compoent可以省略(;也省略掉)

    WPF URI内容文件

    1.Uri uri = new Uri("pack://application:,,,/ContentFile.xaml", UriKind.Absolute);

    2.子文件夹中的内容文件

    Uri uri = new Uri("pack://application:,,,/Subfolder/ContentFile.xaml", UriKind.Absolute);

    3.源站点文件

    Uri uri = new Uri("pack://siteoforigin:,,,/SOOFile.xaml", UriKind.Absolute);

    4.子文件夹中的源站点文件

    Uri uri = new Uri("pack://siteoforigin:,,,/Subfolder/SOOFile.xaml", UriKind.Absolute);

    相对 pack URI      

    1.WPF URI资源文件 — 本地程序集

    Uri uri = new Uri("/ResourceFile.xaml", UriKind.Relative);

         

    2.子文件夹中的资源文件 — 本地程序集

    Uri uri = new Uri("/Subfolder/ResourceFile.xaml", UriKind.Relative);  

        

    3.资源文件 — 所引用的程序集

    Uri uri = new Uri("/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Relative);

        

    4.子文件夹中的资源文件 — 所引用的程序集

    Uri uri = new Uri("/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Relative);

                                                                                                                                    

    内容文件

    Uri uri = new Uri("/ContentFile.xaml", UriKind.Relative);        

    WPF URI子文件夹中的内容文件Uri uri = new Uri("/Subfolder/ContentFile.xaml", UriKind.Relative);

                                              特点:不用协议   

    一、加载本项目的图片 WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源。 其中较为常见的情况是用Uri加载图像。Uri表达式的一般形式为:协议+授权+路径 协议:pack:// 授权:有两种。一种用于访问编译时已经知道的文件,用application:/// 一种用于访问编译时不知道、运行时才知道的文件,用siteoforigin:///
    一般用逗号代替斜杠,也就是改写作application:,,,和pack:,,, 路径:分为绝对路径和相对路径。一般选用相对路径,普适性更强
    下面,我们举一个简单的例子: pack://application:,,,/images/my.jpg 当然,WPF默认Uri设置有pack://application:,,,,所以我们也可以直接将其写作:/images/my.jpg
    后边写例子程序时,为了让读者更好的了解Uri,我们都采用完整的Uri写法。 下面在讲讲装载图片的两种方式,一种用XAML引用资源,一种用代码引用资源。
    用XAML引用资源:

    <Image Source="pack://application:,,,/images/my.jpg"/>

     也可以这样

    <Image Source="/images/my.jpg"/>

      用代码引用资源:

    Image img;
    img.Source=new BitmapImage(new Uri("pack://application:,,,/images/my.jpg"),UriKind.Relative);

      也可以直接使用代码中引用图片资源

    image2.Source = new BitmapImage(new Uri("/images/my.jpg", UriKind.Relative));

      二、WPF 调用资源图片

    imagePath = "pack://application:,,,/Solution;component/Properties/../images/star/my.jpg";
    imageBrush.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));

        三、WPF引用外部项目资源的方法 WPF中如果你使用的资源文件不是本程序集的,是另外的程序集,就可以这样做: 1.引用要用的程序集,pack://application:,,,/程序集名称;component/路径 ,其中pack://application:,,,可以省略 示例:

    <Image Source="pack://application:,,,/Skin;component/image/you.png" />

     或者

    <Image Source="/Skin;component/image/you.png" />

     
    四、使用SiteOfOrigin

     
     
    imgContent.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/images/my.jpg"));
     
    分类: WPF
  • 相关阅读:
    DotText源码阅读
    看来这世界看得太清楚了也未必是种好事呢~~~~~~~
    在Init之前究竟执行了什么?
    孙子兵法
    Excel区域重命名
    Getbuffer ReleaseBuffer Cstring
    批量删除svn标志
    VB制作网页自动填表(强烈推荐)
    GetModuleFileName
    ansi编码
  • 原文地址:https://www.cnblogs.com/Im-Victor/p/10611226.html
Copyright © 2011-2022 走看看