Blend 4 和 WPF 4.0 中奇怪的 UserControl 图像路径行为

发布于 2024-10-07 18:45:01 字数 1104 浏览 0 评论 0原文

我正在使用 Blend 4 和 VisualStudio 2010 构建一个具有类似向导功能的信息亭 WPF 4.0 应用程序。我决定将各个步骤移至单独的用户控件,以保持应用程序的主窗口尽可能精简。但是,我在将 UserControl 包含到主窗口中时遇到了问题,因为将 XAML 移动到单独的 UserControl 文件后图像路径被破坏。

当将此代码放入主窗口 XAML 中时,它可以工作:

<Button Grid.Row="1" Grid.Column="2" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Margin="20" VerticalAlignment="Bottom" Style="{DynamicResource ImageButton}">
  <Button.Background>
    <ImageBrush ImageSource="/Images/Extra/motor_boat_2.jpg" Stretch="UniformToFill"/>
  </Button.Background>
</Button>

但是当我将该代码移动到单独的用户控件并将其包含在主窗口中时,设计器会抛出异常:

IOException:无法找到资源“images/extra/sailing_boat.jpg”。

我尝试过使用几种不同的语法,但都失败了,有时使用 %ProgramFiles% 目录,有时将绝对路径转换为相对路径(如上所述)。有时,Blend 4 中的“构建项目”操作会因此失败。

所有图像都标记为资源,并将构建操作设置为始终复制

VisualStudio 构建和运行应用程序没有问题,用户控件在运行的应用程序中正确显示,没有抛出异常。

编辑:

在 UserControl 中使用相对路径,然后从 VisualStudio 构建并运行应用程序,然后切换回 Blend 正确显示图像,直到对 UserControl 进行下一次修改。

I'm using Blend 4 and VisualStudio 2010 to build a kiosk WPF 4.0 app that has a wizard-like function. I've decided I'd like individual steps moved to separate UserControls, to keep the main window of the application as lean as possible. However, I'm having trouble including the UserControl into the main window, since image paths are being mangled after moving the XAML to a separate UserControl file.

This code works when it's put into the main window XAML:

<Button Grid.Row="1" Grid.Column="2" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Margin="20" VerticalAlignment="Bottom" Style="{DynamicResource ImageButton}">
  <Button.Background>
    <ImageBrush ImageSource="/Images/Extra/motor_boat_2.jpg" Stretch="UniformToFill"/>
  </Button.Background>
</Button>

But when I move that code to a separate user control and include it in the main window, the designer throws and exception:

IOException: Cannot locate resource 'images/extra/sailing_boat.jpg'.

I've tried using a couple of different syntaxes, but all fail, sometimes using the %ProgramFiles% directory, other times converting the absolute path to relative (as above). Sometimes, the Build Project operation in Blend 4 fails because of this.

All the images are marked as Resource with Build Action set to Copy Always.

VisualStudio has no problem building and running the application, the UserControl displays correctly within the running application with no thrown exceptions.

EDIT:

Using relative paths in the UserControl and then building and running the app from VisualStudio and switching back to Blend correctly displays the images, until the next modification to the UserControl.

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

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

发布评论

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

评论(1

以往的大感动 2024-10-14 18:45:01

使用Pack URI,如下所示:

Uri uri = new Uri("pack://application:,,,/Images/Extra/motor_boat_2.jpg");

Use Pack URI like following:

Uri uri = new Uri("pack://application:,,,/Images/Extra/motor_boat_2.jpg");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文