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