C
C#16mo ago
Mekasu0124

✅ Showing Splash Screen with logic functionality

using System;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Diary.ViewModels;
using Diary.Views;
using System.Threading.Tasks;

namespace Diary;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override async void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var splashScreenVM = new SplashScreenViewModel();
var splashScreen = new SplashScreen
{
DataContext = splashScreenVM
};
desktop.MainWindow = splashScreen;
splashScreen.Show();

try
{
splashScreenVM.StartupMessage = "Checking If config.json File Exists...";
await Task.Delay(1000, splashScreenVM.CancellationToken);

bool jsonExists = CheckForJsonFile();

if (jsonExists)
{
splashScreenVM.StartupMessage =
"config.json File Found. Loading Data From Json File...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

splashScreenVM.StartupMessage =
"Data Loaded From Json File... Launching Home Screen... Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

desktop.MainWindow = HomeScreen;
}
else
{
splashScreenVM.StartupMessage = "config.json File Not Found. Creating New Json File...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

bool jsonCreated = CreateJsonFile();

if (jsonCreated)
{
splashScreenVM.StartupMessage = "Json File Created. Launching Setup Screen...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

var mainWin = new MainWindow
{
DataContext = new MainWindowViewModel(),
};

desktop.MainWindow = mainWin;
mainWin.Show();
splashScreen.Close();
}
else
{
throw new ArgumentException("Unable To Create Json File.");
}
}
}
catch (TaskCanceledException)
{
splashScreen.Close();
return;
}
}

base.OnFrameworkInitializationCompleted();
}

private bool CheckForJsonFile()
{
bool jsonCheck = false;
string fileName = "ProjectSettings.Json";
ProjectSettings readProjectSettings;

if (File.Exists(fileName))
{
readProjectSettings = JsonConverter.DeserializeObject<ProjectSettings>(File.ReadAllText(fileName));
jsonCheck = true;
}
else
{
readProjectSettings = new ProjectSettings();
jsonCheck = false;
}

readProjectSettings.decProp1 = readProjectSettings.decProp1;
return jsonCheck;
}
}
using System;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Diary.ViewModels;
using Diary.Views;
using System.Threading.Tasks;

namespace Diary;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override async void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var splashScreenVM = new SplashScreenViewModel();
var splashScreen = new SplashScreen
{
DataContext = splashScreenVM
};
desktop.MainWindow = splashScreen;
splashScreen.Show();

try
{
splashScreenVM.StartupMessage = "Checking If config.json File Exists...";
await Task.Delay(1000, splashScreenVM.CancellationToken);

bool jsonExists = CheckForJsonFile();

if (jsonExists)
{
splashScreenVM.StartupMessage =
"config.json File Found. Loading Data From Json File...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

splashScreenVM.StartupMessage =
"Data Loaded From Json File... Launching Home Screen... Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

desktop.MainWindow = HomeScreen;
}
else
{
splashScreenVM.StartupMessage = "config.json File Not Found. Creating New Json File...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

bool jsonCreated = CreateJsonFile();

if (jsonCreated)
{
splashScreenVM.StartupMessage = "Json File Created. Launching Setup Screen...Please Wait!";
await Task.Delay(2000, splashScreenVM.CancellationToken);

var mainWin = new MainWindow
{
DataContext = new MainWindowViewModel(),
};

desktop.MainWindow = mainWin;
mainWin.Show();
splashScreen.Close();
}
else
{
throw new ArgumentException("Unable To Create Json File.");
}
}
}
catch (TaskCanceledException)
{
splashScreen.Close();
return;
}
}

base.OnFrameworkInitializationCompleted();
}

private bool CheckForJsonFile()
{
bool jsonCheck = false;
string fileName = "ProjectSettings.Json";
ProjectSettings readProjectSettings;

if (File.Exists(fileName))
{
readProjectSettings = JsonConverter.DeserializeObject<ProjectSettings>(File.ReadAllText(fileName));
jsonCheck = true;
}
else
{
readProjectSettings = new ProjectSettings();
jsonCheck = false;
}

readProjectSettings.decProp1 = readProjectSettings.decProp1;
return jsonCheck;
}
}
9 Replies
Mekasu0124
Mekasu0124OP16mo ago
this is my App.axaml.cs file. I am trying to get it to do the following functionality. - Launch SplashScreen - Set splashscreen message to Checking If config.json File Exists... - Run function to check if json file exists. If so, return true, else return false - if json check function returns true, then file exists and launch the home screen - if json check function returns false, then create the new json file and launch the setup screen I'm having some issues with getting this to work correctly. The first issue being that I'm not sure how to import json into the project. The rest of the issues is knowing that the logic works, etc. I have not ran this as I know it doesn't work and am requestin assistance on getting it to work. Thanks in advance. I do not have a creating a json file function in my code as I'm not sure if one is needed. the code for checking if the json file exists came from https://stackoverflow.com/questions/34064067/test-for-a-json-file-in-the-bin-folder and the repo that I'm following for creating my splash screen came from https://github.com/kivarsen/AvaloniaSplashScreenDemo/tree/master/AvaloniaSplashScreenDemo
Omnissiah
Omnissiah16mo ago
i would just try refactoring checks to be sequential instead of nested also what does "import json onto the project" means
Mekasu0124
Mekasu0124OP16mo ago
also what does "import json onto the project" means
using Json.convert or whatever the import statement is for working with a JSON file
i would just try refactoring checks to be sequential instead of nested
idk what this means
Omnissiah
Omnissiah16mo ago
idk what this means
not doing this
if ()
if ()
if ()
else
if ()
if ()
if ()
if ()
if ()
else
if ()
if ()
but this
if (fail)
return
if (fail)
return
return ok
if (fail)
return
if (fail)
return
return ok
so you mean deserialization
Mekasu0124
Mekasu0124OP16mo ago
Yes or whatever nuget package works with json
Omnissiah
Omnissiah16mo ago
mainly two, System.Text.Json and Newtonsoft, the usuals
arion
arion16mo ago
Multiple things we can unpack here. - You seem to be using vm.StartupMessage = "" and await Task.Delay(...)a lot, you can refactor this into a method. Simplest I can think of is adding a method to the VM as an async method then you just await that. - Your persistence logic (checking for json and loading it) can be its own class, then you can do saving / loading pretty nicely. If you plan to just use Json loading and saving, i'd recommend doing a sort of "safe-save" What I mean by safe-save is, write the settings to a temp file, when thats done, replace the temp file's name with the save file, temporarily name it something like config.part, everytime you save, delete all .part files in your app data folder (Saving something many times and then having your computer crash can wipe your config to just be an empty file) Additionally, Jot is a nice library I've come to use more often for settings persistence (even shows examples for GUI settings persistence (like window location) https://github.com/anakic/Jot
GitHub
GitHub - anakic/Jot: Jot is a library for persisting and applying ....
Jot is a library for persisting and applying .NET application state. - GitHub - anakic/Jot: Jot is a library for persisting and applying .NET application state.
arion
arion16mo ago
Avalonia also has a Dispatcher that you can dispatch async code so you don't have to use async void in your method
Dispatcher.UIThread.InvokeAsync(async () => {});
Dispatcher.UIThread.InvokeAsync(async () => {});
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server