我无法观察到 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/