An open API service indexing awesome lists of open source software.

https://github.com/sachiharshitha/avaloniaui.browser.storage

This project implements a simple storage adapter which the avalonia WASM apps can use to interact with the Browser storage.
https://github.com/sachiharshitha/avaloniaui.browser.storage

avalonia avaloniaui browser chrome crossplatform storage storage-api wasm

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

          

# AvaloniaWASM.Storage

[![NuGet](https://img.shields.io/nuget/v/AvaloniaWASM.Storage.svg)](https://www.nuget.org/packages/AvaloniaWASM.Storage/)
[![Github All Releases](https://img.shields.io/nuget/dt/AvaloniaWASM.Storage.svg)]()

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](https://github.com/user-attachments/assets/32aa25af-08d2-4a3b-ad2d-8ea231c3e12d) | ![image](https://github.com/user-attachments/assets/4dcf76ee-dd3d-436b-bf8f-c08f6fe7b25d) |

## 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](https://www.nuget.org/packages/AvaloniaWASM.Storage/)

1. Clone the repository:
```bash
git clone https://github.com/SachiHarshitha/AvaloniaWASM.Storage.git
```

2. Add a reference to the `AvaloniaWASM.Storage` project in your AvaloniaUI application (Both in the base and startup projects).

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

```xml






```

4. Add the required JSScript loading to your crossplatform project (File : xxxx.csproj).

```csharp

# 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();

```

5. Use the `LocalStorage` and `SessionStorage` classes to store and retrieve data in your AvaloniaUI application.
```csharp
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);
```