添加 WindowsFormsHost 引发 XamlParseException

Adding WindowsFormsHost throws XamlParseException

我试图在 MainWindow 中使用 FSharp.Charting 添加折线图。在 MainViewModel 我有 -

let chart = Chart.Line [ for i in 0 .. 10 -> (i, i * i) ]
let control = new ChartControl(chart)
member self.LineChart = control

并在 xaml -

<WindowsFormsHost Child="{Binding LineChart}" />

当我启动应用程序时,我得到 XamlParseException 以及以下附加信息 -

'Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}WindowsFormsHost'.' Line number '20' and line position '10'.

如何解决问题?


这里是@Foggy Finder。我删除了几行定义 TextBlock 和 Buttons 的代码。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
xmlns:local="clr-namespace:ViewModels;assembly=AsyncFS"
xmlns:fsxaml="http://github.com/fsprojects/FsXaml"
Title="MVVM and XAML Type provider" Height="200" Width="400">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <WindowsFormsHost Child="{Binding LineChart}" />
</Grid>

为了使用 WinfowsFormsHost,您需要添加对 WindowsFormsIntegration 的引用。但是如果你这样做,你会得到:

A 'Binding' cannot be set on the 'Child' property of type 'WindowsFormsHost'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

简单的修复方法是直接创建对象:

<ContentControl Content="{Binding LineChart}" />

...

member self.LineChart = new WindowsFormsHost(Child = control)

结果: