2017-06-21 73 views
2

我製作了一個Xamarin Forms應用程序,解決方案名爲RESTTest,我的共享項目名爲RestAppXamarin - 在xmlns中找不到類型clr-namespace

在我的共享項目中,我有一個名爲ViewModels文件夾,其中包含一個名爲MainViewModel.cs

我有一個叫MainPage.xaml頁面其中有一個代碼隱藏稱爲MainPage.xaml.cs類。在我的XAML我想包括我的ViewModels文件夾是這樣的:

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

但是當我添加像這樣綁定到我的網頁:

<ContentPage.BindingContext> 
     <ViewModels:MainViewModel /> 
    </ContentPage.BindingContext> 

我得到一個未處理的異常:

Type ViewModels:MainViewModel not found in xmlns clr-namespace:RestApp.ViewModels;assembly=RestApp 

我錯過了什麼?

enter image description here

回答

0

這時候你有修剪無用的代碼鏈接的優化通常發生。

請注意,反序列化類型可能無法被鏈接器檢測爲類型用途。

在您的項目的屬性中找到「iOS Build」或「Android Build」下的「Linker Behavior」選項,並將其設置爲「僅鏈接SDK程序集」或「不鏈接」。您需要清理+重建您的整個解決方案才能使更改生效。

+0

Hi Sten,在我的'Androiding'中的'Linking'目前設置爲'None'是正確的,我沒有看到「Do not Link」選項 –

+0

@BarneyCmberss是的,這就是它應該的是。如果它仍然沒有找到它然後在你的主要方法在android應用程序中調用Xamarin.Froms.Init添加'object o = typeof(ViewModels.MainViewModel)'以確保類型可以達到編譯時間 –

+0

I嘗試每個鏈接排列,不幸的是這並沒有解決我的問題 –

0

刪除命名空間中的「; assembly = RestApp」,並將鏈接器行爲設置爲「僅鏈接SDK組件」,並解決了問題!

相關問題