I created the following components as .razor files within a freshly genered blazor server app on asp.net core 3.1:
If you access the page /P
via either the IIS Express or the .exe stand alone app are crashing with an Access Violation:
iisexpress.exe" wurde mit Code -1073741819 (0xc0000005) 'Access violation' beendet.
While debugging it looks like it crashes after setting the EventCallBack
at the child component.
parent.razor:
@page "/P"
<h3>Parent</h3>
Parent: @Name
<Child @bind-Name="Name" />
@code {
public string Name { get; set; } = "Bla";
}
Child.razor
<h3>Child</h3>
Value: @Name
@code {
private string _Name;
[Parameter]
public string Name
{
get => Name; set
{
if (value != _Name)
{
_Name = value;
//NameChanged.InvokeAsync(_Name).Wait();
}
}
}
[Parameter]
public EventCallback<string> NameChanged { get; set; }
}
Edit: Within the next step I want to change the Name
field within the child component and use the callback function to send the changes to the parent.
Do anyone have an solution or an idea to get more information?
Couple of things I would suggest for you to try to get you moving along;
On the child component , define the EventCallback
as a parameter. Add some mechanism for changing the Name
and then invoking the EventCallback
. In this example I have used a text box (with two way binding to keep the Name
property up to date) and a button to invoke the callback.
<h3>Child</h3>
<input @bind-Value="Name" />
<button @onclick="OnButtonClicked" >Click me</button>
@code {
[Parameter]
public string Name {get; set;}
[Parameter]
public EventCallback<string> NameChanged {get; set;}
public async Task OnButtonClicked(){
await NameChanged.InvokeAsync(Name);
}
}
On parent page set a default string in the page initialisation, then bind this to the child component. Also define an event handler for the name change which is bubbled from the child component.;
@page "/mypage"
<h3>Parent</h3>
Parent: @Name
<Child Name="@Name" NameChanged="HandleNameChanged" }
@code {
public string Name { get; set; }
public override void OnInitialized(){
Name = "bla";
}
public void HandleNameChanged(string changedName){
Name = changedName;
}
}
I found the answer ... its a typo at the getter on the shout be get => _Name
to access the backing field.
So it goes goes to an infinite loop which will lead to the crash...
User contributions licensed under CC BY-SA 3.0