打包 URI 和路径无法解析 WPF 中的图像

发布于 2024-08-15 01:37:06 字数 1434 浏览 3 评论 0原文

我有以下目录结构

Project
\Images
 +view.png
control.xaml

,并且在控件中我有一个由以下 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>

但是,此包 URI 方法和“/images/view.png”都不起作用。据我了解,这是同一个问题此问题 提出。但是,我遇到了同样的错误。令人困惑的是,在 Visual Studio 2008 的设计器中,图像渲染正确,但在调用 InitializeComponent() 时,我得到:

无法将属性“Source”中的字符串“pack://application:,,,/images/view.png”转换为“System.Windows.Media.ImageSource”类型的对象。无法找到资源“images/view.png”。标记文件“RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml”第 61 行第 40 行中的对象“System.Windows.Controls.ControlTemplate”出错。

我认为也许有一个我必须声明的命名空间,但根据 msdn site 我相信我不需要做任何事情像那样。

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 pack URI method nor the "/images/view.png" is working. As I understand it, this is the same issue this question raises. However, I get the same error. The confusing thing is that in designer in Visual Studio 2008, the image renders correctly, but on the call to the InitializeComponent() call, I get:

Cannot convert string 'pack://application:,,,/images/view.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'images/view.png'. Error at object 'System.Windows.Controls.ControlTemplate' in markup file 'RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml' Line 61 Position 40.

I thought that maybe there was a namespace that I had to declare but according to the msdn site I believe I don't have to do anything like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-08-22 01:37:06

我实际上让它工作,但必须将我的源设置为 /ProjectName;component/images/view.png 因为我将 ProjectName 作为引用的程序集,所以它与 Path: 部分相同在我在问题中引用的msdn页面。

I actually got this to work, but had to set my source to /ProjectName;component/images/view.png Because I have the ProjectName as a referenced assembly this is then the same as the Path: portion at the msdn page that I referenced in the question.

愁以何悠 2024-08-22 01:37:06

将“view.png”的构建操作设置为资源而不是内容,这个问题就会消失。我能够通过这种方式重现您的问题,并且当设置为资源时它可以正常工作。

Set the Build Action for 'view.png' to Resource instead of Content and this problem should go away. I was able to reproduce your problem this way and it works correctly when set as a Resource.

七秒鱼° 2024-08-22 01:37:06

Xaml.VB

从应用程序文件夹和设计页面中调用图像

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

Xaml.VB 中的页面加载

Call LoadImages()

Xaml 设计页面

Image Name="Image2"Height="32" HorizontalAlignment="Left" 

Xaml.VB

Call the Image from Application folder 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 load in Xaml.VB

Call LoadImages()

Xaml Design Page

Image Name="Image2"Height="32" HorizontalAlignment="Left" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文