Package URI and path to not allow image in WPF

I have the following directory structure

Project \Images +view.png control.xaml 

and in the control, I have a button defined by the following XAML:

 <Button Click="Search" Grid.Column="1" Margin="0,5,5, 0" HorizontalAlignment="Right"> <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Image Source="pack://application:,,,/images/view.png" Width="16" Height="16" ToolTip="Search" Cursor="Hand" Opacity="0.8" /> </ControlTemplate> </Button.Template> </Button> 

However, neither this package URI method nor "/images/view.png" work. As far as I understand, this is the same problem, this question is being raised. However, I get the same error. The confusing thing is that in Visual Studio 2008 the image is displayed correctly, but when I call the InitializeComponent () call, I get:

It is not possible to convert the string 'pack: // application: ,, / images / view.png' to the attribute 'Source' for an object of type 'System.Windows.Media.ImageSource'. Cannot find the resource 'images / view.png'. Error in the object "System.Windows.Controls.ControlTemplate" in the markup file "RecapSpecEditControl; component /modaltreadgroupdatadialog.xaml" Line 61 Position 40.

I thought that there might have been a namespace that I should have declared, but according to msdn site , I believe I don't need to do anything.

+4
source share
3 answers

I really got this to work, but I had to set my source to "/ProjectName;component/images/view.png". Since I have the project name as a link, this is the same as the Path: msdn part that I referenced in the question.

+9
source

Set the build action for "view.png" instead of a resource instead of a resource, and this problem should go away. I was able to reproduce your problem this way, and it works correctly when installed as a resource.

+6
source

Xaml.vb

Call the image from the folder "Application" and "Design page"

 Private Sub LoadImages() Dim strUri As String strUri = AppDomain.CurrentDomain.BaseDirectory() & "\NavigationImages\settingsicon.png" Image2.Source = New BitmapImage(New Uri(strUri)) End Sub 

Page loading in Xaml.VB

Call LoadImages()

Xaml Design Page

 Image Name="Image2"Height="32" HorizontalAlignment="Left" 
-3
source

All Articles