2016-11-11 26 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