首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将WindowsFormsHost控件添加到MainWindow会导致exe崩溃

将WindowsFormsHost控件添加到MainWindow会导致exe崩溃
EN

Stack Overflow用户
提问于 2016-05-23 03:29:23
回答 1查看 526关注 0票数 1

我在F#中通过FsXaml使用WPF。MainWindow工作,直到我添加了一个WindowsFormsHost控件,此时它将在执行时出现以下错误:

代码语言:javascript
复制
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-局部命名空间引用。

MainWindow.xaml:

代码语言:javascript
复制
<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>

The App.xaml

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-23 06:24:58

尝试显式添加名称空间(使用clr-namespace),与winforms控件本身的方式相同--只需使用System.Windows.Forms.Integration。我假设默认的XAML名称空间中没有包含它:)

代码语言:javascript
复制
xmlns:wfi="clr-namespace:System.Windows.Forms;assembly=WindowsFormsIntegration"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37382234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档