[英]How to set an image source in code-behind inside a library?
我有一個用C#為Windows 8編寫的類庫,帶有一個自定義控件和一個名為Images的文件夾,其中包含一堆圖像。 在控制模板中,我有一張圖片
<ControlTemplate TargetType="local:MyCustomControl">
<Grid Background="Transparent">
<Image x:Name="MyImage"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Stretch="Fill" />
...
</Grid>
</ControlTemplate>
我想在該控件的代碼中設置其中一個圖像(位於此類庫中)的后面,作為該圖像的源。 我已經嘗試過的:
將FrameworkElement.BaseUri
與“不復制”和“圖像的始終復制”屬性值一起使用的絕對uri:
this.MyImage.Source = new BitmapImage(new Uri(this.BaseUri, imagePath));
具有“不復制”和“圖像始終復制”屬性值的相對uri:
this.MyImage.Source = new BitmapImage(new Uri(imagePath, UriKind.Relative));
ms-resource
uri方案。
我該如何實現?
好的,那很快就升級了。 :)也許會對他人有用。 解決方案是在路徑中使用庫的程序集名稱:
var assemblyName = this.GetType().GetTypeInfo().Assembly.GetName().Name;
this.MyImage.Source = new BitmapImage(new Uri(this.BaseUri, assemblyName + imagePath));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.