Blazor:将复杂类型作为参数传递

Blazor:将复杂类型作为参数传递,blazor,blazor-client-side,Blazor,Blazor Client Side,我正在尝试将我的简单应用程序组件化,当所有东西都在一个组件中时,它就工作了。然而,当我将按钮逻辑移入它自己的子组件并移出主组件时,我得到以下结果 Unable to set property 'story' on object of type 'Shared.ChoiceButton'. The error was: Specified cast is not valid. ---> System.InvalidCastException: Specified cast is not va

我正在尝试将我的简单应用程序组件化,当所有东西都在一个组件中时,它就工作了。然而,当我将按钮逻辑移入它自己的子组件并移出主组件时,我得到以下结果

Unable to set property 'story' on object of type 'Shared.ChoiceButton'. The error was: Specified cast is not valid. ---> System.InvalidCastException: Specified cast is not valid
我可以看出这是一个铸造错误,但当我尝试同样的事情与灵长类动物,它的工作。我遗漏了什么技巧来处理复杂类型

父组件

 <div id="choices">
        @foreach (var choice in story?.currentChoices)
        {
            <ChoiceButton story="@story" choice="@choice" />
        }
 </div>

@code {
    public Story story;

    protected override async Task OnInitializedAsync()
    {
        story = new Story(await Http.GetStringAsync("sample-data/Deamons.json"));
    }
}

@foreach(故事中的var选择?.currentChoices)
{
}
@代码{
公共故事;
受保护的重写异步任务OnInitializedAsync()
{
story=newstory(等待Http.GetStringAsync(“sample data/Deamons.json”);
}
}
我的新子组件(ChoiceButton)

@choice.text
@代码{
[参数]公共故事{get;set;}
[参数]公共选择选项{get;set;}
无效选择(选择)
{
story.ChooseChoiceIndex(choice.index);
故事。连续最大();
}
}

我怀疑新的故事(等待…当您从该构造函数中删除http调用时会发生什么情况?我不知道这是否是打字错误,但ChoiceButton中的参数是大写的,但当您使用ChoiceButton并传递值时,您使用小写。C区分大小写,因此无法识别“故事”作为ChoiceButton上的一个参数。@Mistermago选择按钮中的参数是小写的,与传入的参数匹配,类型是大写的。
story
类型是
story
,而
choice
类型是
choice
你说得对,我没有仔细阅读!有可能将整个项目发布到某个地方吗?很难在没有整个上下文的情况下讲述。我怀疑新的故事(等待…当您从该构造函数中删除http调用时会发生什么?我不知道这是否是一个输入错误,但ChoiceButton中的参数是大写的,但是当您使用ChoiceButton并传递值时,您使用了小写。C#区分大小写,因此无法识别“故事”作为ChoiceButton上的一个参数。@Mistermago选择按钮中的参数是小写的,与传入的参数匹配,类型是大写的。
story
类型是
story
,而
choice
类型是
choice
你说得对,我没有仔细阅读!有可能将整个项目发布到某个地方吗?很难不带背景地讲。
<button class="choiceButton" @onclick="() => Choose(choice)">@choice.text</button>

@code {

    [Parameter] public Story story { get; set; }
    [Parameter] public Choice choice { get; set; }

    void Choose(Choice choice)
    {
        story.ChooseChoiceIndex(choice.index);
        story.ContinueMaximally();
    }
}