wpf 图片资源引用问题


   自学 wpf 经常 会遇到一些奇怪的问题,很多东西要去尝试,才能得到满意的答案

   首先看我遇到问题,图片资源引用,如果是在wpf应用程序中,这样写是没有问题的.

如果换在把这个当dll文件 让别人调用,这个就会出现问题,项目找到不到图片资源。

        <Style x:Key="PageTextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Height" Value="25" />
            <Setter Property="Width" Value="40" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="/Images/Page_TextBack.png"  ></ImageBrush>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsReadOnly" Value="True">
                    <Setter Property="Background" Value="#FFCCCCCC" />
                </Trigger>
            </Style.Triggers>
        </Style>

我的项目




   遇到这个问题怎么解决?

        <Style x:Key="PageTextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Height" Value="25" />
            <Setter Property="Width" Value="40" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Setter Property="Background">
                <Setter.Value>
                   <ImageBrush ImageSource="pack://siteoforigin:,,,/Images/Page_TextBack.png"  ></ImageBrush>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsReadOnly" Value="True">
                    <Setter Property="Background" Value="#FFCCCCCC" />
                </Trigger>
            </Style.Triggers>
        </Style>


   协议:pack://
     授权:有两种。一种用于访问编译时已经知道的文件,用application:///一种用于访问编译时不知道、运行时才知道的文件,用siteoforigin:///。在这里加载图片时,我们选用前者,即application:///,但是书写时候,我们一般用逗号代替斜杠,也就是改写作application:,,,
     路径:分为绝对路径和相对路径。这里我们选用相对路径,普适性更强。

     下面,我们举一个简单的例子:
          pack://application:,,,/images/my.jpg
     当然,WPF默认Uri设置有pack://application:,,,,所以我们也可以直接将其写作:
          /images/my.jpg
     后边写例子程序时,为了让读者更好的了解Uri,我们都采用完整的Uri写法。

     下面在讲讲装载图片的两种方式,一种用XAML引用资源,一种用代码引用资源。

     用XAML引用资源:
          <Image Source="pack://application:,,,/images/my.jpg"/>

     用代码引用资源:
          Image img;
          img.Source=new BitmapImage(new Uri("pack://application:,,,/images/my.jpg"),UriKind.Relative);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值