2016-05-23 4 views
2

Я использую WPF в F # через FsXaml. MainWindow работает, пока я не добавить элемент управления WindowsFormsHost, в какой момент он будет врезаться со следующей ошибкой при выполнении:Добавление управления WindowsFormsHost к MainWindow приводит к сбою exe

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 [email protected](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#. Приложение F # работает, если в нем нет winformshost. Разница между C# и F # xamls - это ссылка на пространство имен x-local.

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:

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 

ответ

3

Try явного добавления пространства имен (с использованием clr-namespace), то же самое как вы бы для winforms сами контролируете - просто используйте System.Windows.Forms.Integration. Я полагаю, что не входит в XAML по умолчанию пространство имен :)

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

так для WinForms в: 'Xmlns: Wf = "clrnamespace: System.Windows.Forms; сборочные = System.Windows.Forms"' и winformshost как вы указали. отлично работал. – s952163

+0

Необходимо добавить 'Integration',' xmlns: wfi = "clr-namespace: System.Windows.Forms.Integration; assembly = WindowsFormsIntegration" ' – George

Смежные вопросы