Relative path for Content file in WPF control in an extension RRS feed

  • Question

  • 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" />

    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?

    Wednesday, September 29, 2010 6:18 PM

All replies

  • 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.

    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:

    <UserControl x:Class="Microsoft.SimpleToolWindow.MyControl"
       mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Name="MyToolWindow"
       Background="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowBackgroundKey}}">
    <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}" />

    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");

    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.



    Ed Dore
    Thursday, October 7, 2010 6:47 PM