Search code examples
c#mauimaui-blazormaui-windows

How to get current window?


I have problem. I have code in MAUI -> Blazor.

In Blazor must be two buttons: 1 - create new window 2 - show alert in current window.

And Idk how to find current Window. if I use Application.Current.MainPage -> it's will be reference to first window. But I want show alert in current window.

example of code:


@page "/" 
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Web


@inject IJSRuntime JS

<div class="row">
    <div class="col-md-4 mb-3">
        <button class="btn btn-primary" @onclick="CreateNewWindow">Create new window</button>
    </div>
    <div class="col-md-4 mb-3">
        <button class="btn btn-primary" @onclick="ShowAlert">Show alert!</button>
    </div>
</div>



@code {

    private async Task ShowAlert()
    {
        Page? currentWindow = Application.Current.MainPage;
        if (currentWindow != null)
        {
            await currentWindow.DisplayAlert("Good", "Nice", "Test");
        }
    }

    private async Task CreateNewWindow()
    {
        var newWindow = new Window(new MainPage());
        (Application.Current as App).OpenWindow(newWindow);
    }
}



How can I get instance of current window in which I click to button?

enter image description here

I expected that you help me)


Solution

  • How can I get instance of current window in which I click to button?

    For the button is in the razor page, I can't find any way to get the current instance of the window or page. But for a workaround, you can add the reference of the MainPage into the razor page.

    1. Using the Static member (needn't to consider the memory leak because there is only a mainpage in a window):

    In the MainPage:

    public partial class MainPage : ContentPage
        {
            public static Page Instance;
            public MainPage()
            {
                InitializeComponent();
                Instance = this;
            }
           
        }
    

    In the Razor page:

    @code {
        private async Task ShowAlert()
        {
           await MainPage.Instance.DisplayAlert("Good", "Nice", "Test");
        }
    
    1. Passing the page as parameter:

    In the xaml:

    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:MauiBlazorWindow.Components.Pages"
                 x:Class="MauiBlazorWindow.MainPage"
                 BackgroundColor="{DynamicResource PageBackgroundColor}">
    
        <BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html">
            <BlazorWebView.RootComponents>
                <RootComponent x:Name="root"  Selector="#app" ComponentType="{x:Type local:Home}" />
            </BlazorWebView.RootComponents>
        </BlazorWebView>
    
    </ContentPage>
    

    In the code behind:

     public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
                root.Parameters = new Dictionary<string, object>()
                {
                    {"CurrentPage",this }
                }; 
            }
           
        }
    

    In the razor page:

    @code {
        [Parameter]
        public Page CurrentPage { get; set; }
        private async Task ShowAlert()
        {
           await CurrentPage.DisplayAlert("Good", "Nice", "Test");
        }