使用dotnet 3.1.100-preview2-014569
好,考虑以下示例:
从模板创建一个新的Blazor WebAssemply项目,然后添加以下内容:
books.razor
@page "/books"
@inject BookService bookService
@if (bookService.isLoaned)
{
<p><em>Book loaned</em></p>
}
else
{
<p><em>Book returned</em></p>
}
BookService.cs
public class BookService
{
public int BookId { get; set; }
public string Title { get; set; }
public bool isLoaned { get; set; }
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<BookService>();
}
NavMenu.razor
@inject BookService bookService;
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">BlazorBlank_PV2</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="books" @onclick="LoanBookClicked">
<span class="oi oi-plus" aria-hidden="true"></span>Loan Book
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="books" @onclick="ReturnBookClicked">
<span class="oi oi-list-rich" aria-hidden="true"></span>Return Book
</NavLink>
</li>
</ul>
</div>
@code {
private bool collapseNavMenu = true;
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
private void LoanBookClicked()
{
bookService.isLoaned = true;
}
private void ReturnBookClicked()
{
bookService.isLoaned = false;
}
}
预期的行为: 单击菜单项“借书”时,页面应显示“借书”,或者如果我单击“退还书”,则应显示“已退还书”。但这只会在我单击另一个页面(如“主页”)然后再次返回时发生。
即使页面已经在同一页面上,如何强制页面重新呈现/重新检查BookService中的更新值?
谢谢!
答案 0 :(得分:3)
books.razor(为图书服务的新onchange事件添加处理程序)
@page "/books"
@inject BookService bookService
@if (bookService.isLoaned)
{
<p><em>Book loaned</em></p>
}
else
{
<p><em>Book returned</em></p>
}
@code{
protected override void OnInitialized()
{
bookService.OnChange += StateHasChanged;
}
}
BookService.cs(添加事件操作,将属性更改为只读,添加设置器,触发事件将为NotifyStateChanged()
public class BookService
{
public event Action OnChange;
public int BookId { get; set; }
public string Title { get; set; }
public bool isLoaned { get; }
public void SetLoanedState(bool State)
{
isLoaned = State;
NotifyStateChanged();
}
private void NotifyStateChanged() => OnChange?.Invoke();
}
NavMenu.razor(更改为使用服务SetLoanedState()
@inject BookService bookService;
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">BlazorBlank_PV2</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="books" @onclick="LoanBookClicked">
<span class="oi oi-plus" aria-hidden="true"></span>Loan Book
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="books" @onclick="ReturnBookClicked">
<span class="oi oi-list-rich" aria-hidden="true"></span>Return Book
</NavLink>
</li>
</ul>
</div>
@code {
private bool collapseNavMenu = true;
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
private void LoanBookClicked()
{
bookService.SetLoanedState(true);
}
private void ReturnBookClicked()
{
bookService.SetLoanedState(false);
}
}
有关更多信息,请查看Chris Sainty的博客文章here,它非常清楚
答案 1 :(得分:1)
这是一个新的解决方案,其中我在BookService类中实现了INotifyPropertyChanged接口。为什么要使用此界面?
它也可以应用于其他属性
通知客户属性值已更改是良好服务的重要组成部分,并且不仅限于为此目的而调用StateHasChanged。
实现了INotifyPropertyChanged接口,我可以将事件数据传递给已注册或预订的对象。
重要:调用诸如bookService.SetLoanedState(false)
之类的更新属性值的方法是不好的编程和反模式设计。一个属性可以有两个访问器,即get和set,它们应用于获取值和更改值。方法有不同的作用...
BookService.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class BookService : INotifyPropertyChanged
{
private bool isLoaned;
public event PropertyChangedEventHandler PropertyChanged;
public int BookId { get; set; }
public string Title { get; set; }
public bool IsLoaned
{
get
{
return this.isLoaned;
}
set
{
if (value != this.isLoaned)
{
this.isLoaned = value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
@page "/books"
@inject BookService bookService
@using System.ComponentModel
@if (bookService.IsLoaned)
{
<p><em>Book loaned</em></p>
}
else
{
<p><em>Book returned</em></p>
}
@code{
protected override void OnInitialized()
{
bookService.PropertyChanged += PropertyHasChanged;
}
private void PropertyHasChanged(object sender, PropertyChangedEventArgs args)
{
StateHasChanged();
}
}
@code {
// Code removed for brevity
private void LoanBookClicked()
{
bookService.IsLoaned = true;
}
private void ReturnBookClicked()
{
bookService.IsLoaned = false;
}
}
希望这行得通...