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

时间:2017-01-29 14:49:17

标签: f# xamarin.forms

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

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

注意:

我无法引用包含viewmodel的实际F#项目(也就是PCL)。作为一种解决方法,我浏览了包含viewmodel的DLL(即bin \ debug)的路径。

在进行故障排除时,我确实发现C#项目中的视图模型已实例化。但是,我的F#项目中的视图模型没有。

我的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#项目中的viewmodel如下:

namespace Home.ViewModels

type HomeViewModel() =

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

在引用C#实现时,我的viewmodel会通过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#中的viewmodel:

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

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

1 个答案:

答案 0 :(得分:2)

我将 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>