C# 如何在视图模型中使用管线参数?
我的剃须刀组件是这样开始的:C# 如何在视图模型中使用管线参数?,c#,asp.net-core,blazor,blazor-server-side,C#,Asp.net Core,Blazor,Blazor Server Side,我的剃须刀组件是这样开始的: @page "/track/{TrackId}" @using BlazorSPA1.ViewModels @inject ITrackViewModel TrackViewModel My TrackViewModel具有公共属性TrackId: [Parameter] public string TrackId { get; set; } 问题是我的视图vodel中的TrackId属性被忽略了。我将获得以下异常: 错误:System.In
@page "/track/{TrackId}"
@using BlazorSPA1.ViewModels
@inject ITrackViewModel TrackViewModel
My TrackViewModel具有公共属性TrackId:
[Parameter]
public string TrackId { get; set; }
问题是我的视图vodel中的TrackId属性被忽略了。我将获得以下异常:
错误:System.InvalidOperationException:类型为“BlazorSPA1.Pages.Track”的对象没有与名称“TrackId”匹配的属性。
位于Microsoft.AspNetCore.Components.Reflection.ComponentProperties.ThrowForUnknownIncomingParameterName(键入targetType,字符串parameterName)
位于Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView¶meters,对象目标)
位于Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(对象目标)
位于Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView参数)
位于Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView参数)
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext&DiffContext,Int32 frameIndex)中
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext&DiffContext,Int32 frameIndex)中
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext&DiffContext,Int32 newFrameIndex)中
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&DiffContext、Int32 oldFrameIndex、Int32 newFrameIndex)中
位于Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&DiffContext,Int32 oldStartIndex,Int32 OldEndIndexCcl,Int32 newStartIndex,Int32 NewEndIndexCcl)
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&DiffContext、Int32 oldFrameIndex、Int32 newFrameIndex)中
位于Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&DiffContext,Int32 oldStartIndex,Int32 OldEndIndexCcl,Int32 newStartIndex,Int32 NewEndIndexCcl)
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&DiffContext、Int32 oldFrameIndex、Int32 newFrameIndex)中
位于Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&DiffContext,Int32 oldStartIndex,Int32 OldEndIndexCcl,Int32 newStartIndex,Int32 NewEndIndexCcl)
在Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&DiffContext、Int32 oldFrameIndex、Int32 newFrameIndex)中
位于Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&DiffContext,Int32 oldStartIndex,Int32 OldEndIndexCcl,Int32 newStartIndex,Int32 NewEndIndexCcl)
位于Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(渲染器渲染器、RenderBatchBuilder、Int32 componentId、ArrayRange1 oldTree、ArrayRange
1 newTree)
在Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder、RenderFragment和RenderFragment)中
位于Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry RenderQueueEntry)
在Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()中
摆脱该例外的唯一方法是直接在my razor组件中定义属性:
@functions {
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await TrackViewModel.OnInitialyzeAsync();
}
[Parameter]
public string TrackId { get; set; }
}
有没有办法使用“我的视图模型”中的属性实现此功能?您的视图模型的基类是什么?Blazor在可路由组件上进行[Parameter]注入,所以这不是实现的方法。@Henkholtman我还没有基类。那么OnInitialyzeAsync从何而来?Blazor:那么,(I)TrackViewModel的基类是什么?ViewModel的基类是什么?Blazor在可路由组件上进行[Parameter]注入,所以这不是实现的方法。@Henkholtman我还没有基类。那么OnInitialyzeAsync从何而来?来自Blazor:那么,(I)TrackViewModel的基类是什么?