<?xml version="1.0" encoding="utf-8"?> <Window x:Class="WinuiApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinuiApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="WinuiApp"> <Grid> <TextBlock Name="tbTxt" Text="{x:Bind MyProperty}"/> <Button Name="btnClick" Content="点我" Click="btnClick_Click"/> </Grid> </Window>
#pragma once #include "MainWindow.g.h" namespace winrt::WinuiApp::implementation { struct MainWindow : MainWindowT<MainWindow> { private: int32_t myPropertyValue = 32; public : MainWindow() { // Xaml objects should not call InitializeComponent during construction. // See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent } int32_t MyProperty(); void MyProperty(int32_t value); void btnClick_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); }; } namespace winrt::WinuiApp::factory_implementation { struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow> { }; }
#include "pch.h" #include "MainWindow.xaml.h" #if __has_include("MainWindow.g.cpp") #include "MainWindow.g.cpp" #endif using namespace winrt; using namespace Microsoft::UI::Xaml; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. namespace winrt::WinuiApp::implementation { int32_t MainWindow::MyProperty() { return myPropertyValue; } void MainWindow::MyProperty(int32_t value) { myPropertyValue = value; this->Bindings->Update(); OutputDebugString(L"属性值更新到:"); OutputDebugString(winrt::to_hstring(myPropertyValue).c_str()); } } void winrt::WinuiApp::implementation::MainWindow::btnClick_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { MyProperty(myPropertyValue+1); }

浙公网安备 33010602011771号