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
版权声明:本文标题:mvvm - how does data bindingsync working in winui3 c++winrt - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303823a1932014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论