MvxTabBarViewController 作为第一个 ViewController 没有出现
MvxTabBarViewController as first ViewController doesn't appear
在我的 Xamarin.iOS 应用程序(使用 MvvmCross)中,我注册了一个自定义 AppStart,它根据用户是否已经登录来启动登录屏幕或主屏幕。我正在使用 EntityFrameworkCore在启动时存储用户数据和从数据库加载信息工作正常,问题出现在从 AppStart 调用 await NavigationService.Navigate<MainViewModel()
之后。
我在调试器中收到 MvvmCross 导航 (iOSNavigation
) 的消息,紧随其后的是 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction
,据我在网上的研究可以看出,这是正常的。但是,该视图从未出现,应用程序仍然停留在 launch/splash 屏幕上。
我的MainViewController(对应MainViewModel)继承自MvxTabBarViewController,具有如下展示属性:[MvxRootPresentation(AnimationOptions = UIViewAnimationOptions.TransitionCrossDissolve | UIViewAnimationOptions.CurveEaseInOut, WrapInNavigationController = true)]
.
MainViewController 的唯一构造函数是:
public MainViewController()
: base()
{
// No call to ViewDidLoad here as base() seems to do it for me.
}
我的 Xamarin.Android 项目一切正常,所以我猜它在 iOS 方面。
MvvmCross 6.3.1.
编辑
在MainViewController的ViewDidLoad里面创建要显示的tabs:
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (ViewModel == null)
return;
// There are 3 ViewControllers, all created this way.
var viewControllerOne = new ViewControllerOne
{
ViewModel = ViewModel.ViewModelOne,
TabBarItem = new UITabBarItem(ViewModel.ViewModelOne.Title, UIImage.FromBundle("Icon1"), 0)
};
ViewControllers = new UIViewController[]
{
viewControllerOne,
viewControllerTwo,
viewControllerThree
};
}
每个选项卡都继承自 MvxViewController 并具有 [MvxTabPresentation]
presentation 属性。每个选项卡的构造函数是:
public ViewControllerOne() // One, Two, Three
: base("ViewOne", null) // One, Two, Three
{
// None of the tab views currently have any bindings to ViewModels,
just a UILabel constrained to the centre of the view for testing purposes.
}
我尝试了 运行 主线程上的初始导航逻辑,没有任何区别。这就是我在里面做的 MvxAppStart.NavigateToFirstViewModel:
await Mvx.IoCProvider.Resolve<IMvxMainThreadAsyncDispatcher>().ExecuteOnMainThreadAsync(() =>
{
if (isLoggedIn)
NavigationService.Navigate<MainViewModel>().GetAwaiter().GetResult();
else
NavigationService.Navigate<LoginViewModel>().GetAwaiter().GetResult();
});
我发现您设置标签的方式存在一些问题。
首先,在您的 ViewDidLoad 中,以下行可能有问题:
if (ViewModel == null)
return;
你不应该这样做。如果代码执行命中 return;
,方法中的其余代码将永远不会执行。
在此处查看示例代码:https://github.com/pnavk/Xamarin.iOS.MvvmCross.Tabs
希望对您有所帮助。
问题是我的 ViewModel 逻辑阻塞了主线程。
在我的 MainViewModel
中,我覆盖了 Initialize
任务以从 API 异步加载数据以显示各个选项卡。
最初,当我应该使用 MvxViewModel
方法 InvokeOnMainThread
和 InvokeOnMainThreadAsync
时,我在主线程上使用 IMvxMainThreadAsyncDispatcher
到 运行 东西.现在我正在使用这些应用程序启动没有问题。
感谢所有试图提供帮助的人,特别感谢 Cheesebaron 为我指明了有关阻塞主线程的正确方向。
在我的 Xamarin.iOS 应用程序(使用 MvvmCross)中,我注册了一个自定义 AppStart,它根据用户是否已经登录来启动登录屏幕或主屏幕。我正在使用 EntityFrameworkCore在启动时存储用户数据和从数据库加载信息工作正常,问题出现在从 AppStart 调用 await NavigationService.Navigate<MainViewModel()
之后。
我在调试器中收到 MvvmCross 导航 (iOSNavigation
) 的消息,紧随其后的是 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction
,据我在网上的研究可以看出,这是正常的。但是,该视图从未出现,应用程序仍然停留在 launch/splash 屏幕上。
我的MainViewController(对应MainViewModel)继承自MvxTabBarViewController,具有如下展示属性:[MvxRootPresentation(AnimationOptions = UIViewAnimationOptions.TransitionCrossDissolve | UIViewAnimationOptions.CurveEaseInOut, WrapInNavigationController = true)]
.
MainViewController 的唯一构造函数是:
public MainViewController()
: base()
{
// No call to ViewDidLoad here as base() seems to do it for me.
}
我的 Xamarin.Android 项目一切正常,所以我猜它在 iOS 方面。
MvvmCross 6.3.1.
编辑
在MainViewController的ViewDidLoad里面创建要显示的tabs:
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (ViewModel == null)
return;
// There are 3 ViewControllers, all created this way.
var viewControllerOne = new ViewControllerOne
{
ViewModel = ViewModel.ViewModelOne,
TabBarItem = new UITabBarItem(ViewModel.ViewModelOne.Title, UIImage.FromBundle("Icon1"), 0)
};
ViewControllers = new UIViewController[]
{
viewControllerOne,
viewControllerTwo,
viewControllerThree
};
}
每个选项卡都继承自 MvxViewController 并具有 [MvxTabPresentation]
presentation 属性。每个选项卡的构造函数是:
public ViewControllerOne() // One, Two, Three
: base("ViewOne", null) // One, Two, Three
{
// None of the tab views currently have any bindings to ViewModels,
just a UILabel constrained to the centre of the view for testing purposes.
}
我尝试了 运行 主线程上的初始导航逻辑,没有任何区别。这就是我在里面做的 MvxAppStart.NavigateToFirstViewModel:
await Mvx.IoCProvider.Resolve<IMvxMainThreadAsyncDispatcher>().ExecuteOnMainThreadAsync(() =>
{
if (isLoggedIn)
NavigationService.Navigate<MainViewModel>().GetAwaiter().GetResult();
else
NavigationService.Navigate<LoginViewModel>().GetAwaiter().GetResult();
});
我发现您设置标签的方式存在一些问题。
首先,在您的 ViewDidLoad 中,以下行可能有问题:
if (ViewModel == null)
return;
你不应该这样做。如果代码执行命中 return;
,方法中的其余代码将永远不会执行。
在此处查看示例代码:https://github.com/pnavk/Xamarin.iOS.MvvmCross.Tabs
希望对您有所帮助。
问题是我的 ViewModel 逻辑阻塞了主线程。
在我的 MainViewModel
中,我覆盖了 Initialize
任务以从 API 异步加载数据以显示各个选项卡。
最初,当我应该使用 MvxViewModel
方法 InvokeOnMainThread
和 InvokeOnMainThreadAsync
时,我在主线程上使用 IMvxMainThreadAsyncDispatcher
到 运行 东西.现在我正在使用这些应用程序启动没有问题。
感谢所有试图提供帮助的人,特别感谢 Cheesebaron 为我指明了有关阻塞主线程的正确方向。