我是 MvvmCross 6.0 和 Xamarin 的新手。
我正在尝试遵循这里是 MvvmCrosss 5.5 的教程
我按照解释,
- 创建 App.xaml 作为 MvxFormsApplication
<?xml version="1.0" encoding="utf-8" ?>
<core:MvxFormsApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:core="clr-namespace:MvvmCross.Forms.Core;assembly=MvvmCross.Forms"
x:Class="App3.App">
</core:MvxFormsApplication>
- CoreApp.cs 作为 MvxApplication 并运行 RegisterAppStart();在我重写的Initialize()中
public class CoreApp : MvxApplication
{
public override void Initialize()
{
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
CreatableTypes()
.EndingWith("Client")
.AsInterfaces()
.RegisterAsLazySingleton();
// register the appstart object
RegisterAppStart<MainPageViewModel>();
}
}
- MainPageViewModel 继承 MvxViewModel
public class MainPageViewModel : MvxViewModel
{
}
- 创建为 MvxContentPage 且类型为 MainPageViewModel 的视图
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center" />
- 删除 MainActivity 并创建一个名为 MainApplication.cs 的文件,如下所示
[Activity(Label = "MvvmcrossGettingStarted", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | 配置更改 = ConfigChanges.ScreenSize |
ConfigChanges.Orientation)]
公共类MainActivity:MvxFormsAppCompatActivity
{
protected override void OnCreate(Bundle 捆绑)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
var startup = Mvx.Resolve<IMvxAppStart>();
startup.Start();
InitializeForms(bundle);
}
}
public class Setup : MvxFormsAndroidSetup
{
public Setup():base()
{
}
protected override IEnumerable<Assembly> AndroidViewAssemblies => new List<Assembly>(base.AndroidViewAssemblies
.Union(new[] { typeof(App).GetTypeInfo().Assembly })
.Except(new[] { this.GetType().Assembly })
);
protected override Application CreateFormsApplication()
{
return new App();
}
protected override IMvxApplication CreateApp() => new CoreApp();
}
然而,我启动应用程序时,它给了我 null 异常,说 OnCreated 方法中的“bundle”参数为 null。
附:本教程提到创建Setup.cs,但我不知道代码如何运行Setup.cs......我看不到在哪里引用它。