xamarin.forms - 是否可以延迟加载不需要的 PRISM / Xamarin Forms 组件?
问题描述
我有以下 AppDelegate 需要相当长的时间来加载:
Syncfusion.ListView.XForms.iOS.SfListViewRenderer.Init();
new Syncfusion.SfNumericUpDown.XForms.iOS.SfNumericUpDownRenderer();
Syncfusion.SfCarousel.XForms.iOS.SfCarouselRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfSegmentedControlRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfCheckBoxRenderer.Init();
new Syncfusion.XForms.iOS.ComboBox.SfComboBoxRenderer();
//Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init();
new Syncfusion.SfRotator.XForms.iOS.SfRotatorRenderer();
new Syncfusion.SfRating.XForms.iOS.SfRatingRenderer();
new Syncfusion.SfBusyIndicator.XForms.iOS.SfBusyIndicatorRenderer();
当我知道主屏幕不需要这些组件但子屏幕需要这些组件时,我应该考虑哪些选项?
我正在使用 PRISM,似乎每个选项卡都在允许显示或与最终用户交互之前立即预加载。我可以做些什么来延迟 Prism TabView 在显示界面之前进行的预渲染?
我应该使用
Lazy<T>
吗?什么是正确的方法?我应该将这些组件移动到另一个初始化部分吗?
解决方案
您可以通过多种方式最终实现这一目标,这完全取决于您的真正目标是什么。
如果您的目标是确保您尽可能快地进入 Xamarin.Forms 页面,以便您拥有某种活动指示器,这实质上是对用户说,“没关系,我没有冻结,我们只是做一些事情为你做好准备”,那么你可以尝试创建一个“SpashScreen”页面,你可以在其中进行额外的加载。设置可能如下所示:
public partial class AppDelegate : FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(new iOSInitializer()));
return base.FinishedLaunching(app, options);
}
}
}
public class iOSInitializer : IPlatformInitializer, IPlatformFinalizer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance<IPlatformFinalizer>(this);
}
public void Finalize()
{
new Syncfusion.SfNumericUpDown.XForms.iOS.SfNumericUpDownRenderer();
Syncfusion.SfCarousel.XForms.iOS.SfCarouselRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfSegmentedControlRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfCheckBoxRenderer.Init();
}
}
public class App : PrismApplication
{
protected override async void OnInitialized()
{
await NavigationService.NavigateAsync("SplashScreen");
}
}
public class SplashScreenViewModel : INavigationAware
{
private IPlatformFinalizer _platformFinalizer { get; }
private INavigationService _navigationService { get; }
public SplashScreenViewModel(INavigationService navigationService, IPlatformFinalizer platformFinalizer)
{
_navigationService = navigationService;
_platformFinalizer = platformFinalizer;
}
public async void OnNavigatedTo(INavigationParameters navigationParameters)
{
_platformFinalizer.Finalize();
await _navigationService.NavigateAsync("/MainPage");
}
}
如果您正在使用模块,则可以采取类似的方法,尽管在启动时初始化的任何模块仍然会在您设置要导航到的页面之前调用 Init 渲染器。也就是说,使用模块确实给您带来了许多好处,因为您只需要初始化应用程序此时实际需要的东西。
所有这一切都表明,如果您看到很多收益,我会感到惊讶,因为这些Init
调用通常是空方法,仅旨在防止链接器将它们链接出来......如果您没有链接或有链接器文件可以简单地指示链接器不理会您的 Syncfusion 和其他库。
推荐阅读
- sql-server - DATEADD(DAY, 1, GETDATE()) 和 DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE())) 有什么不同
- sql-server - 将一个 temptable 与另一个具有不同列的表连接起来,并且在 mysql 服务器中两个表中的列数不同
- javascript - mongodb中用户之间的自定义搜索
- c++ - 如何从 C++ 中的字符串中删除纯“\n”?
- java - 无法使用 HTTPS 连接到在服务器上运行的 Spring 项目(SSL 错误)
- android - 无法在 Iphone 上访问我们的 Angular 网站?
- swift - Firebase UpdateChildValues 未设置任何值
- angular - Jasmine:匹配器与未定义和 null 不同(!= undefined 和!= null)
- powershell-2.0 - powershell ping 连续特定持续时间并保存日志
- c# - 无法访问输入文本框值到asp.net c#后面的代码