-
Notifications
You must be signed in to change notification settings - Fork 2
/
Index.razor.cs
43 lines (35 loc) · 1.24 KB
/
Index.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace MauiBlazorUIBestPractices.Pages;
public partial class Index : ComponentBase
{
public int WindowWidth { get; set; }
private string _resizeEventListenerId = string.Empty;
private DotNetObjectReference<Index>? _dotnetObjectReference;
[Inject] private IJSRuntime JSRuntime { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
_dotnetObjectReference = DotNetObjectReference.Create(this);
await base.OnInitializedAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("UpdateWindowWidth", _dotnetObjectReference);
await InitWindowWidthListener();
}
await base.OnAfterRenderAsync(firstRender);
}
[JSInvokable]
public void UpdateWindowWidth(int windowWidth)
{
WindowWidth = windowWidth;
StateHasChanged();
}
private async Task InitWindowWidthListener()
{
_resizeEventListenerId = Guid.NewGuid().ToString();
await JSRuntime.InvokeVoidAsync("AddWindowWidthListener", _dotnetObjectReference, _resizeEventListenerId);
}
}