How do I call method in Blazor component from XAML code?

94 Views Asked by At

Is there any way how to call blazor method (ShowSomething()) from xaml code (Button_clicked())?

for example:

@page "/"
<h1>Hello, world!</h1>
<h1>@if (testingString is not null)
    {
        @testingString
    }
    else
    {
       <h2>nothing is loaded</h2>
    }
</h1>
<button @onclick="ShowSomething">Update from Blazor</button>
@code{
    public string testingString { get; set; }
    public void ShowSomething()
    {
        testingString = "Showing something";
    }
}

MainPage.xaml.cs:

 public partial class MainPage : ContentPage
 {
     public MainPage()
     {
         InitializeComponent();
     }
     TestBackdrop.Pages.Index index = new TestBackdrop.Pages.Index();
     private void Button_clicked(object sender, EventArgs e)
     {
         index.ShowSomething();
         index.testingString = "experiment number 2";
     }
 }

and xaml page is simple grid with webview and button:

I tried to use StateHasChanged() but then I got

The render handle is not yet assigned

Is there any way to do it?

Thank you.

0

There are 0 best solutions below