Xamarin.forms 如何在App.xml中实现ActivityIndicator

Xamarin.forms 如何在App.xml中实现ActivityIndicator,xamarin.forms,loader,activity-indicator,Xamarin.forms,Loader,Activity Indicator,在MainPage中,我实现了应用程序的登录功能。登录后,它将重定向到仪表板。如果用户已登录应用程序,则用户将直接重定向到仪表板 在MainPage.XML上,我实现了ActivityIndicator。这将向首次登录应用程序的用户显示 现在我需要在App.xml页面中实现ActivityIndicator。因为如果用户已经登录,主页将不会显示给用户。它将直接重定向到仪表板。在重定向到仪表板之前,我需要显示ActivityIndicator 如何在App.XML页面中实现活动指示器 我在App.

在MainPage中,我实现了应用程序的登录功能。登录后,它将重定向到仪表板。如果用户已登录应用程序,则用户将直接重定向到仪表板

在MainPage.XML上,我实现了ActivityIndicator。这将向首次登录应用程序的用户显示

现在我需要在App.xml页面中实现ActivityIndicator。因为如果用户已经登录,主页将不会显示给用户。它将直接重定向到仪表板。在重定向到仪表板之前,我需要显示ActivityIndicator

如何在App.XML页面中实现活动指示器

我在App.XML中尝试的内容:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="VSS.App">
    <Application.Resources>
        <ActivityIndicator x:Name="actIndicator2"
                    WidthRequest="50" HeightRequest="50" Color="Black"
                    AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" 
                    HorizontalOptions="Center" VerticalOptions="Center"/>
    </Application.Resources>
</Application>
public partial class App : Xamarin.Forms.Application
    {
        public App()
        {
            Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
            //MainPage = new VendorTabbedPage();
        }
        protected override void OnStart()
        {
            string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "***SQLite.db3");
            SQLiteConnection db = new SQLiteConnection(dbPath);
            db.CreateTable<LoginSqlLiteM>();
            var data = db.Table<LoginSqlLiteM>();
            Xamarin.Forms.Application.Current.Properties["isAddContact"] = "";

            if (data.Count() != 0)
            {
                actIndicator2.IsVisible = true;
                actIndicator2.IsRunning = true;

                MainPage = new NavigationPage(new MainMasterDetailPage())
                {
                    BarBackgroundColor = Color.Transparent,
                };
            }
            else
            {
                MainPage = new NavigationPage(new MainPage())
                {
                    BarBackgroundColor = Color.Transparent,
                }; 
            }
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

App.cs:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="VSS.App">
    <Application.Resources>
        <ActivityIndicator x:Name="actIndicator2"
                    WidthRequest="50" HeightRequest="50" Color="Black"
                    AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" 
                    HorizontalOptions="Center" VerticalOptions="Center"/>
    </Application.Resources>
</Application>
public partial class App : Xamarin.Forms.Application
    {
        public App()
        {
            Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
            //MainPage = new VendorTabbedPage();
        }
        protected override void OnStart()
        {
            string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "***SQLite.db3");
            SQLiteConnection db = new SQLiteConnection(dbPath);
            db.CreateTable<LoginSqlLiteM>();
            var data = db.Table<LoginSqlLiteM>();
            Xamarin.Forms.Application.Current.Properties["isAddContact"] = "";

            if (data.Count() != 0)
            {
                actIndicator2.IsVisible = true;
                actIndicator2.IsRunning = true;

                MainPage = new NavigationPage(new MainMasterDetailPage())
                {
                    BarBackgroundColor = Color.Transparent,
                };
            }
            else
            {
                MainPage = new NavigationPage(new MainPage())
                {
                    BarBackgroundColor = Color.Transparent,
                }; 
            }
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
public分部类App:Xamarin.Forms.Application
{
公共应用程序()
{
Xamarin.Forms.Application.Current.On().UseWindowsOfInputModeAdjust(WindowsOfInputModeAdjust.Resize);
初始化组件();
主页=新导航页(新主页());
//MainPage=新供应商AbbedPage();
}
受保护的覆盖void OnStart()
{
字符串dbPath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),“***SQLite.db3”);
SQLiteConnection db=新的SQLiteConnection(dbPath);
db.CreateTable();
var data=db.Table();
Xamarin.Forms.Application.Current.Properties[“isAddContact”]=”;
如果(data.Count()!=0)
{
actIndicator2.IsVisible=true;
actIndicator2.IsRunning=true;
MainPage=新导航页(新MainMasterDetailPage())
{
BarBackgroundColor=颜色。透明,
};
}
其他的
{
主页=新导航页(新主页())
{
BarBackgroundColor=颜色。透明,
}; 
}
}
受保护的覆盖void OnSleep()
{
//在应用程序睡眠时处理
}
受保护的覆盖void OnResume()
{
//在应用程序恢复时处理
}
}

帮助我。

首先,App.xaml不是显示在用户界面上的页面,因此无法保存用户界面元素。一个更好的方法是添加一个像你的启动屏幕那样的屏幕,它可以容纳这个指示器,并且实际上也可以让你的导航看起来更平滑!仅供参考,只有在您等待异步调用时,它才会显示您应该在加载内容时在MainMasterDetailPage中显示活动指示器。我不认为推迟导航有什么意义。如果您需要通知用户他正在被重定向到内容,我宁愿使用toast,它将在导航过程中留在屏幕上,并且不会附加到当前视图。@FreakyAli,如何实现启动屏幕?它基本上是一个屏幕,可以执行所有计算和DB调用,并在所需屏幕上获取适当的数据,同时也可以在该屏幕上显示活动指示器!您可以使用
IsVisible
此属性控制
ActivityIndicator
的显示或消失。