【发布时间】:2016-07-17 12:13:25
【问题描述】:
我参考了this,编写了几乎相同的代码,但使用不同的语言,它没有按预期工作!
我使用 winform(C++/Cli) 作为主机 & 'WPF User Control Libaray'(C#) 作为子控件。
在 Winform 中使用 ElementHost 组件在 Winform 中集成 WPF 用户控件(PictureBox)。
基本上我想从winform按钮更改WPF控件中的图片。
它编译并运行良好。
但唯一的问题是即使图片路径正确,图片也不会改变。
下面的代码在 winform button_click 事件中
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
OpenFileDialog ^ofd = gcnew OpenFileDialog();
WpfControlLibrary1::UserControl1 ^uc = gcnew UserControl1();
if (ofd->ShowDialog() == Windows::Forms::DialogResult::OK)
uc->open(ofd->FileName);
}
以下代码在 UserControl1.xaml.cs 中
public void open(string path)
{
MessageBox.Show(path); //path seems to be fine
img.Source = new BitmapImage(new Uri(path));
}
下面的代码在 UserControl1.xaml 中
<UserControl x:Class="WpfControlLibrary1.UserControl1"
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="300" d:DesignWidth="300">
<Grid Margin="-54,0,0,0">
<Image x:Name="img" Stretch="Uniform" Opacity="1" Source="Koala.jpg"/>
</Grid>
</UserControl>
尝试在 xaml 代码中删除源图像,但没有效果
【问题讨论】:
标签: c# wpf winforms visual-c++ elementhost