gpt4 book ai didi

asp.net-core - Blazor - 更改 DI 服务的属性后, Razor 页面不会更新

转载 作者:行者123 更新时间:2023-12-02 17:20:46 25 4
gpt4 key购买 nike

使用 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;
}


}

会发生什么: Not rendering issue

预期行为:当单击菜单项“借书”时,页面应显示“图书已借出”,或者如果我单击“归还图书”,则应显示“图书已归还”。但只有当我单击另一个页面(例如主页)然后再次返回时,才会发生这种情况。

如何强制页面重新呈现/重新检查 BookService 中的更新值,即使它已经位于同一页面上?

谢谢!

最佳答案

这是一个新的解决方案,我在 BookService 类中实现了 INotifyPropertyChanged 接口(interface)。为什么使用这个接口(interface)?

  • 它也可以应用于其他属性

  • 通知客户属性值已发生变化是良好服务的重要组成部分,并且这不仅限于仅出于此目的调用 StateHasChanged。

  • 通过实现 INotifyPropertyChanged 接口(interface),我可以将事件数据传递给已注册或订阅的对象。

重要提示:调用像 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));
}
}
}

Books.razor

@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();
}
}

NavMenu.razor

@code {

// Code removed for brevity

private void LoanBookClicked()
{
bookService.IsLoaned = true;
}
private void ReturnBookClicked()
{
bookService.IsLoaned = false;
}
}

希望这有效...

关于asp.net-core - Blazor - 更改 DI 服务的属性后, Razor 页面不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58726847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com