【问题标题】:Xamarin - Type not found in xmlns clr-namespaceXamarin - 在 xmlns clr 命名空间中找不到类型
【发布时间】:2017-06-21 00:48:14
【问题描述】:

我正在制作一个 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

我错过了什么?

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    删除命名空间中的“;assembly=RestApp”,并将链接器行为设置为“仅链接 SDK 程序集”对我有用并解决了问题!

    【讨论】:

      【解决方案2】:

      这通常发生在您对未使用的代码进行链接器优化时。

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

      在项目的属性中找到“iOS Build”或“Android Build”下的“Linker Behavior”选项,并将其设置为“Link SDK assembly only”或“Don't Link”。您需要清理+重建整个解决方案才能使更改生效。

      【讨论】:

      • 嗨 Sten,在我的 Linking Android Options 中,它当前设置为 None 是正确的,我没有看到“不链接”选项
      • @BarneyChambers 是的,应该是这样。如果仍然找不到它,那么在调用 Xamarin.Froms.Init 的 android 应用程序的 main 方法中添加“object o = typeof(ViewModels.MainViewModel)”以确保可以在编译时达到该类型跨度>
      • 我尝试了所有链接排列,不幸的是这并没有解决我的问题
      • 我添加了硬编码参考,但仍然看到错误
      • 检查 1. 对文件设置的操作是编译和 2. 您的共享项目在您的主项目中被引用。 3.你的程序集名称是RestApp吗?如果您使用 PCL,它会是,但因为它是共享项目,它实际上可能是 RestApp.Android(不确定)?
      【解决方案3】:
      <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 部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-19
        • 2017-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2017-10-09
        相关资源
        最近更新 更多