-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Index.razor
140 lines (113 loc) · 3.71 KB
/
Index.razor
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@page "/"
@inject ISessionStorageService sessionStorage
<h1>Blazored SessionStorage Sample</h1>
<hr class="mb-5" />
<div class="row mb-5">
<div class="col-md-4">
<h5>Add Item to session storage</h5>
<div class="input-group">
<input id="Name" class="form-control" type="text" placeholder="Enter a value" @bind="Name" />
<div class="input-group-append">
<button id="NameButton" class="btn btn-primary" @onclick="SaveName">Save</button>
</div>
</div>
</div>
<div class="col-md-4">
<h5>Remove item from session storage</h5>
<button class="btn btn-primary" @onclick="RemoveName">Remove Item</button>
</div>
<div class="col-md-4">
<h5>Clear session storage</h5>
<button class="btn btn-primary" @onclick="ClearSessionStorage">Clear All</button>
</div>
</div>
<div class="row mb-5">
<div class="col-md-4">
<h5>Value Read from session storage</h5>
@NameFromSessionStorage
</div>
<div class="col-md-4">
<h5>Items in session storage</h5>
@ItemsInSessionStorage
</div>
<div class="col-md-4">
<h5>Item exist in session storage</h5>@(ItemExist)
</div>
</div>
<div class="row">
<div class="col-md-4">
<h5>String Read from session storage</h5>
@StringFromSessionStorage
</div>
</div>
@code {
string NameFromSessionStorage { get; set; }
string StringFromSessionStorage { get; set; }
int ItemsInSessionStorage { get; set; }
string Name { get; set; }
bool ItemExist { get; set; }
protected override async Task OnInitializedAsync()
{
await GetNameFromSessionStorage();
await GetStringFromSessionStorage();
await GetSessionStorageLength();
sessionStorage.Changed += (sender, e) =>
{
Console.WriteLine($"Value for key {e.Key} changed from {e.OldValue} to {e.NewValue}");
};
}
async Task SaveName()
{
await sessionStorage.SetItemAsync("name", Name);
await GetNameFromSessionStorage();
await GetStringFromSessionStorage();
await GetSessionStorageLength();
Name = "";
}
async Task GetNameFromSessionStorage()
{
try
{
NameFromSessionStorage = await sessionStorage.GetItemAsync<string>("name");
if (string.IsNullOrEmpty(NameFromSessionStorage))
{
NameFromSessionStorage = "Nothing Saved";
}
}
catch(Exception)
{
Console.WriteLine("error reading 'name'");
}
}
async Task GetStringFromSessionStorage()
{
StringFromSessionStorage = await sessionStorage.GetItemAsStringAsync("name");
if (string.IsNullOrEmpty(StringFromSessionStorage))
{
StringFromSessionStorage = "Nothing Saved";
}
}
async Task RemoveName()
{
await sessionStorage.RemoveItemAsync("name");
await GetNameFromSessionStorage();
await GetStringFromSessionStorage();
await GetSessionStorageLength();
}
async Task ClearSessionStorage()
{
Console.WriteLine("Calling Clear...");
await sessionStorage.ClearAsync();
Console.WriteLine("Getting name from session storage...");
await GetNameFromSessionStorage();
await GetStringFromSessionStorage();
Console.WriteLine("Calling Get Length...");
await GetSessionStorageLength();
}
async Task GetSessionStorageLength()
{
Console.WriteLine(await sessionStorage.LengthAsync());
ItemsInSessionStorage = await sessionStorage.LengthAsync();
ItemExist = await sessionStorage.ContainKeyAsync("name");
}
}