c# - 从 Windows 窗体应用程序打开 wpf 窗口时缺少图像

标签 c# .net wpf

我正在尝试从 Windows 窗体应用程序打开 WPF 窗口。 Windows 加载,WPF 窗口的功能正常,但我添加到 WPF 窗口中某些按钮的图像没有显示。图像的构建操作属性已设置为“资源”。我正在使用以下代码从 WinForm 应用程序加载:

private void button1_Click(object sender, EventArgs e)
{
    var wpfwindow = new WPF.View.MainWindow();
    wpfwindow.Show();
}

我能做些什么来解决这个问题?

最佳答案

在 WinForms 中没有 WPF System.Windows.Application 处于事件状态,因此无法识别“pack”uri 前缀,并且无法加载引用为“pack://some image uri”的资源图像。

要么注册uri前缀:

if (!UriParser.IsKnownScheme("pack"))
{
    UriParser.Register(new GenericUriParser
        (GenericUriParserOptions.GenericAuthority), "pack", -1);
}

或者尝试创建一个 Application 的实例类

关于c# - 从 Windows 窗体应用程序打开 wpf 窗口时缺少图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122075/

相关文章:

c# - WPF, Prism : How to access non-static method in other module?

c# - 如何重新查询单个 RoutedCommand 的 'CanExecute'?

wpf - 按钮周围的虚线边框

c# - 如何结合自定义序列化程序使用/启用 LINQ? (MONGODB C#)

c# - 如何输入整数数组

c# - 如何在C#进程和非托管C进程之间共享快速内存?

c# - .NET 4.7.1 项目无法使用 .NET Core 2.0 库

c# - 我需要等待回调吗?

c# - 为什么我在解析 XML 文档时得到空值

C# TCP Async EndReceive() 仅在 Windows XP 32 位上抛出 InvalidOperationException