Documentation example missing for MAUI x Blazor

Benoit Hoffman 51 Reputation points
2022-06-02T12:19:39.97+00:00

I have been trying to make the MAUI BlazorView work, and based myself on the documentation here: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/blazorwebview .

The problem I face is that the documentation specifically states that you can send a IDictionary<string, object?>? object as 'Parameters' to the RootComponent, but no example shows how to actually consumes those parameters in your Blazor component.

Would it be possible to have an explanation on how to do this and possibly update the documentation?

Thanks

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,860 questions
0 comments No comments
{count} vote

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,221 Reputation points Microsoft Vendor
    2022-06-03T09:39:52.94+00:00

    Hello @Benoit Hoffman ,

    no example shows how to actually consumes those parameters in your Blazor component

    You could refer to the Component parameters part in this doc : https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-6.0#component-parameters, and check Binding with component parameters

    If you add the Parameters for your RootComponent, try to find the Main. razor file, then add the Parameter, refer to the following code :

    Set Parameters in MainPage(set the RootComponent name in Xaml <RootComponent x:Name="RootComponent"....> )

    public MainPage()  
     {  
     InitializeComponent();  
     RootComponent.Parameters = new Dictionary<string, object> { { "MyPara", "222" } };  
           // or RootComponent.Parameters = new Dictionary<string, object> { ["MyPara"]= "111" };  
        }  
    

    Add Parameter attribute in Main. razor

    @code {  
        [Parameter]  
        public string MyPara { get; set; }  
    }  
    

    Display this Parameter for testing

    <Router AppAssembly="@typeof(Main).Assembly">  
     <Found Context="routeData">  
     ......  
     <h1>@MyPara</h1>  
     </Found>  
     ......  
    </Router>  
    

    ------UPDATE------
    We can't change parameters after setting them, because it only works on initialization. Form the source code, we can see that parameters is copied to ParameterView by WebViewManager on initialization. And we can check the WebViewManager source code, it only allows get and init method.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful