我在F#中通过FsXaml使用WPF。MainWindow工作,直到我添加了一个WindowsFormsHost控件,此时它将在执行时出现以下错误:
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:
<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 发布于 2016-05-23 06:24:58
尝试显式添加名称空间(使用clr-namespace),与winforms控件本身的方式相同--只需使用System.Windows.Forms.Integration。我假设默认的XAML名称空间中没有包含它:)
xmlns:wfi="clr-namespace:System.Windows.Forms;assembly=WindowsFormsIntegration"https://stackoverflow.com/questions/37382234
复制相似问题