【发布时间】:2018-04-24 16:13:34
【问题描述】:
我正在尝试使用三个程序集“模型”、“视图模型”和“视图”设置 Xamarin.Forms 解决方案,以根据 MVVM 样式分隔我的代码,就像我以前在 Windows WPF 应用程序中经常做的那样。
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。 类型 vm:MainPageViewModel not found in xmlns clr-namespace:MyApp3.ViewModel;assembly=MyApp3.ViewModel
代码:
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>
我只是错过了一些隐藏的设置,还是这是一个限制,所以我必须将我的代码保存在一个程序集中?
【问题讨论】:
标签: ios mvvm xamarin.forms .net-assembly