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.
Session / Local Storage | IndexedDB |
---|---|
![]() |
![]() |
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
- Clone the repository:
git clone https://github.com/SachiHarshitha/AvaloniaWASM.Storage.git
-
Add a reference to the
AvaloniaWASM.Storage
project in your AvaloniaUI application (Both in the base and startup projects). -
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-->
- 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();
- Use the
LocalStorage
andSessionStorage
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);