как реализовать сервис навигации и внедрение зависимостей на главной странице формы xamarin - PullRequest
0 голосов
/ 25 апреля 2019

Я создавал приложение xamarin с главной страницей сведений, используя MVVM.Позже я понял, что у моего приложения есть некоторые ограничения, например, переход от одной модели представления к другой на основе бизнес-логики. Поэтому я следовал документам Microsoft по корпоративному шаблону (https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/navigation).But, к сожалению, это навигация с вкладками. Я много искал вИнтернет, кто-то использует платформу призмы, но я не могу выбрать.

Я уже добавил контейнер Tiny Ioc и изменил свой сервис в качестве интерфейсов для внедрения зависимостей.

мой код

public partial class MainPage : MasterDetailPage
    {
        public List<MasterPageItem> MenuList
        {
            get;
            set;
        }

        public MainPage()
        {
            NavigationPage.SetHasNavigationBar(this, false);
            InitializeComponent();
            BindingContext = new ProfileViewModel();
            MenuList = new List<MasterPageItem>();


                // Adding menu items to menuList and you can define title ,page and icon  

                    MenuList.Add(new MasterPageItem()
                    {
                        Title = "Home",
                        Icon = FontAwesomeFonts.Home,
                        TargetType = typeof(HomePage)
                    });

                MenuList.Add(new MasterPageItem()
                    {
                        Title = "My Profile",
                        Icon = FontAwesomeFonts.User,
                        TargetType = typeof(ProfilePage)
                    });
    private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
        {

                Type page = item.TargetType;
                Detail = new NavigationPage((Page)Activator.CreateInstance(page));
                IsPresented = false;

        }
           }

шаблон предприятия

public partial class MainView : TabbedPage
    {
        public MainView()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            MessagingCenter.Subscribe<MainViewModel, int>(this, MessageKeys.ChangeTab, (sender, arg) =>
            {
               switch(arg)
                {
                    case 0:
                        CurrentPage = HomeView;
                        break;
                    case 1:
                        CurrentPage = ProfileView;
                        break;
                    case 2:
                        CurrentPage = BasketView;
                        break;
                    case 3:
                        CurrentPage = CampaignView;
                        break;
                }
            });

            await ((CatalogViewModel)HomeView.BindingContext).InitializeAsync(null);
            await ((BasketViewModel)BasketView.BindingContext).InitializeAsync(null);
            await ((ProfileViewModel)ProfileView.BindingContext).InitializeAsync(null);
            await ((CampaignViewModel)CampaignView.BindingContext).InitializeAsync(null);
        }

        protected override async void OnCurrentPageChanged()
        {
            base.OnCurrentPageChanged();

            if (CurrentPage is BasketView)
            {
                // Force basket view refresh every time we access it
                await (BasketView.BindingContext as ViewModelBase).InitializeAsync(null);
            }
            else if (CurrentPage is CampaignView)
            {
                // Force campaign view refresh every time we access it
                await (CampaignView.BindingContext as ViewModelBase).InitializeAsync(null);
            }
            else if (CurrentPage is ProfileView)
            {
                // Force profile view refresh every time we access it
                await (ProfileView.BindingContext as ViewModelBase).InitializeAsync(null);
            }
        }
    }
...