Added message handling

This commit is contained in:
Thorsten Sommer 2021-09-23 18:58:52 +02:00
parent e4fc2d7512
commit 0c20c898da
3 changed files with 45 additions and 11 deletions

View File

@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
namespace BlazorWebassemblyI18n
{
public sealed partial class App : IMessage
{
[Inject] private SessionManager sessionManager { get; set; }
protected override Task OnInitializedAsync()
{
sessionManager.Register(this);
return base.OnInitializedAsync();
}
public Task ReceiveMessage(string message)
{
this.StateHasChanged();
return Task.CompletedTask;
}
}
}

View File

@ -1,7 +1,9 @@
@page "/employee"
@using Newtonsoft.Json;
@using R = BlazorWebassemblyI18n.Resources;
@inject IJSRuntime JSRuntime
@inject Microsoft.Extensions.Localization.IStringLocalizer<App> Localize
@implements IMessage
<h1>@title</h1>
<br />
@ -23,11 +25,11 @@
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label col-md-12">@Localize["City"]</label>
<label class="control-label col-md-12">@R.App.City</label>
<input class="form-control" @bind="employee.City" />
</div>
<div class="form-group">
<label class="control-label col-md-12">@Localize["Salary"]</label>
<label class="control-label col-md-12">@R.App.Salary</label>
<input type="number" class="form-control" @bind="employee.Salary" />
</div>
</div>
@ -65,19 +67,21 @@
</div>
@code{
[Inject] private SessionManager sessionManager { get; set; }
Employee employee = new Employee();
List<Employee> lstEmployees = new List<Employee>();
string title;
string title => string.Format(R.App.Title, companyName);
string companyName = "Phrase";
string[] TableHeader = { "Name", "Gender", "City", "Salary", "Joining Date" };
protected override async Task OnInitializedAsync()
{
setTitle();
var empGetJS = (IJSInProcessRuntime)JSRuntime;
var empList = await empGetJS.InvokeAsync<string>("employeeData.get");
FetchEmployeeFromLocalStorage(empList);
sessionManager.Register(this);
}
void SaveEmployeeToLocalStorage()
@ -89,6 +93,12 @@
employee = new Employee();
}
public Task ReceiveMessage(string message)
{
this.StateHasChanged();
return Task.CompletedTask;
}
void FetchEmployeeFromLocalStorage(string empList)
{
if (empList != null)
@ -97,12 +107,6 @@
}
}
void setTitle()
{
string localizedTitle = Localize["Title"];
title = string.Format(localizedTitle, companyName);
}
class Employee
{
public string Name { get; set; }

View File

@ -13,6 +13,9 @@
@code
{
[Inject]
SessionManager sessionManager {get;set;}
CultureInfo[] supportedLanguages = new[]
{
new CultureInfo("en-US"),
@ -29,8 +32,13 @@
{
var js = (IJSInProcessRuntime)JSRuntime;
js.InvokeVoid("appCulture.set", value.Name);
var cultureInfo = new CultureInfo(value.Name);
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
CultureInfo.CurrentCulture = cultureInfo;
Nav.NavigateTo(Nav.Uri, forceLoad: true);
//Nav.NavigateTo(Nav.Uri, forceLoad: true);
sessionManager.SendMessage($"culture was changed to '{value.Name}'");
}
}
}