我在iOS中遇到“选项卡式页面”的问题。我在选项卡式页面中添加了5个导航页面。显示并关闭模态后,我无法在水龙头上更改选项卡。如果我按住另一个标签3-4秒(长按),则该标签被选中。不知道是什么问题。没有日志以及
相同的代码在Android上运行正常。请让我知道是否有人遇到过类似的问题。
主页代码
private bool tabsConfigured = false;
public HomeView (HomeViewModel viewModel)
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
if (Device.RuntimePlatform == Device.Android)
{
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarItemColor(Color.FromHex("#c1c1c1"));
On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarSelectedItemColor(Color.White);
On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
On<Xamarin.Forms.PlatformConfiguration.Android>().SetOffscreenPageLimit(5);
}
else if (Device.RuntimePlatform == Device.iOS)
{
this.BarBackgroundColor = Color.FromHex("#0c293d");
//this.BarTextColor = Color.White;
}
this.ViewModel = viewModel;
this.ViewModel.SwitchTabAction = OnSwitchTabFromViewModel;
#if __IOS__
InitTabs();
#endif
}
protected override void OnAppearing()
{
base.OnAppearing();
#if __ANDROID__
InitTabs();
#endif
}
protected override void OnDisappearing()
{
base.OnDisappearing();
this.ViewModel.SwitchTabAction = null;
}
private void InitTabs()
{
var logService = AutofacUtil.Instance.Resolve<ILogService>();
try
{
if (tabsConfigured) return;
this.Children.Add(this.GetInitialTabPage<DestinationsViewModel>("tab_guide", "Guide"));
this.Children.Add(this.GetInitialTabPage<GroupsViewModel>("tab_chats", "Groups"));
this.Children.Add(this.GetInitialTabPage<ItineraryViewModel>("tab_itineraries", "Itineraries"));
this.Children.Add(this.GetInitialTabPage<ExpenseViewModel>("tab_expenses", "Expenses"));
this.Children.Add(this.GetInitialTabPage<MoreViewModel>("tab_mores", "More"));
#if __ANDROID__
this.BatchCommit();
#endif
tabsConfigured = true; //Important to be here.
//Setting Current Page
if (this.ViewModel.ActiveTab >= 1)
this.CurrentPage = this.Children[this.ViewModel.ActiveTab];
else if (this.ViewModel.ActiveTab == 0) //OnPageChanged doesn't get called for first tab.
{
CurrentPage = this.Children[this.ViewModel.ActiveTab];
OnPageChanged();
}
}
catch (Exception ex)
{
logService.Error(ex);
}
}
public NavigationPage GetInitialTabPage<T>(string icon, string title) where T : BaseCoreViewModel
{
Page page = new TabLoadingPage(typeof(T));
NavigationPage navigationPage = new NavigationPage(page);
navigationPage.Icon = icon;
navigationPage.Title = title;
navigationPage.BarBackgroundColor = Color.FromHex("#0c293d");
navigationPage.BarTextColor = Color.White;
return navigationPage;
}
显示模态的代码。调用PushModalAsync以显示模式。
public class NavigationService : INavigationService
{
private INavigation _mainNavigation = null;
private INavigation _currentTabNavigation = null;
private INavigation _currentModalNavigation = null;
private NavigationPage _currentModalNavPage = null;
public void SetMainXamarinFormsNavigation(INavigation navigation)
{
_mainNavigation = navigation;
}
public void SetTabXamarinFormsNavigation(INavigation navigation)
{
_currentTabNavigation = navigation;
}
public async Task PushAsync<T>(object parameters = null, bool animate = true) where T : BaseCoreViewModel
{
var page = ViewCreator.CreateViewAndViewModelAndCallInit<T>(parameters);
INavigation currentNavigation = null;
if (_currentModalNavigation != null)
currentNavigation = _currentModalNavigation;
else if (_currentTabNavigation != null)
currentNavigation = _currentTabNavigation;
else if (_mainNavigation != null)
currentNavigation = _mainNavigation;
#if __IOS__
var currentPage = currentNavigation.NavigationStack.ElementAt(currentNavigation.NavigationStack.Count - 1);
var oldTitle = currentPage.Title;
currentPage.Title = "Back";
#endif
await currentNavigation.PushAsync(page, animate);
#if __IOS__
currentPage.Title = oldTitle;
#endif
}
public async Task PushModalAsync<T>(object parameters = null, bool animate = true) where T : BaseCoreViewModel
{
if (_mainNavigation == null) return;
#if __ANDROID__
var emptyPage = new FirstEmptyNavPage();
#endif
var startPage = ViewCreator.CreateViewAndViewModelAndCallInit<T>(parameters, true);
#if __ANDROID__
_currentModalNavPage = new NavigationPage(emptyPage);
#else
_currentModalNavPage = new NavigationPage(startPage);
#endif
_currentModalNavPage.Popped += _currentModalNavPage_Popped;
_currentModalNavPage.BarBackgroundColor = Color.FromHex("#0c293d");
_currentModalNavPage.BarTextColor = Color.White;
#if __ANDROID__
await _currentModalNavPage.PushAsync(startPage);
#endif
await _mainNavigation.PushModalAsync(_currentModalNavPage, animate);
if (_currentModalNavPage != null)
_currentModalNavigation = _currentModalNavPage.Navigation;
}
private async void _currentModalNavPage_Popped(object sender, NavigationEventArgs e)
{
if (_currentModalNavPage != null && _currentModalNavPage.CurrentPage is FirstEmptyNavPage)
{
_currentModalNavPage.Popped -= _currentModalNavPage_Popped;
_currentModalNavPage = null;
_currentModalNavigation = null;
await _mainNavigation.PopModalAsync(false);
}
}
public async Task PopAsync(bool animate = true)
{
if (_currentModalNavigation != null && _currentModalNavigation.NavigationStack.Count > 1)
await _currentModalNavigation.PopAsync(animate);
else if (_currentModalNavigation != null && _currentModalNavigation.NavigationStack.Count > 1)
{
await _currentModalNavigation.PopModalAsync(animate);
_currentModalNavigation = null;
}
else if (_currentTabNavigation != null)
await _currentTabNavigation.PopAsync(animate);
else if (_mainNavigation != null)
await _mainNavigation.PopAsync(animate);
}
public async Task PopModalAsync(bool animate = true)
{
if (_currentModalNavigation != null)
{
await _currentModalNavigation.PopModalAsync(animate);
_currentModalNavigation = null;
}
}
}