建立共用類別
public class Filter
{
private string _filter;
public string filter
{
get
{
return _filter;
}
set
{
_filter = value;
NotifyDataChanged();
}
}
public event Action OnChange;
private void NotifyDataChanged() => OnChange?.Invoke();
}
startup.cs 註冊為服務
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<Filter>();
}
.razor
====
注入服務
@inject demo1.Filter Filter
顯示於畫面
<input type="text" @bind="Filter.filter" @bind:event="oninput">
設定物件內容改變時通知以便刷新頁面
protected override void OnInitialized()
{
Filter.OnChange+= OnMyChangeHandler;
}
public void Dispose()
{
Filter.OnChange -= OnMyChangeHandler;
}
private async void OnMyChangeHandler()
{
await InvokeAsync(StateHasChanged);
}
沒有留言:
張貼留言