Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在库中的代码隐藏中设置图像源?_C#_Windows 8_Windows Runtime_Bitmapimage - Fatal编程技术网

C# 如何在库中的代码隐藏中设置图像源?

C# 如何在库中的代码隐藏中设置图像源?,c#,windows-8,windows-runtime,bitmapimage,C#,Windows 8,Windows Runtime,Bitmapimage,我有一个用C#for Windows 8编写的类库,其中有一个自定义控件和一个名为Images的文件夹,其中包含一堆图像。在控制模板中,我有一个图像 <ControlTemplate TargetType="local:MyCustomControl"> <Grid Background="Transparent"> <Image x:Name="MyImage" Height="{TemplateBinding

我有一个用C#for Windows 8编写的类库,其中有一个自定义控件和一个名为Images的文件夹,其中包含一堆图像。在控制模板中,我有一个图像

<ControlTemplate TargetType="local:MyCustomControl">
    <Grid Background="Transparent">
        <Image x:Name="MyImage"
               Height="{TemplateBinding Height}"
               Width="{TemplateBinding Width}"
               Stretch="Fill" />
               ...
    </Grid>
</ControlTemplate>
  • 具有图像的“不复制”和“始终复制”属性值的相对uri:

    this.MyImage.Source = new BitmapImage(new Uri(this.BaseUri, imagePath)); 
    
    this.MyImage.Source = new BitmapImage(new Uri(imagePath, UriKind.Relative));
    
  • ms资源
    uri方案


  • 我怎样才能做到这一点呢?

    好的,这很快就升级了。:)也许,对其他人会有用。 解决方案是在路径中使用库的程序集名称:

    var assemblyName = this.GetType().GetTypeInfo().Assembly.GetName().Name;
    this.MyImage.Source = new BitmapImage(new Uri(this.BaseUri, assemblyName + imagePath));