admin管理员组

文章数量:1122832

I wish to use mvvm instead of hardcoding all the change events in MainWindow. Tried lot of example deom different sources but not working with c++/winrt winui-3....

Main Window

File Mainwindow.xaml

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="MyBindingExample.MainWindow"
    xmlns=";
    xmlns:x=";
    xmlns:local="using:MyBindingExample"
    xmlns:d=";
    xmlns:mc=";
    mc:Ignorable="d"
    Title="MyBindingExample">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBox x:Name="OrderNo_TextBox" Text="{Binding OrderNo, Mode=TwoWay}" Width="100" Height="30" Margin="10"/>
    </StackPanel>
</Window>

File: MainWindow.idl

namespace MyBindingExample
{
    [default_interface]
    runtimeclass MainWindow : Microsoft.UI.Xaml.Window
    {
        MainWindow();
    }
}

File MainWindow.xaml.h

#pragma once

#include "MainWindow.g.h"

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

        MyBindingExample::DocumentViewModel DocViewModel() const;

    private:
        MyBindingExample::DocumentViewModel m_viewModel{ nullptr };
    };
}

namespace winrt::MyBindingExample::factory_implementation
{
    struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
    {
    };
}

File: MainWindow.xaml.cpp

#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;

namespace winrt::MyBindingExample::implementation
{

    MainWindow::MainWindow()
    {
        InitializeComponent();
        m_viewModel = MyBindingExample::DocumentViewModel();
        m_viewModel.OrderNo(L"12345");
    }
    MyBindingExample::DocumentViewModel MainWindow::DocViewModel() const
    {
        return MyBindingExample::DocumentViewModel();
    }
}

View Model

File: DocumentViewModel.idl

namespace MyBindingExample
{
    [bindable]
    [default_interface]
    runtimeclass DocumentViewModel
    {
        DocumentViewModel();
        String OrderNo;
    }
}

File: DocumentViewModel.h

#pragma once

#include "DocumentViewModel.g.h"
#include "winrt/base.h"

namespace winrt::MyBindingExample::implementation
{
    struct DocumentViewModel : DocumentViewModelT<DocumentViewModel>
    {
        DocumentViewModel() = default;

        hstring OrderNo();
        void OrderNo(hstring value);

    private:
        hstring m_orderNo;
    };
}

namespace winrt::MyBindingExample::factory_implementation
{
    struct DocumentViewModel : DocumentViewModelT<DocumentViewModel, implementation::DocumentViewModel>
    {
    };
}

File: DocumentViewModel.cpp

#include "pch.h"

#include "DocumentViewModel.h"
#if __has_include("DocumentViewModel.g.cpp")
#include "DocumentViewModel.g.cpp"
#endif
#include "winrt/base.h"

namespace winrt::MyBindingExample::implementation
{
    hstring DocumentViewModel::OrderNo()
    {
        return m_orderNo;
    }

    void DocumentViewModel::OrderNo(hstring orderNo)
    {
        m_orderNo = orderNo;
    }
}

本文标签: mvvmhow does data bindingsync working in winui3 cwinrtStack Overflow