将WindowsFormsHost控件添加到MainWindow导致exe崩溃 [英] adding WindowsFormsHost control to MainWindow causes to crash the exe

查看:80
本文介绍了将WindowsFormsHost控件添加到MainWindow导致exe崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过FsXaml在F#中使用WPF.MainWindow一直有效,直到我添加WindowsFormsHost控件为止,这时执行时它将崩溃并显示以下错误:

I'm using WPF in F# via FsXaml. The MainWindow works, until I add a WindowsFormsHost control, at which point it will crash with the following error when executed:

Unhandled Exception: System.Xaml.XamlObjectWriterException: Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}WindowsFormsHost'.
   at System.Xaml.XamlObjectWriter.WriteStartObject(XamlType xamlType)
   at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
   at FsXaml.InjectXaml.from$cont@37(String file, Object root, Stream resStream, Unit unitVar)
   at FsXaml.InjectXaml.from(String file, Object root)
   at Program.Win.InitializeComponent()
   at Program.Win..ctor()

我需要添加xaml或应用程序来识别winformshost吗?C#生成的xaml可在C#应用程序中使用.如果其中没有winformshost,则F#应用程序可以工作.C#和F#xamls之间的区别是x-local名称空间引用.

Is there anything I need to add the xaml or the app to recognize the winformshost? The C# generated xaml works in a C# app. The F# app works if there is no winformshost inside it. The difference between the C# and F# xamls is the x-local namespace reference.

MainWindow.xaml:

The MainWindow.xaml:

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="124" VerticalAlignment="Top" Width="217"/>
        <WindowsFormsHost HorizontalAlignment="Left" Height="123" Margin="49,171,0,0" VerticalAlignment="Top" Width="394"/>

    </Grid>
</Window>

App.xaml

<Application 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
 </Application>

Program.fs:

Program.fs:

open System
open System.Windows
open FsXaml
open Microsoft.FSharp.Control
open System.Windows.Forms
open System.Windows.Forms.Integration
open FSharp.Charting
open FSharp.Charting.ChartTypes

type App = XAML<"App.xaml">
type Win = XAML<"MainWindow.xaml">

[<STAThread>]
[<EntryPoint>]            
let main _ = 
    let app = App()
    let win = Win() 
    win.button.Content <- "Click!"  
    win.button.MouseRightButtonDown.Add (fun _ -> MessageBox.Show("OK") |> ignore) 
    app.Run win |> ignore
    0 

推荐答案

尝试显式添加名称空间(使用 clr-namespace ),就像使用winforms控件本身一样-只需使用 System.Windows.Forms.Integration .我假设默认XAML命名空间中不包含该代码:)

Try explicitly adding the namespace (using clr-namespace), the same way you would for the winforms controls themselves - just use System.Windows.Forms.Integration. I assume that isn't included in the default XAML namespace :)

xmlns:wfi="clr-namespace:System.Windows.Forms;assembly=WindowsFormsIntegration"

这篇关于将WindowsFormsHost控件添加到MainWindow导致exe崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆