C#WPF项目参照F#视图模型(C# WPF project with reference to F

2019-10-17 09:09发布

我有项目命名FSharpProject在whick有这样的代码:

module FSharpProject.ViewModel

type A =
    member x.a = 1

在其他项目(在C#典型的WPF应用程序写入),其中有参考FSharpProject我有这样的XAML文件:

<UserControl x:Class="CSharpProjectView"
             x:Name="Root"
             xmlns:local="clr-namespace:CSharpProjectView"
             xmlns:data="clr-namespace:FSharpProject.ViewModel;assembly=FSharpProject"            
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
<DataTemplate x:Key="LogDataTemplate" DataType="{data:A}">
                <TextBlock Text="{Binding a}" />                
            </DataTemplate>

但我得到错误信息:未找到A型。


UPD:它不工作:

<UserControl x:Class="CSharpProjectView"
             x:Name="Root"
             xmlns:local="clr-namespace:CSharpProjectView"
             xmlns:data="clr-namespace:FSharpProject;assembly=FSharpProject"            
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
<DataTemplate x:Key="LogDataTemplate" DataType="{data:ViewModel.A}">
                <TextBlock Text="{Binding a}" />                
            </DataTemplate>

Answer 1:

你的DataType应该是简单的data:A{x:Type data:A}

<DataTemplate x:Key="LogDataTemplate" DataType="data:A">


文章来源: C# WPF project with reference to F# ViewModel
标签: c# wpf f#