C
C#14mo ago
Ikarmus

✅ Async app initialization fails

Hi, I tried to upgrade my app initialization steps by making them async, but I am stuck with errors I don't truly understand.
1 Reply
Ikarmus
IkarmusOP14mo ago
public partial class App : Application
{
internal static bool IsInitialized = false;

public App()
{
CollectionView_HeaderFooterFix();
InitializeComponent();

MainPage = new MainNavigationPage();
}

internal static async Task Initialization()
{
#if DEBUG
Preferences.Clear();
#endif
await CheckFirstRun();
FileManager.ReloadMaps();
IsInitialized = true;
}
...
public partial class App : Application
{
internal static bool IsInitialized = false;

public App()
{
CollectionView_HeaderFooterFix();
InitializeComponent();

MainPage = new MainNavigationPage();
}

internal static async Task Initialization()
{
#if DEBUG
Preferences.Clear();
#endif
await CheckFirstRun();
FileManager.ReloadMaps();
IsInitialized = true;
}
...
Initialization() is called from MainNavigationPage Appearing event, because that's first place I can run async code
public partial class MainNavigationPage : NavigationPage
{
public MapDisplayPage mapDisplay;
private Page newPage;

public MainNavigationPage()
{
mapDisplay = new();
InitializeComponent();
Appearing += MainNavigationPage_Appearing;
newPage = new MapPickerPage();
}

private async void MainNavigationPage_Appearing(object sender, EventArgs e)
{
if (!App.IsInitialized)
await App.Initialization();

await mapDisplay.UpdateDisplay();
await Navigation.PushAsync(newPage);
}
}
public partial class MainNavigationPage : NavigationPage
{
public MapDisplayPage mapDisplay;
private Page newPage;

public MainNavigationPage()
{
mapDisplay = new();
InitializeComponent();
Appearing += MainNavigationPage_Appearing;
newPage = new MapPickerPage();
}

private async void MainNavigationPage_Appearing(object sender, EventArgs e)
{
if (!App.IsInitialized)
await App.Initialization();

await mapDisplay.UpdateDisplay();
await Navigation.PushAsync(newPage);
}
}
I observed code failing somewhere about CopyToAsync
internal static async Task<bool> SaveFileAsync(Stream content, string filename, bool isUserChoosingLocation = false, string initialPath = null)
{
initialPath ??= FileSystem.AppDataDirectory;
if (!filename.StartsWith('\\'))
filename = '\\' + filename;

if (!isUserChoosingLocation)
{
var opts = new FileStreamOptions()
{
Mode = FileMode.CreateNew,
Access = FileAccess.Write
};
var x = new StreamWriter(Path.Combine(initialPath + filename), opts);

await content.CopyToAsync(x.BaseStream);
x.Close();
return true;
}

#pragma warning disable CA1416 // Validate platform compatibility (Android >= 26)
var saveResult = await fileSaver.SaveAsync(FileSystem.AppDataDirectory, filename, content, default);
return saveResult.IsSuccessful;
#pragma warning restore CA1416 // Validate platform compatibility
}
internal static async Task<bool> SaveFileAsync(Stream content, string filename, bool isUserChoosingLocation = false, string initialPath = null)
{
initialPath ??= FileSystem.AppDataDirectory;
if (!filename.StartsWith('\\'))
filename = '\\' + filename;

if (!isUserChoosingLocation)
{
var opts = new FileStreamOptions()
{
Mode = FileMode.CreateNew,
Access = FileAccess.Write
};
var x = new StreamWriter(Path.Combine(initialPath + filename), opts);

await content.CopyToAsync(x.BaseStream);
x.Close();
return true;
}

#pragma warning disable CA1416 // Validate platform compatibility (Android >= 26)
var saveResult = await fileSaver.SaveAsync(FileSystem.AppDataDirectory, filename, content, default);
return saveResult.IsSuccessful;
#pragma warning restore CA1416 // Validate platform compatibility
}
I tried to execute Task.Run( async () => await Initialization()); in App constructor Tried to Wait() and GetAwaiter().GetResult(); Result property as well I need to execute this method before my App.MainPage gets assigned (also can't do it outside ctor) I'm kinda stuck because I don't want to revert my changes to previous commit better is the enemy of good Ok actually some other operation were blocking the UI
Want results from more Discord servers?
Add your server