ContentDialog.showAsync in a Win 10 Universal window application

I want to show contentDialog as a login screen as soon as the application is launched. Only if the user is authenticated, I want to show the rest of the page, otherwise nothing will work.

I do not want the user to click any button to download this content dialog, it should appear on its own as soon as the application starts.

In the MainPage constructor, I call the method to display the dialog.

But I get this exception "The value is not in the expected range." (System.ArgumentException), and the application does not load after that.

this is from my MainPage.xaml

 <ContentDialog x:Name="loginDialog"
                    VerticalAlignment="Stretch"
                    Title="Login"
                    PrimaryButtonText="Login"
                    SecondaryButtonText="Cancel">
                    <StackPanel>
                        <StackPanel>
                            <TextBlock Text="Username" />
                            <TextBox x:Name="Username" ></TextBox>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="Password" />
                            <TextBox x:Name="Password" ></TextBox>
                        </StackPanel>
                    </StackPanel>
                </ContentDialog>

Is it impossible? Can ContentDialog only start when a button is clicked? enter image description here enter image description here

+4
1

, , , OnNavigatedTo. , , await Task.Delay(1); , ShowPopup.

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    await Task.Delay(1);
    var result = await loginDialog.ShowAsync();
}

: @sibbl, Loaded event, . OnNavigatedTo, Prism MVVM, ViewModel - OnNavigatedTo, .

private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
    var result = await ShowPopup();
}

: async void ShowPopup, . /, "" . , :

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    await Task.Delay(1);
    var result = await ShowPopup();
}

private Task<ContentDialogResult> ShowPopup()
{
    return loginDialog.ShowAsync().AsTask();
}
+3

Source: https://habr.com/ru/post/1612361/


All Articles