Guide des contrôles WinUI 3 C++ — Fenêtres flottantes Popup (Notifications Popup)

Nous allons d’abord présenter le contrôle Popup

Dans le XAML, ajoutez un bouton qui ouvre ce Popup au clic. Déclarez également un Popup masqué par défaut ; il ne s’affichera qu’après avoir cliqué sur le bouton.

    <StackPanel Spacing="24" VerticalAlignment="Top">

        <!-- Conteneur racine natif du Popup -->
        <Popup x:Name="MainPopup" Width="320" Height="180">
            <Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="8" Padding="16">
                <StackPanel Spacing="12">
                    <TextBlock Text="Contenu personnalisé du Popup" FontSize="18"/>
                    <TextBlock Text="Il s'agit du conteneur de base natif Popup"/>
                    <Button Content="Fermer le Popup" Click="BtnClosePopup_Click"/>
                </StackPanel>
            </Border>
        </Popup>

        <Button Content="Ouvrir le conteneur Popup natif" Click="BtnOpenPopup_Click" HorizontalAlignment="Left"
        ToolTipService.ToolTip="Ouvrir la couche Popup de base brute"/>

    </StackPanel>Langage du code : HTML, XML (xml)

Ensuite, créez le gestionnaire d’événement de clic BtnOpenPopup_Click qui affiche le Popup. La balise <Popup x:Name="MainPopup" Width="320" Height="180"> ...</Popup> permet de créer une fenêtre flottante masquée par défaut. À l’intérieur se trouve un bouton <Button Content="Fermer le Popup" Click="BtnClosePopup_Click"/> qui ferme le Popup parent lorsqu’on clique dessus.

Appuyez sur F12 pour générer automatiquement les deux gestionnaires d’événements du bouton

Fichier d’en-tête .H

#pragma once

#include "MainWindow.g.h"

namespace winrt::AppWinui::implementation
{
    struct MainWindow : MainWindowT<MainWindow>
    {
    public:
        MainWindow();

        int32_t MyProperty();
        void MyProperty(int32_t value);

        void BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
        void BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
    };
}

namespace winrt::AppWinui::factory_implementation
{
    struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
    {
    };
}
Langage du code : C++ (cpp)
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Globalization.h>

#include <winrt/Windows.UI.h> //Windows::UI::Colors


// Alias d'espace de noms global pour corriger les erreurs de classe introuvable et RoutedEventArgs non défini
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Controls::Primitives;


// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace winrt::AppWinui::implementation
{
    int32_t MainWindow::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainWindow::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    MainWindow::MainWindow()
    {
        InitializeComponent();
    }
}


void winrt::AppWinui::implementation::MainWindow::BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(true);
}

void winrt::AppWinui::implementation::MainWindow::BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(false);
}
Langage du code : C++ (cpp)

Guide des contrôles WinUI 3 C++ — Fenêtres flottantes Popup (Notifications Popup)

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *