需要修改导航方法,以实现比一个导航更多的导航

时间:2019-04-10 04:07:00

标签: c# xamarin xamarin.forms

我在基本抽象类中的导航方法

public abstract void Navigate(SelectedItemChangedEventArgs e); '

在我的一个View类中,单击或轻击某个按钮时,我需要进行多个导航。仅使用导航功能,我可以用于一种导航,我需要改进导航方法以使其得到改进,以便可以用于多种导航。我在这里有点笨,我应该怎么做一个中继命令或使用通用命令。为了将此方法用于多个导航目的,我需要更改

 private void JournalSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e?.SelectedItem == null) return;
        JournalGroupList.SelectedItem = null;
        Navigate(e);
    }

    public override async void Navigate(SelectedItemChangedEventArgs e)
    {
        JournalHistoryViewPage journalHistoryViewPage = App.Container.Resolve<JournalHistoryViewPage>();
        journalHistoryViewPage.BaseViewModel.JournalGroup = e.SelectedItem as JournalGroup;
        journalHistoryViewPage.BaseViewModel.SelectedPatient = BaseViewModel.SelectedPatient;
        await Navigation.PushAsync(journalHistoryViewPage, true);
    }

  private async void VitalSigns_Tapped(object sender, System.EventArgs e)
    {
        var vitalSignsViewPage = App.Container.Resolve<VitalSignsViewPage>();
        vitalSignsViewPage.BaseViewModel.SelectedPatient = BaseViewModel.SelectedPatient;
        await Navigation.PushAsync(vitalSignsViewPage, true);
    }

当我在journalselected中使用“导航功能”时,我也需要将其用于VitalSigns_tapped。在这里,我觉得我已经重复了代码,并且没有正确使用方法

1 个答案:

答案 0 :(得分:1)

将页面传递到Navigate function将使导航功能更加通用。下次您要浏览新页面时,只需将新页面传递给它即可。

    private void JournalSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e?.SelectedItem == null) return;
        JournalGroupList.SelectedItem = null;

        JournalHistoryViewPage journalHistoryViewPage = App.Container.Resolve<JournalHistoryViewPage>();
        journalHistoryViewPage.BaseViewModel.JournalGroup = e.SelectedItem as JournalGroup;
        journalHistoryViewPage.BaseViewModel.SelectedPatient = BaseViewModel.SelectedPatient;

        Navigate(journalHistoryViewPage);
    }

    private async void VitalSigns_Tapped(object sender, System.EventArgs e)
    {
        var vitalSignsViewPage = App.Container.Resolve<VitalSignsViewPage>();
        vitalSignsViewPage.BaseViewModel.SelectedPatient = BaseViewModel.SelectedPatient;

        Navigate(vitalSignsViewPage);
    }

    public override async void Navigate(Page  page)
    {        
        await Navigation.PushAsync(page, true);
    }

如果每个页面都有一个BaseViewModel.SelectedPatient并具有相同值,则您也可以将其合并到导航功能中,例如:

public override async void Navigate(Page  page)
    {        
        page.BaseViewModel.SelectedPatient = BaseViewModel.SelectedPatient;
        await Navigation.PushAsync(page, true);
    }