PatrickJahr.Blazor.ScreenWakeLock
1.0.0
dotnet add package PatrickJahr.Blazor.ScreenWakeLock --version 1.0.0
NuGet\Install-Package PatrickJahr.Blazor.ScreenWakeLock -Version 1.0.0
<PackageReference Include="PatrickJahr.Blazor.ScreenWakeLock" Version="1.0.0" />
paket add PatrickJahr.Blazor.ScreenWakeLock --version 1.0.0
#r "nuget: PatrickJahr.Blazor.ScreenWakeLock, 1.0.0"
// Install PatrickJahr.Blazor.ScreenWakeLock as a Cake Addin #addin nuget:?package=PatrickJahr.Blazor.ScreenWakeLock&version=1.0.0 // Install PatrickJahr.Blazor.ScreenWakeLock as a Cake Tool #tool nuget:?package=PatrickJahr.Blazor.ScreenWakeLock&version=1.0.0
PatrickJahr.Blazor.ScreenWakeLock
Introduction
A Blazor wrapper for the Screen Wake Lock API.
The Screen Wake Lock API allows web applications to request a screen wake lock. Under the right conditions, and if allowed, the screen wake lock prevents the system from turning off a device's screen.
Getting started
Prerequisites
You need .NET 7.0 or newer to use this library.
Download .NET 7 Download .NET 8
Platform support
Installation
You can install the package via NuGet with the Package Manager in your IDE or alternatively using the command line:
dotnet add package PatrickJahr.Blazor.ScreenWakeLock
Usage
The package can be used in Blazor WebAssembly projects.
Add to service collection
To make the ScreenWakeLockService available on all pages, register it at the IServiceCollection in Program.cs
before the host is built:
builder.Services.AddScreenWakeLockService();
Checking for browser support
Before using the Screen Wake Lock API, you should first test if the API is supported on the target platform by calling the IsSupportedAsync()
method.
This method returns a boolean to indicate whether the Screen Wake Lock API is supported or not.
var isSupported = await screenWakeLock.IsSupportedAsync();
if (isSupported)
{
// enable screen wake lock feature
}
else
{
// use fallback mechanism or hide/disable feature
}
Request screen wake lock
To request a screen wake lock, you need to call the RequestWakeLockAsync()
method from the IScreenWakeLockService
service.
The browser can refuse the request for various reasons (for example, because the battery charge level is too low),
so it's a good practice to wrap the call in a try…catch
statement.
The exception's message will contain more details in case of failure.
try
{
await _screenWakeLockService.RequestWakeLockAsync();
}
catch(Exception e)
{
// Handle exxception
}
Release screen wake lock
You also need a way to release the screen wake lock, which is achieved by calling the ReleaseWakeLockAsync()
method of the IScreenWakeLockService
service.
await _screenWakeLockService.ReleaseWakeLockAsync();
As soon as the object was released the action WakeLockReleased
will be fired.
protected override async Task OnInitializedAsync()
{
//...
_screenWakeLockService.WakeLockReleased = () =>
{
_wakeLockRequested = false;
};
//...
await base.OnInitializedAsync();
}
Related articles
Acknowledgements
Thanks to Kristoffer Strube who provides a Blazor wrapper for the File System Access API. This library is inspired by Kristoffer's implementation and project setup.
License and Note
BSD-3-Clause.
This is a technical showcase, not an official PatrickJahr product.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.17)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 932 | 3/15/2024 |