[英]Xamarin Forms Tabbed Page Tabs Initializing
I am developing a tabbed Page in Xamarin that consist of 3 tabs ( Tab1= "Home", Tab2= "Chat",Tab3="Menu" )我正在 Xamarin 中开发一个标签页,其中包含 3 个标签( Tab1= "Home", Tab2= "Chat",Tab3="Menu" )
When the App starts ( MainPage = new TabbedPage()
) it initializes ( InitializeComponent();
) all the tabs together at first.当应用程序启动( MainPage = new TabbedPage()
)时,它首先将所有选项卡一起初始化( InitializeComponent();
)。
My question is that if there is a way to initialize each component alone whenever the tab is pressed?我的问题是,是否有办法在按下选项卡时单独初始化每个组件?
In my experience, what takes most of the time isn't loading the XAML, its a combination of "measure and layout" , then "loading data" (eg the items of a list).根据我的经验,大部分时间不是加载 XAML,而是“测量和布局”的组合,然后是“加载数据” (例如列表中的项目)。
This example shows how to defer "measure and layout".这个例子展示了如何推迟“测量和布局”。 It is as simple as setting IsVisible="False"
on any XAML you want to defer.就像在您想要推迟的任何 XAML 上设置IsVisible="False"
一样简单。 Then when it is needed, set IsVisible="True"
.然后在需要时设置IsVisible="True"
。 I show the simplest case, where there is no data binding.我展示了最简单的情况,即没有数据绑定。
Deferring data loading depends on your data.延迟数据加载取决于您的数据。 The simplest technique is to start all lists ( ListView
, CollectionView
) with ZERO ELEMENTS.最简单的技术是从零元素开始所有列表( ListView
, CollectionView
)。 When the data is needed, add the elements.当需要数据时,添加元素。 I do not show that below.我没有在下面显示。 See How to load data when a tab is clicked .请参阅单击选项卡时如何加载数据。
NOTE: ON Android , InitializeComponent
can be sped up further if you have Visual Studio 2019 Enterprise .注意:在 Android上,如果您有Visual Studio 2019 Enterprise ,则可以进一步加快InitializeComponent
的速度。 In your MyProjectName.Android
project, in Project Properties / Android Options (for Configuration "Release"
) / Code Generation and Runtime, check "AOT Compilation" .在您的MyProjectName.Android
项目中,在 Project Properties / Android Options (for Configuration "Release"
) / Code Generation and Runtime 中,检查"AOT Compilation" 。 and "Enable Startup Tracing".和“启用启动跟踪”。 You may want to also check "Use LLVM Optimiziing Compiler".您可能还需要检查“使用 LLVM 优化编译器”。 These options may increase app bundle size.这些选项可能会增加应用程序包的大小。 Test load time on an actual device - emulator may give misleading results as to what combination loads fastest.在实际设备上测试加载时间- 模拟器可能会给出关于哪种组合加载速度最快的误导性结果。
TabbedPage1.xaml: TabbedPage1.xaml:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TabbedPage1.Views"
x:Class="TabbedPage1.TabbedPage1">
<local:StartPage Title="Start" />
<local:SlowPage Title="Slow Load" />
</TabbedPage>
TabbedPage1.xaml.cs: TabbedPage1.xaml.cs:
using Xamarin.Forms;
namespace TabbedPage1
{
public partial class TabbedPage1 : TabbedPage
{
public TabbedPage1()
{
InitializeComponent();
}
}
}
SlowPage.xaml: SlowPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TabbedPage1.Views.SlowPage">
<ContentPage.Content>
<StackLayout x:Name="MyContents" IsVisible="False">
<Label Text="This text should appear after a 3 second delay."
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
SlowPage.xaml.cs: SlowPage.xaml.cs:
using System.Threading.Tasks;
using Xamarin.Forms;
namespace TabbedPage1.Views
{
public partial class SlowPage : ContentPage
{
public SlowPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
if (!MyContents.IsVisible) {
// Simulate a page that is slow to layout or load.
await Task.Delay(3000);
MyContents.IsVisible = true;
}
}
}
}
StartPage.xaml:起始页.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TabbedPage1.Views.StartPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome!" FontSize="32"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
StartPage.xaml.cs:起始页.xaml.cs:
using Xamarin.Forms;
namespace TabbedPage1.Views
{
public partial class StartPage : ContentPage
{
public StartPage()
{
InitializeComponent();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.