F# ViewModel 未通过 XAML 中的 BindingContext 实例化

标签 f# xamarin.forms

我无法观察到 F# 库中的 View 模型通过 XAML 中的 ContextBinding 进行实例化。

相反,我只观察到我的应用程序挂起(即黑屏)。

注意:

我无法引用包含 View 模型的实际 F# 项目(这是一个 PCL)。作为解决方法,我浏览到包含 View 模型的 DLL 路径(即 bin\debug)。

在排除故障时,我确实观察到 C# 项目中的 View 模型被实例化。但是,我的 F# 项目中的 View 模型却没有。

我的 XAML 如下:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:home="clr-namespace:Home.ViewModels;assembly=Home"
             x:Class="FnTrade.MainPage">

    <ContentPage.BindingContext>
        <home:HomeViewModel />
    </ContentPage.BindingContext>
</ContentPage>

我在 F# 项目中的 View 模型如下:

namespace Home.ViewModels

type HomeViewModel() =

    let foo = "Hello World" // Breakpoint never gets hit

在引用 C# 实现时,我的 View 模型确实通过 XAML 进行实例化:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:FnTrade;assembly=FnTrade"
             x:Class="FnTrade.MainPage">

    <ContentPage.BindingContext>
        <local:ViewModel />
    </ContentPage.BindingContext>
</ContentPage>

这是 C# 中的 View 模型:

namespace FnTrade
{
    public class ViewModel
    {
        public ViewModel() { } // Breakpoint DOES get hit
    }
}

有人可以向我解释为什么我用 F# 编写的 View 模型没有被实例化吗?

最佳答案

我将 FSharp.Core 添加到我的 Xamarin.Forms 项目以及我的平台项目(即 Droid)中。

我还确保两个项目中都存在 app.config 文件。

我将 app.config 文件更新为以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a"
                          culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-999.999.999.999" newVersion="4.4.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

关于F# ViewModel 未通过 XAML 中的 BindingContext 实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41922182/

相关文章:

f# - F# 中是否存在与 DU 的 [<RequireQualifiedAccess>] 属性等效的 OCaml?

azure - 在隔离进程 Azure 函数中将 C# HostBuilder 管道转换为 F#

java - 支持/整合语言测量单位的策略是什么?

generics - 由于 "ambiguity",F# 通用约束不起作用

visual-studio - MonoDevelop F# 类型和函数签名的工具提示;它们存在吗?

opencv - 如何在 Xamarin 中使用 openCV?

xaml - Xamarin Forms - Listview viewcell 使用 xaml 向左和向右滑动事件

xamarin - Xam 表单中的 ModernHttpClient。在最新版本中是否有必要?

c# - 回收 ListView 中这个看不见的、吞噬性能的单元格是什么?

xamarin.forms - 如何更改标题在导航栏中的位置