Xamarin - 在xmlns clr-namespace中找不到类型

时间:2017-06-21 00:48:14

标签: c# xaml xamarin xamarin.forms

我正在制作一个Xamarin Forms应用程序,该解决方案名为RESTTest,我的共享项目名为RestApp

在我的共享项目中,我有一个名为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

3 个答案:

答案 0 :(得分:1)

当您使用可以修剪未使用代码的链接器优化时,通常会发生这种情况。

请注意,链接器可能无法将类型反序列化作为类型使用。

在项目的属性中,找到“iOS Build”或“Android Build”下的“链接器行为”选项,并将其设置为“仅限链接SDK程序集”或“不链接”。您需要清理+重建整个解决方案才能使更改生效。

答案 1 :(得分:1)

删除&#34 ;; assembly = RestApp&#34;在命名空间中,并将链接器行为设置为&#34;仅限链接SDK程序集&#34;为我工作并解决了问题!

答案 2 :(得分:0)

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cvt="clr-namespace:XamarinTest.UI.Resources.Converts;assembly=XamarinTest.UI">
    <cvt:DoubleToImageConverter x:Key="CVT_DoubleToImage"/>
    <cvt:MediaTypeToImageConverter x:Key="CVT_MediaTypeToImage"/>
</ResourceDictionary>

在我的应用中,我添加了 assembly=XamarinTest.UI 解决了问题。我认为开发团队可能会删除 Xamarin Forms 5.0 中的 assembly=xxxx 部分。

相关问题