【问题标题】:Xamarin.Forms ViewModel in markup doesnt load dll标记中的 Xamarin.Forms ViewModel 不加载 dll
【发布时间】:2016-11-11 20:07:57
【问题描述】:

我正在尝试在 Xamarin.Forms App.xaml 的标记中定义视图模型类。

<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 中声明并初始化这个类,它就可以正常工作。为什么这会加载在代码后面而不是标记中?

【问题讨论】:

    标签: c# xaml mvvm xamarin


    【解决方案1】:

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

    您可以阅读有关链接器及其不同设置的更多信息here

    【讨论】:

    • 这行得通,但现在我得到 Xamarin.Forms.Xaml.XamlParseException: Position 10:6。无法分配属性“资源”:“MyPCL.Client.ViewModels.MainViewModel”和“Xamarin.Forms.ResourceDictionary”之间的类型不匹配
    • @shady 请参阅stackoverflow.com/questions/40518329/… 以了解您上面评论中描述的错误的解决方案。
    猜你喜欢
    • 2012-10-03
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多