C
C#8mo 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
Ikarmus8mo 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
More Posts
❔ save checkbox value after refreshIs there a way in Blazor where I save the checkbox after I refresh the page, for example I press a c❔ Using Microsoft Graph to verify users exist based on IEnumerable<Guid>I am trying to work with Microsoft Graph to verify that each guid in some list is associated with a ✅ How to register a onNavigate/navigation event callback in Net8 SSR?blazor.web.js replaced the entire html on navigate with the response, but scripts like `<script src=❔ Establishing a connection to SQL Server in Mac (Azure data studio & Docker desktop)Ive been stuck on this error: A network-related or instance-specific error occurred while establish✅ Reading matricies to an output fileI am trying to read the data inside two matricies from an input file firstly in order to add them to❔ How to Deploy WASM Project with .NET Backend in same repo?I have a Blazor frontend with a .NET backend. How do I use Azure to deploy these when they are in th❔ EF Core LINQ translation documentIn the process of upgrading a .NET Core 1.1 to 6 app, our EF Core LINQ queries are now hitting some Is there a better (or more appropriate way) of implementing method overloading for my usecase here?I have two methods of a class I'm writing - in one usecase I want to update the field with no return❔ Referencing new projects with the main projectLet's say I have a main project `CoreProject` and I want to add 2 new projects: a `LoggerService` an❔ Refactoring strategy pattern to a simpler codeI have the following code and I want te refactor it to a less-boiler-plate code. Wha'ts suposed to