2017-11-11 157 views
0

我想设置一个Xamarin.Forms解决方案,使用三个程序集“模型”,“视图模型”和“视图”来分离我的代码根据像我这样的MVVM风格往往在Windows WPF应用程序中。Xamarin.Forms绑定使用不同程序集的类

iOS和Android引导程序项目对这三个程序集中的每一个都有引用View-Assembly具有对ViewModel和Model的引用,ViewModel只引用Model。

我在View-Assembly中添加了一个简单的“MainPage”,并在ViewModel-Assembly中添加了匹配的“MainViewModel”。 一切都很好,Visual Studio中的Xamarin.Forms-Previewer显示了一个预览,其中标签文本通过绑定到ViewModel来显示。

在Android模拟器上,应用程序按预期工作。 在iOS上,它尝试显示页面时崩溃。

我想这有很多事情要做:iOS正在使用提前编译。或者,也许我在xaml中定义xmlns标记时会出错。

未处理的异常: Xamarin.Forms.Xaml.XamlParseException:位置8:10。

namespace MyApp3.ViewModel 
{ 
    public class MainPageViewModel 
    { 
     public string MyText { get; set; } = "Some Text"; 
    } 
} 

和页面:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:MyApp3" 
      xmlns:vm="clr-namespace:MyApp3.ViewModel;assembly=MyApp3.ViewModel" 
      x:Class="MyApp3.View.MainPage"> 
    <ContentPage.BindingContext> 
     <vm:MainPageViewModel /> 
    </ContentPage.BindingContext> 
    <ContentPage.Content> 
     <StackLayout> 
      <Label Text="{Binding MyText}" 
        VerticalOptions="CenterAndExpand" 
        HorizontalOptions="CenterAndExpand" /> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

难道我只是错过; 类型VM:装配= MyApp3.ViewModel

代码MyApp3.ViewModel:MainPageViewModel不 的xmlns CLR的命名空间中一些隐藏的设置,或者这是一个限制,所以我必须保持我的代码在一个大会内?

回答

0

是的,你可以做到这一点,检查名称。 检查视图模型的程序集名称是否被命名为MyApp3.ViewModel(在项目的属性中检查它)。