I'm making a small app with WinUI 3. The app require to connect to the Internet while loading. Then it will download some necessary resources. I can't put the network check under any one window, because all windows need the resources which have downloaded, otherwise they won't start properly. So I added a network check in the Onlaunch method in App.xaml.cs. There are the codes.
if (SystemEnvironmentHelper.IsConnectInternet() == false)
{
//No wifi connection
ContentDialog noWifiDialog = new ContentDialog()
{
Title = "No wifi connection",
Content = "Check connection and try again.",
CloseButtonText = "Ok"
};
await noWifiDialog.ShowAsync();
}
And the IsConnectInternet
method is defined as follows.(Actually it works well)
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(ref int Description, int ReservedValue);
public static bool IsConnectInternet()
{
int Description = 0;
return InternetGetConnectedState(ref Description, 0);
}
But when I run this app, it throw an exception System.ArgumentException
. I know I should add XamlRoot, but no form is declared in App.xaml.cs, and XamlRoot does not exist. I don't know how to solve this problem because there are only a few documents and they didn't mention this problem.
Also, I'm using an unpackaged app, does Content Dialog work with unpackaged apps?
Any help are appreciated.
You should create and display a window as soon as possible after the app has been started regardless of whether you intend to display a
ContentDialog
. It doesn't have to be your "main" window but you should display something to confirm that your app is actually running.You cannot show a
ContentDialog
until there are some content displayed on the screen.You could for example create a temporary window that shows a
ProgressRing
while you check the connection:Once the temporary window has been shown, you can then check the connection and display the dialog: