2017-01-29 28 views
2

我無法觀察我的F#庫中的視圖模型通過XAML中的ContextBinding實例化。F#ViewModel沒有通過XAML中的BindingContext實例化

相反,我只觀察我的應用程序掛起(即黑屏)。

注:

我無法參考實際的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#項目中的視圖模型如下:

namespace Home.ViewModels 

type HomeViewModel() = 

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

我的視圖模型並通過XAML引用C#,當實例化實施:

<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#中的視圖模型:

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

可以給我這樣的人解釋爲什麼我寫在F#視圖模型不實例化?

回答

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> 
+0

你不是以前有類似的問題? –

+0

是的。然而,由於具有XAML依賴性,有用的異常正在被吞噬。我在使用代碼隱藏進行故障排除時發現了此問題。 –

+1

請注意,如果您在代碼中構造類型,通常這會更容易診斷,而不是xaml –

相關問題