s952163
s952163

Reputation: 6324

adding WindowsFormsHost control to MainWindow causes to crash the exe

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()

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.

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>

The 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 

Upvotes: 1

Views: 660

Answers (1)

Luaan
Luaan

Reputation: 63722

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"

Upvotes: 3

Related Questions