Skip to content

Commit 4c8456f

Browse files
committed
Enhance the usage of the Messaging center in the sample
1 parent b58901c commit 4c8456f

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
@page "/MyAccount"
22

33
<h3>Update my Username</h3>
4-
<EditForm Model="new object()" OnValidSubmit="UpdateUsername">
54
<div class="row">
65
<div class="col-4">
76
<div class="form-group">
87
<label>New username:</label>
9-
<input type="text" @bind-value="_username" class="form-control"/>
10-
<button type="submit" class="btn btn-primary m-3">Save</button>
11-
</div>
8+
<input type="text" @bind-value="_username" class="form-control" @onkeyup="UpdateUsername"/>
9+
<button type="submit" class="btn btn-primary m-3" @onclick="() => UpdateUsername(new KeyboardEventArgs())">Save</button>
1210
</div>
1311
</div>
14-
</EditForm>
12+
</div>
1513

src/BlazorUtilitiesSample/Pages/MyAccount.razor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public partial class MyAccount
2222

2323
private string _username = UserSettings.DefaultUsername;
2424

25-
private void UpdateUsername()
25+
private void UpdateUsername(KeyboardEventArgs args)
2626
{
2727
// Set the new value of the username
2828
UserSettings.DefaultUsername = _username;
2929

3030
// Send the updated
3131
// You can use the Send function to send the sender object with the value you want to send
32-
MessagingCenter.Send("username_updated", _username);
32+
MessagingCenter.Send(this, "username_updated", _username);
3333
}
3434

3535
}

src/BlazorUtilitiesSample/Shared/MainLayout.razor

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626

2727
protected override void OnInitialized()
2828
{
29-
string newUsername = string.Empty;
30-
31-
// Subscribe to "username_updated" message so we can take an action whenever the username gots udpatd from the my account page
32-
MessagingCenter.Subscribe<string>(this, "username_updated", arg =>
33-
{
34-
_username = arg;
35-
StateHasChanged();
29+
string newUsername = string.Empty;
30+
31+
// Subscribe to "username_updated" message so we can take an action whenever the username gots udpatd from the my account page
32+
MessagingCenter.Subscribe<MyAccount, string>(this, "username_updated", (sender, arg) =>
33+
{
34+
_username = arg;
35+
StateHasChanged();
3636
});
3737
base.OnInitialized();
3838
}

src/BlazorUtilitiesSample/Shared/NavMenu.razor

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@using AKSoftware.Blazor.Utilities
2+
@using BlazorUtilitiesSample.Pages
3+
24
<div class="top-row pl-4 navbar navbar-dark">
35
<a class="navbar-brand" href="">BlazorUtilitiesSample</a>
46
<button class="navbar-toggler" @onclick="ToggleNavMenu">
@@ -10,7 +12,7 @@
1012
<ul class="nav flex-column">
1113
<li class="nav-item px-3">
1214
<NavLink class="nav-link" href="myaccount">
13-
<span class="oi oi-person" aria-hidden="true"></span> Welcome @_username
15+
<span class="oi oi-person" aria-hidden="true"></span> @_username
1416
</NavLink>
1517
</li>
1618
<li class="nav-item px-3">
@@ -46,7 +48,7 @@
4648
protected override void OnInitialized()
4749
{
4850
// Subscribe to "username_updated" message so we can take an action whenever the username gots udpatd from the my account page
49-
MessagingCenter.Subscribe<string>(this, "username_updated", arg =>
51+
MessagingCenter.Subscribe<MyAccount, string>(this, "username_updated", (sender, arg) =>
5052
{
5153
_username = arg;
5254
StateHasChanged();

0 commit comments

Comments
 (0)