首先我们演示Popub
再Xaml中,添加一个按钮,点击按钮打开这个Popub,然后同时添加一个Popub,默认它是关闭的,点击按钮后,它才显示
<StackPanel Spacing="24" VerticalAlignment="Top">
<!-- Raw Popup Root Container -->
<Popup x:Name="MainPopup" Width="320" Height="180">
<Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="8" Padding="16">
<StackPanel Spacing="12">
<TextBlock Text="Custom Popup Content" FontSize="18"/>
<TextBlock Text="This is raw Popup base container"/>
<Button Content="Close Popup" Click="BtnClosePopup_Click"/>
</StackPanel>
</Border>
</Popup>
<Button Content="Open Raw Popup Container" Click="BtnOpenPopup_Click" HorizontalAlignment="Left"
ToolTipService.ToolTip="Open base raw popup layer"/>
</StackPanel>Code language: HTML, XML (xml)
然后创建 Click=”BtnOpenPopup_Click” 这个按钮点击事件,里面是显示Popub,然后<Popup x:Name=”MainPopup” Width=”320″ Height=”180″> …</Popup> 是创建一个冒泡窗体,默认是关闭的,里面有一个按钮<Button Content=”Close Popup” Click=”BtnClosePopup_Click”/> 这个按钮点击就把按钮所在的Popup 关闭了
按住F12,创建两个按钮事件
.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>
{
};
}
Code language: 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
// 全局命名空间别名,消除::类名找不到、RoutedEventArgs未定义
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);
}
Code language: C++ (cpp)
Previous: WinUI 3 C++ 控件篇-导航和选项卡