wpf - 无法加载 FsXaml.Wpf.TypeProvider

标签 wpf mvvm f# fsxaml fsharp.viewmodule

我正在尝试将 WPF 与 F# 一起使用。我已经使用 F# 空 Windows 应用程序模板创建了一个项目(事实上,我已经使用不同的设置创建了多个项目)。然后我添加了 FSharp.ViewModule 和 FsXaml.Wpf 引用。当项目只有 MainWindow.xaml 和 MainWindow.xaml.fs 时,它工作正常。但是,一旦我添加了另一个 .xaml View 文件和 .fs ViewModel 文件,我就会在 .fs 文件顶部的“命名空间 ViewModels”声明中收到以下错误:

The type provider '...\packages\FsXaml.Wpf.2.1.0\lib\net45\FsXaml.Wpf.TypeProvider.dll' reported an error: Assembly attribute 'TypeProviderAssemblyAttribute' refers to a designer assembly 'FsXaml.Wpf.TypeProvider' which cannot be loaded or doesn't exist. Could not load file or assembly 'file:///...\packages\FsXaml.Wpf.2.1.0\lib\net45\FsXaml.Wpf.TypeProvider.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

“FsXaml加载错误”帖子中描述了类似的问题(我无法添加评论,因为我没有足够的声誉)。但我已经尝试了那里给出的两种答案 - 更改 CPU 和 F# 运行时 - 但都不起作用。

我在 Windows 7 SP1 64 位虚拟机中使用 Visual Studio 2015 Community 版本。我尝试过使用以下框架和 DLL 版本:

  • NET Framework 4.5 和 4.6
  • F# 3.0 和 4.0
  • FSharp.ViewModule 0.9.9 和 0.9.9.3
  • FsXaml.Wpf 2.0.0 和 2.1.0

编辑1: 为了回复下面的里德评论,这里是我的两个显示错误的代码文件。首先,MainWindow.xaml.fs:

namespace ViewModels
open FSharp.ViewModule
open FsXaml

type MainView = XAML<"MainWindow.xaml", true>

type MainViewModel() as self = 
    inherit ViewModelBase()    

    let text = self.Factory.Backing (<@ self.Text @>, "")

    let newCommand = self.Factory.CommandSync (fun _ -> self.Text <- "File > Open")
    let openCommand = self.Factory.CommandSync(fun _ -> self.Text <- "File > Open")
    let closeCommand = self.Factory.CommandSync(fun _ -> self.Text <- "File > Close")
    let exitCommand = self.Factory.CommandSync(fun _ -> self.Text <- "File > Exit")

    member x.Text with get() = text.Value and set value = text.Value <- value

    member x.NewCommand = newCommand
    member x.OpenCommand = openCommand
    member x.CloseCommand = closeCommand
    member x.ExitCommand = exitCommand

这是 Dialog.fs:

namespace ViewModels
open FSharp.ViewModule

type DialogView = XAML<"Dialog.xaml", true>

type DialogVM() as self =
    inherit ViewModelBase()    

    let name = self.Factory.Backing (<@ self.Name @>, "")
    let address = self.Factory.Backing (<@ self.Address @>, "")

    member x.Name with get() = name.Value and set value = name.Value <- value
    member x.Address with get() = address.Value and set value = address.Value <- value

在这两个文件中,我在命名空间 ViewModelsXAML 下都看到了红色的波浪线。该项目将在只有 MainWindow.xaml.fs 时构建,但自从我添加 Dialog.fs 后就不会构建

编辑2: 这是我的 .fsproj 文件

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>b765eaca-f0ed-4898-bfea-fa19fca3788d</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <RootNamespace>Test</RootNamespace>
    <AssemblyName>Test</AssemblyName>
    <targetframeworkversion>v4.5</targetframeworkversion>
    <TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
    <Name>Test</Name>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <Tailcalls>false</Tailcalls>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <WarningLevel>3</WarningLevel>
    <PlatformTarget>x86</PlatformTarget>
    <DocumentationFile>bin\Debug\Test.XML</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <Tailcalls>true</Tailcalls>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <WarningLevel>3</WarningLevel>
    <PlatformTarget>x86</PlatformTarget>
    <DocumentationFile>bin\Release\Test.XML</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup>
    <MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
  </PropertyGroup>
  <Choose>
    <When Condition="'$(VisualStudioVersion)' == '11.0'">
      <PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
        <FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
      </PropertyGroup>
    </When>
    <Otherwise>
      <PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
        <FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
      </PropertyGroup>
    </Otherwise>
  </Choose>
  <Import Project="$(FSharpTargetsPath)" />
  <ItemGroup>
    <None Include="Dialog.xaml" />
    <Compile Include="Dialog.fs" />
    <Resource Include="MainWindow.xaml" />
    <Compile Include="MainWindow.xaml.fs" />
    <Resource Include="App.xaml" />
    <Compile Include="App.fs" />
    <Content Include="App.config" />
    <Content Include="packages.config" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Accessibility" />
    <Reference Include="FSharp.ViewModule.Core.Wpf">
      <HintPath>..\..\packages\FSharp.ViewModule.Core.0.9.9.1\lib\net45\FSharp.ViewModule.Core.Wpf.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="FsXaml.Wpf">
      <HintPath>..\..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="FsXaml.Wpf.TypeProvider">
      <HintPath>..\..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.TypeProvider.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="mscorlib" />
    <Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <Private>True</Private>
    </Reference>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="PresentationUI" />
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Numerics" />
    <Reference Include="System.Windows.Interactivity">
      <HintPath>..\..\packages\Expression.Blend.Sdk.1.0.2\lib\net45\System.Windows.Interactivity.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Xaml" />
    <Reference Include="System.Xml" />
    <Reference Include="UIAutomationProvider" />
    <Reference Include="UIAutomationTypes" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
</Project>

编辑 3:App.fs 给出错误“未定义字段、构造函数或成员“Root”。

module main
open System
open FsXaml

type App = XAML<"App.xaml">

[<STAThread>]
[<EntryPoint>]
let main argv =
    App().Root.Run()

packages.config 给出警告“'packages' 元素未声明”。

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Expression.Blend.Sdk" version="1.0.2" targetFramework="net45" />
  <package id="FSharp.ViewModule.Core" version="0.9.9.3" targetFramework="net45" />
  <package id="FsXaml.Wpf" version="2.1.0" targetFramework="net45" />
</packages>

最佳答案

在当前版本的 FsXaml(2.1 和 2.0)中,您不再包含第二个静态参数:

 type MainView = XAML<"MainWindow.xaml"> // Remove the 2nd arg here ... 

另外,在Dialog.fs中,您需要添加open FsXaml:

namespace ViewModels
open FSharp.ViewModule
open FsXaml // This is probably causing the specific error you're seeing

type DialogView = XAML<"Dialog.xaml"> // Again, remove the true

我建议使用 F# 4(需要 3.1 或更高版本)以及最新的 FsXaml 和 FSharp.ViewModule。

关于wpf - 无法加载 FsXaml.Wpf.TypeProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838357/

相关文章:

c# - 根据 ListView 中的位置触发按钮可见性

wpf - 在 XAML 中绑定(bind)到窗口事件

c# - 将 CheckBox IsChecked 属性绑定(bind)到 ListView 的 SelectedItems 属性

c# - Prism:如何从内存中删除现有的 View 实例

c# - F# NativePtr.stackalloc 比 C# stackalloc 慢 - 包含反编译代码

.net - 使用 SetValue 添加事件处理程序

.net - WPF - 如何强制命令通过其 CommandBindings 重新评估 'CanExecute'

wpf - 在 WPF 文本框中使用箭头键 (C#)

c#-4.0 - 来自 C# 的 MailboxProcessor<T>

f# - 如何使用WebSharper翻译器?