我的基本内容类.我将此类用作我的项目的主题.我不知道此信息是否相关.在这里,我创建一个抽象方法,该方法将使导航方法超载.
My base content class. I used this class as a theme for my project. I do not know this info relevant or not. In here I create an abstract method that would overload the navigation method.
public abstract class BaseContentPage : ContentPage { public readonly BaseViewModel BaseViewModel; public BaseContentPage(BaseViewModel baseViewModel) { BaseViewModel = baseViewModel; } public abstract void Navigate(SelectedItemChangedEventArgs e); }在我的依赖项中构建依赖项注入公共类的Locator.此类中的重点是将此类添加到容器中以进行所有代码丢失耦合
In my locator where I build the dependency Injection public class Locator. in this class mainly focus on adding this class to the container to make the all code loss coupling
private readonly ContainerBuilder _builder; public Locator() { _builder = new ContainerBuilder(); RegisterTypes(); Container = _builder.Build(); } public IContainer Container { get; set; } private void RegisterTypes() { _builder.RegisterType<WardListService>().As<IWardListService>(); _builder.RegisterType<WardListPageViewModel>(); _builder.RegisterType<WardListPage>(); _builder.RegisterType<PatientService>().As<IPatientService>(); _builder.RegisterType<PatientListPageViewModel>(); _builder.RegisterType<PatientListViewPage>(); _builder.RegisterType<PatientDetailsPageViewModel>(); _builder.RegisterType<PatientDetailsViewPage>(); }在我的 app.Xaml.Cs 文件中
public App() { InitializeComponent(); Locator locator = new Locator(); Container = locator.Container; MainPage = new NavigationPage(Container.Resolve<WardListPage>()); } public static IContainer Container;我在页面后面的视图代码中使用了这种方法进行导航
I used this method for navigation in my view code behind page
public async override void Navigate(SelectedItemChangedEventArgs e) { PatientDetailsViewPage patientDetailsViewPage = App.Container.Resolve<PatientDetailsViewPage>(); patientDetailsViewPage.BaseViewModel.SelectedPatient = e.SelectedItem as PatientViewModel; await Navigation.PushAsync(patientDetailsViewPage); }此代码运行良好,但只能导航到一页.例如,在一页上,我们有两个按钮可导航到两个不同的页面.我不知道如何使用上面的导航重载程序来实现此任务.任何人都可以提出解决该问题的建议?另外,我使用autofac进行依赖注入谢谢
This code is working perfectly but this only can navigate to one page.meaning as an example on one page we have two buttons that navigate to two different pages. I don't know how to implement this task using above navigate overloader. How to do it can anyone give suggestion to overcome the problem?. Also, I used autofac for dependency injection Thank you
推荐答案
您可以在CustomNavigationPage中定义容器,并在每个导航页面实例中使用.
You can define container in your CustomNavigationPage and use in every navigation page instance.
public class CustomNavigationPage : NavigationPage { public static IContainer Container; public CustomNavigationPage() { Locator locator = new Locator(); locator.RegisterTypes(); Container = locator.Container(); } }
我所说的是伪代码.
您创建了自定义的导航页面.因此,您可以使用这种方式浏览页面,例如:
You creating a navigation page that customized. So you can use this navigating your pages for example:
CustomNavigationPage.PushASync(new TestPage(Container.Resolve<WardListPage>())):如果使用此选项,则您的自定义导航页面将在每次调用时解决您的依赖性.
If use this your custom navigation page will be resolve your dependencies every call.
要提高性能,您可以使用以下方式注册您的依赖项: 单例模式.应用启动时,将注册依赖项. 使用此注册的依赖项之后.
To improve performance you can register your dependencies with singleton pattern. When the app started, dependencies will be registered. After you use this registered dependencies.
有一个改进:您可以使用单例模式定义一个静态定位器,它将静态依赖项注册到app.cs中.
There is an improvement : You define a static locator with singleton pattern it registers dependencies in app.cs
public sealed class Locator { private static Locator locator = null; private static readonly object padlock = new object(); Locator() { //your registries } public static Locator Locator { get { lock (padlock) { if (locator == null) { locator = new Locator(); } return locator; } } } }还有您的app.cs:
And your app.cs :
public App() { InitializeComponent(); Locator locator = new Locator(); Container = locator.Container; . . } public static IContainer Container;这样,您只需一次注册您的依赖项.没有重复的代码.将仅使用一个实例.
This way you only one time register your dependencies. There is no duplication of code. Only one instance will be used.
更多推荐
通过依赖注入进行多重导航控制
发布评论