gpt4 book ai didi

razor - 如何在同一 Blazor 页面上的 Razor 组件之间传递数据?

转载 作者:行者123 更新时间:2023-12-02 06:26:47 30 4
gpt4 key购买 nike

我有这个 Blazor 页面

@page "/bearoffdata"
@using BlazorBoinq.Components

<h3>Bearoff Data</h3>

<Position_Hex_IdPair />

<PositionData />

@code {

}
使用这两个 Razor 组件:
@using BlazorBoinq.Data
@using BgBearoffCoreNamespace;
@inject BgBearoffService BoService

<label>Position</label>

<input type="text" spellcheck="false" @bind-value="@PositionText" @bind-value:event="oninput" />

<span> = </span>

<input type="number" step="1" @bind-value="@PositionId" @bind-value:event="oninput" />

<label>Id</label>

@code {

BgBearoffCore BgBo;

protected override async Task OnInitializedAsync()
{
BgBo = await BoService.GetBgBearoffAsync();
}

private Int64 positionId;
private String positionText;

protected Int64 PositionId
{
get => positionId;
set
{
positionId = value;
if (positionId > 0 && positionId <= BgBo.MaxId)
{
positionText = BgBearoffCore.menOnPointToHexString(BgBo.getMenOnPointFromInvariantId(positionId));
}
else
positionText = "";
}
}

protected String PositionText
{
get => positionText;
set
{
positionText = value;
if (BgBo.IsValidHexPosition(positionText))
positionId = BgBo.getInvariantIdFromPosition(positionText);
else
positionId = 0;
}
}
}
@using BlazorBoinq.Data
@using BgBearoffCoreNamespace;
@inject BgBearoffService BoService

<button class="btn btn-primary" @onclick="ShowBearoffInfo">Show Data</button>

<br>

<textarea cols="36" rows="36" readonly @bind="@BearoffInfo" />


@code {
BgBearoffCore BgBo;

protected override async Task OnInitializedAsync()
{
BgBo = await BoService.GetBgBearoffAsync();
}


private String bearoffInfo = "";
public String BearoffInfo
{
get => bearoffInfo;
set { }
}
protected void ShowBearoffInfo()
{
bearoffInfo = BgBo.getPositionInformationText(86);
}
}
我想通过 PositionId将第一个组件转换为第二个组件,因此我可以将最后一行中的硬编码 86 替换为 PositionId范围。

最佳答案

是的,你有两个不直接相关的控件,所以你不能简单地传递一个参数。

两种选择:

级联参数:https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#cascading-values-and-parameters

或状态管理。对于状态管理,这可能会有所帮助:
Implementing State Management In Blazor

您有这样的类(class):

using System;
public class CounterState
{
// _currentCount holds the current counter value
// for the entire application
private int _currentCount = 0;
// StateChanged is an event handler other pages
// can subscribe to
public event EventHandler StateChanged;
// This method will always return the current count
public int GetCurrentCount()
{
return _currentCount;
}
// This method will be called to update the current count
public void SetCurrentCount(int paramCount)
{
_currentCount = paramCount;
StateHasChanged();
}
// This method will allow us to reset the current count
public void ResetCurrentCount()
{
_currentCount = 0;
StateHasChanged();
}
private void StateHasChanged()
{
// This will update any subscribers
// that the counter state has changed
// so they can update themselves
// and show the current counter value
StateChanged?.Invoke(this, EventArgs.Empty);
}
}

您可以像这样在您的 startup.cs 文件中注册它:
services.AddScoped<CounterState>();

您在每个 .razor 控件中引用它,如下所示:
@inject CounterState CounterState

一个控件可以设置如下值:
// Call the GetCurrentCount() method
// to get the current count
int CurrentCount = CounterState.GetCurrentCount();
// Increase the count
CurrentCount++;
// Set Current count on the Session State object
CounterState.SetCurrentCount(CurrentCount);

位于应用程序中任何位置的另一个控件可以接收如下值:
   // This method is called when the control is initialized
protected override void OnInitialized()
{
// Subscribe to the StateChanged EventHandler
CounterState.StateChanged +=
OnCounterStateAdvancedStateChanged;
}
// This method is fired when the CounterState object
// invokes its StateHasChanged() method
// This will cause this control to invoke its own
// StateHasChanged() method refreshing the page
// and displaying the updated counter value
void OnCounterStateAdvancedStateChanged(
object sender, EventArgs e) => StateHasChanged();
void IDisposable.Dispose()
{
// When this control is disposed of
// unsubscribe from the StateChanged EventHandler
CounterState.StateChanged -=
OnCounterStateAdvancedStateChanged;
}

关于razor - 如何在同一 Blazor 页面上的 Razor 组件之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58510707/

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