Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I'm working on a small project where I introduced an image button as a user control.
The funny thing is, that it just works fine, but I'm getting an error message inside my MainWindow.xam:
'Invalid URI: The format of the URI could not be determined'
The image property including the path information is marked as invalid (Image="\Images\icon_HMI_NewTask.png").
But as I said, pressing F5 just works fine and I'm getting my button with the proper text and image.

But I would like to avoid any issues with it which I don't see right now.

Please help!

Adding a UserControl Button to my MainPage:
XML
<userControls:LargeMenuButton Label="New Decision" Image="\Images\icon_HMI_NewDeci.png" />


The control consists of the XAML part:
XML
<UserControl x:Class="OpenPointList.UserControls.LargeMenuButton"
             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" 
             Name="myLargeMenuButton"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="100">
    
    <StackPanel Orientation="Horizontal" x:Name="LayoutRoot" HorizontalAlignment="Center">
        <Button Height="52" >
            <StackPanel Orientation="Vertical" Width="Auto">
                <Image Source="{Binding Path=Image, ElementName=myLargeMenuButton}" Height="32" Width="32"/>
                <TextBlock Foreground="Black" FontSize="12" Text="{Binding Path=Label}" TextAlignment="Center" />
            </StackPanel>
        </Button>
    </StackPanel>
</UserControl>


And here is the CS part:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OpenPointList.UserControls
{
    /// <summary>
    /// Interaction logic for LargeMenuButton.xaml
    /// </summary>
    public partial class LargeMenuButton : UserControl
    {
        #region Fields

        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LargeMenuButton), new PropertyMetadata(""));
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(LargeMenuButton), new PropertyMetadata(null));

        #endregion

        #region Constructor

        /// <summary>
        /// Default Constructor
        /// </summary>
       public LargeMenuButton()
        {
            InitializeComponent();

            LayoutRoot.DataContext = this;
        }

        #endregion

        #region Custom Control properties

        /// <summary>
        /// Set and get for the text which shall be displayed on the button
        /// </summary>
        public string Label
        {
            get { return (String)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        /// <summary>
        /// Sets or gets the Image source for the icon which shall be displayed on the button
        /// </summary>
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        #endregion
     }
}
Posted
Comments
Agent__007 29-Dec-14 6:16am    
Did you try "Pack URIs"? link: http://msdn.microsoft.com/en-us/library/aa970069%28v=vs.110%29.aspx

Basically, try either:
"pack://application:,,,/Images/icon_HMI_NewDeci.png"
or
"/{YourProjectNameHere};component/Images/icon_HMI_NewDeci.png"
Carpi_1968 29-Dec-14 6:29am    
You are perfect!
That was the key to get it resolved.

1 solution

Adding this answer as per the OP's response to my comment above:

Did you try Pack URIs[^]?

Basically, try either:
pack://application:,,,/Images/icon_HMI_NewDeci.png

or
/{YourProjectNameHere};component/Images/icon_HMI_NewDeci.png


That should do the trick.
 
Share this answer
 
Comments
Carpi_1968 30-Dec-14 3:49am    
I tried your proposals and both of them are solving my issue.

Thanks again for your proper help.
Agent__007 30-Dec-14 3:56am    
You are most welcome. I am glad it helped. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900