2016-11-11 60 views
0

我想在我的Xamarin.Forms App.xaml的标记中定义一个viewmodel类。Xamarin.Forms ViewModel在标记没有加载dll

<xml version="1.0" encoding="utf-8" ?> 
<Application xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:viewModels="clr-namespace:MyPCL.Client.ViewModels;assembly=MyPCL.Client" 
      x:Class="TestApp2.App"> 
    <Application.Resources> 
    <viewModels:MainViewModel x:Key="MainViewModel"/> 
    </Application.Resources> 
</Application> 

我得到这个错误: 未处理的异常:

System.IO.FileNotFoundException:未能加载文件或程序集 'MyPCL.Client' 或它的一个依赖。

但如果我声明并初始化这个类在我的app.xaml.cs,它工作正常。为什么这会在代码后面加载,而不是在标记中加载?

回答

1

Xamarin链接器会自动尝试通过删除未引用的库和类来最小化应用程序的大小。显然仅在XAML中声明它并不足以让链接器识别出存在引用(这可能是您应该用Xamarin提交的错误)。在App.xaml.cs中添加引用会强制链接器保留它。

您可以阅读更多关于链接器及其不同设置here

+0

这工作,但现在我得到Xamarin.Forms.Xaml.XamlParseException:位置10:6。无法分配属性“资源”:类型不匹配“MyPCL.Client.ViewModels.MainViewModel”和“Xamarin.Forms.ResourceDictionary” – shady

+0

@shady请参阅http://stackoverflow.com/questions/40518329/uncomfort-myapp-android-has -stopped-with-contentpage-resource-in-xaml-file/40519339#40519339上述评论中描述的错误的解决方案。 – matthewrdev