扩展程序中WPF控件中内容文件的相对路径 [英] Relative path for Content file in WPF control in an extension

查看:118
本文介绍了扩展程序中WPF控件中内容文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序包创建并显示一个Wpf对话框.该对话框具有一个Image控件,该控件指向标记为Content的图像.在我的Vsix项目中,我确保映像与程序集一起部署(我已对此进行了验证).图片 标记如下:

My package creates and shows a Wpf dialog. The dialog has an Image control which points to an image which are marked as Content. In my Vsix project, I make sure the image is deployed together with the assembly (I have verified this). The Image's markup is as follows:

< Image Source =" myimage.png" />

<Image Source="myimage.png" />

但是,在运行时,图像无法正确显示.如果我将图像的内容类型更改为资源",则会显示出来.由于我的项目的具体情况,我更喜欢使用Content而不是Resources.

However, at runtime, the image doesn't display correctly. If I change the content type of the image to Resources, then it shows up. Due to the specific situation of my project, I prefer to use Content rather than Resources.

所以我的问题是:作为内容类型的内容类型我在做什么,还是根本不支持它?

So my question is: What am I doing wrong with the content type as Content, or is it simply not supported at all?

推荐答案

我对Image控件的内部工作不是很熟悉,但是考虑到程序包的组装位置可能会有所不同,并且所涉及的应用程序实际上是devenv.exe,因此您可能不应该依赖于这种情况的相对路径.

I'm not very familiar with the internal workings of the Image control, but given that the package assembly location may well vary, and that the app in question is actuall devenv.exe, you probably shouldn't be relying on a relative path for this scenario.

如果您确实需要从外部文件(未将.jpg编译到您的资源中)加载此图像,则我可以提出的最佳建议是自定义绑定,该绑定将计算执行路径组装(并提供路径 相对于执行组件).例如:

If you do need to load this image from an external file (where the .jpg isn't compiled into your resources), the best recommendation I can make would be a custom binding, that would calculate the path to the executing assembly (and provide the path relative to the executing assemly). For example:

< x : Class    xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
  
xmlns : = http://schema s.microsoft.com/winfx/2006/xaml
  
xmlns
: = xmlns : = http://schemas .microsoft.com/expression/blend/2008
   xmlns : vsfx   mc : 无法理解 d : ="300" : DesignWidth 名称 ="MyToolWindow"
 背景 =""{ DynamicResource x : vsfx : VsBrushes   < 网格 >
    
< StackPanel =垂直">
       
< TextBlock ="10" Horizo​​ntalAlignment =" Center" 前景 =""{ DynamicResource x : vsfx : VsBrushes 此 是具有WPF内容的工具窗口 </ TextBlock >
       ;  < 按钮 内容 =" _ Click Me!" 宽度 ="80" 高度 ="80" 名称 ="button1" 点击 =" button1_Click"/>
       ;  < 图片 高度 ="150" 名称 ="image1" 拉伸 =填充" 宽度 ="200" 来源 = ElementName = MyToolWindow, 路径 = ImageFile}" />
      </ StackPanel >
  </ 网格 >
</
UserControl >

<UserControl x:Class="Microsoft.SimpleToolWindow.MyControl"
  
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
  
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
  
xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
  
xmlns:d=http://schemas.microsoft.com/expression/blend/2008
   xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.10.0"
   mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Name="MyToolWindow"
   Background="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowBackgroundKey}}">
   <Grid>
     
<StackPanel Orientation="Vertical">
        
<TextBlock Margin="10" HorizontalAlignment="Center" Foreground="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowTextKey}}">This is a toolwindow with WPF content</TextBlock>
         <Button Content="_Click Me!" Width="80" Height="80" Name="button1" Click="button1_Click" />
         <Image Height="150" Name="image1" Stretch="Fill" Width="200" Source="{Binding ElementName=MyToolWindow, Path=ImageFile}" />
      </StackPanel>
   </Grid>
</
UserControl>

然后在您的XAML控件上具有以下属性:

Then have  property on your  XAML control that looks something like:

 

 

public string ImageFile
{
  得到
   {
    
string assemblyPath = System.IO.Path.GetDirectoryName(System.IO.Path.Combine(Assembly.GetExecutingAssembly().Location));
      return System.IO.Path.Combine(assemblyPath, "images\\penguins.jpg");
   }
}

public string ImageFile
{
   get
   {
     
string assemblyPath = System.IO.Path.GetDirectoryName(System.IO.Path.Combine(Assembly.GetExecutingAssembly().Location));
      return System.IO.Path.Combine(assemblyPath, "images\\penguins.jpg");
   }
}

Also, in addition to setting the  "Build Action" property to "Content", if you also set the "Include in VSIX" property to "True", the .jpg should be property copied beneath the location of the package assembly.

Also, in addition to setting the  "Build Action" property to "Content", if you also set the "Include in VSIX" property to "True", the .jpg should be property copied beneath the location of the package assembly.

此致

 


这篇关于扩展程序中WPF控件中内容文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆