1.安装对应dll
Update-Package Xamarin.Forms
选中Android项目及PCL项目:Install-Package Xamarin.Forms.Labs
2.设置系统相关信息,右键android项目-属性
3.设置应用的名称
4.在MainActivity设置IOC
添加变量:private static bool _initialized;
修改MainActivity集成自:XFormsApplicationDroid
添加方法:
private void SetIoc() { var resolverContainer = new SimpleContainer(); var app = new XFormsAppDroid(); app.Init(this); resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice) .Register<IDisplay>(t => t.Resolve<IDevice>().Display) .Register<IDependencyContainer>(resolverContainer) .Register<IXFormsApp>(app); Resolver.SetResolver(resolverContainer.GetResolver()); _initialized = true; }
在OnCreate的base.OnCreate(bundle); 语句下添加
if (!_initialized) { this.SetIoc(); }
如下图:
时间: 2024-11-25 14:45:08