gpt4 book ai didi

c# - 是否可以在启动时显示 DisplayAlert (Xamarin.Forms)

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:43 24 4
gpt4 key购买 nike

在我的移动应用程序(xamarin 表单)中,我从互联网获取数据,因此它需要互联网连接。因为我有一本在 App.xaml.cs 中初始化的字典,并且我使用来自互联网的数据,所以我需要检查互联网连接。我看过this问题 OP 在哪里要求类似的东西,但答案对我不起作用,因为我需要在应用程序启动时检查互联网连接,而不是在启动 MainPage 之后。例如,部落冲突。每当应用程序启动时,应用程序都会检查互联网连接,如果没有连接,它会重复向用户显示警报,直到有连接。

enter image description here

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Multi
{
public partial class App : Application
{
static GroupStage groupstage = new GroupStage();

public static HtmlWeb web = new HtmlWeb();
public static HtmlDocument doc = LoadUrlAndTestConnection();

//The reason why I have put a method is because I wanted to try if I can use try-catch to display alert, however this didn't work.
public static HtmlDocument LoadUrlAndTestConnection()
{
bool con = true;
while (con)
{
try
{
doc = web.Load(someURL);
}
catch (Exception ex)
{
var sth = new ErrorPage();
sth.InternetErrorDisplay();
con = true;
continue;
}
con = false;
}
return docSK;
}

public static Dictionary<string, Country> _countries = new Dictionary<string, Country>
{
["Australia"] = new Country(1, "Australia", false, "AU", "ausFlag.png", 3, groupstage, GetScore("Australia", 3)),

public static string[] GetScore(string name, int GroupID)
{
//Gets the score data from internet
}

public App()
{
InitializeComponent();

TwitchClass.MainAsync().Wait();

MainPage = new OpeningPage();
}

protected override void OnStart()
{

}

protected override void OnSleep()
{

}

protected override void OnResume()
{

}
}
}

//GetScore method requires internet connection as it gets the score data from internet.

InternetErrorDisplay 方法是,

public void InternetErrorDisplay() => DisplayAlert("Connection Error", "Could not detect internet connection. This application requires access to internet.", "Retry");

是否可以在 xamarin 表单应用程序中实现此行为?我怎样才能实现它?

最佳答案

是的,为什么不可能呢?

这是一个使用 async/await 的例子

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Threading.Tasks;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace LoadingSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();

//MainPage = new MainPage();
}

protected override async void OnStart()
{
// shows Loading...
MainPage = new LoadPage();

await Task.Yield();
// Handle when your app starts

// Just a simulation with 10 tries to get the data
for (int i = 0; i < 10; i++)
{
await Task.Delay(500);
// await internet_service.InitializeAsync();
await MainPage.DisplayAlert(
"Connection Error",
"Unable to connect with the server. Check your internet connection and try again",
"Try again");
}
await Task.Delay(2000);
// after loading is complete show the real page
MainPage = new MainPage();
}

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

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

关于c# - 是否可以在启动时显示 DisplayAlert (Xamarin.Forms),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52341817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com