我制作了 WinForm 项目并添加了 ElementHost 以在某些图像上使用 WPF 混合效果。
ElementHost m_host = new ElementHost();
WPF_TK4S wpfTK4S = new WPF_TK4S();
m_host.Child = wpfTK4S;
m_host.Size = new Size(1000, 1000);
m_host.Location = new Point(0, 0);
this.Controls.Add(m_host);
我在 WPF 用户控件上添加了图像控件,如下所示。
<UserControl x:Class="Test.WPF_TK4S"
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"
mc:Ignorable="d"
d:DesignHeight="1000" d:DesignWidth="1000">
<Grid>
<Grid.Background>
<ImageBrush/>
</Grid.Background>
<Image Source="/Resources/TK4S_254X253.png" HorizontalAlignment="Left" Height="344" Margin="48,44,0,0" VerticalAlignment="Top" Width="342" Stretch="None"/>
</Grid>
该图像在控制视图上显示良好,但现在在我运行应用程序时显示。幸运的是,在我将源 uri 更改为绝对 uri 之后
"D://Visual Project/Test/Test/Resources/TK4S_254X253.png"
图像显示得很好!我尝试使用各种相对 uri
".../Test/Resources/TK4S_254X253.png", "pack://application:,,,/TK4S_254X253.png"
但这是行不通的。我需要使用相对路径,因为项目在服务器上运行,所以每个项目路径都不同。而且我必须在 xaml 上使用这些东西,因为在初始化图像后我必须使用混合。我需要知道其他选择吗?