带有ImageControl的按钮会抛出'无效的URI:无法确定URI的格式' [英] Button with Image as UserControl throws 'Invalid URI: The format of the URI could not be determined'
问题描述
我正在开发一个小项目,我在其中引入了一个图像按钮作为用户控件。
有趣的是,它只是工作正常,但我收到一条错误消息我的MainWindow.xam:
'无效的URI:无法确定URI的格式'
包含路径信息的image属性被标记为无效( Image =\Images\icon_HMI_NewTask.png)。
但正如我所说,按F5工作正常,我正在按下正确的文字和图像。
但是我想避免任何与我现在看不到的问题。
请帮助!
将UserControl按钮添加到我的主页:
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:
<userControls:LargeMenuButton Label="New Decision" Image="\Images\icon_HMI_NewDeci.png" />
控件由XAML部分组成:
The control consists of the XAML part:
<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>
这里是CS部分:
And here is the CS part:
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
}
}
推荐答案
根据OP对上述评论的回复添加此答案:
您是否尝试过包URI [ ^ ]?
基本上,请尝试:
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.
这篇关于带有ImageControl的按钮会抛出'无效的URI:无法确定URI的格式'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!