Skip to content

This project implements a simple storage adapter which the avalonia WASM apps can use to interact with the Browser storage.

License

Notifications You must be signed in to change notification settings

SachiHarshitha/AvaloniaUI.Browser.Storage

Repository files navigation

AvaloniaWASM.Storage

NuGet Github All Releases

This library provides a simple way to use browser storage in AvaloniaUI applications. It supports both local, session storage as well as IndexedDB, allowing you to store key-value pairs that persist across sessions or only for the duration of the page load. The IndexedDB support is useful for storing larger files or structured data.

Sample Application

Session / Local Storage IndexedDB
image image

How to Use

Currently, the library is under constant development. To use it, you can clone the repository and reference the project in your AvaloniaUI application or can install the nuget package.

Nuget Package: AvaloniaWASM.Storage

  1. Clone the repository:
   git clone https://github.com/SachiHarshitha/AvaloniaWASM.Storage.git
  1. Add a reference to the AvaloniaWASM.Storage project in your AvaloniaUI application (Both in the base and startup projects).

  2. Add the following section to the Browser startup project (File : xxxx.Browser.csproj).

<!--Section :Direct Reference, Add following section if you refer the project directly-->
	<ItemGroup>
		<JsFiles Include="..\AvaloniaWASM.Storage\wwwroot\js\**\*.*" />
	</ItemGroup>

	<Target Name="CopyJsFilesBeforeBuild" BeforeTargets="Build">
		<Message Importance="high" Text="Copying JS files to wwwroot..." />
		<Copy
			SourceFiles="@(JsFiles)"
			DestinationFolder="$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)"
			SkipUnchangedFiles="true" />
	</Target>
<!-- End Section-->
  1. Add the required JSScript loading to your crossplatform project (File : xxxx.csproj).
	# Option  1
	# Direct Reference, Add the following section to your App.cs file or wherever you initialize your application.

	if (OperatingSystem.IsBrowser())
        {
            await JSHost.ImportAsync("FuncLocalStorage", "/js/FuncLocalStorage.js");
            await JSHost.ImportAsync("FuncSessionStorage", "/js/FuncSessionStorage.js");
			await JSHost.ImportAsync("FuncIndexedDbFile", "/js/FuncIndexedDbFile.js");

        }

	..........

	# Option  2
	# For Dependency Injection, inject the services in your `App.cs` file or wherever you configure your services.

	services.AddBrowserStorage();
  1. Use the LocalStorage and SessionStorage classes to store and retrieve data in your AvaloniaUI application.
	using AvaloniaWASM.Storage;

	.........

	# Initialize the storage services in your class or view model, if not dependecy Injection.
	private SessionStorageService _sessionStorageService = new SessionStorageService();
	private LocalStorageService _localStorageService = new LocalStorageService();

	..........

	# Session Storage Example
	await _sessionStorageService.SetItemAsync("Session_Text", ValueToSetSessionStorage);
	ValueFromSessionStorage = await _sessionStorageService.GetItemAsync("Session_Text");

	...........
	# Local Storage Example
	await _localStorageService.SetItemAsync("Local_Text", ValueToSetLocalStorage);
	ValueFromLocalStorage = await _localStorageService.GetItemAsync("Local_Text");

	...........
	# IndexedDB Example
	await IndexedDbFileService.SaveFileAsync(dbName, storeName, fileName, _fileContent, "text/plain");
	var filefromDB = await IndexedDbFileService.LoadFileAsBase64Async(dbName, storeName, fileName);

About

This project implements a simple storage adapter which the avalonia WASM apps can use to interact with the Browser storage.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published