Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

c++-winrt - 如何在cppwinrt中为按钮绑定单击处理程序(how to bind a click handler for a Button in cppwinrt)

according to this page https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/binding-property

(根据此页面https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/binding-property)
I'm writing the following code

(我正在编写以下代码)

/*cl /nologo /Yupch.h /bigobj /await /std:c++latest /EHsc /I. demo.cpp pch.obj /link /winmd /out:appxApp.exe /subsystem:console /appcontainer*/
struct MainPage : PageT<MainPage>
{
  MainPage()
  {
    Application::LoadComponent(*this, Uri(L"ms-appx:///MainPage.xaml"));
  }

  void ClickHandler(Windows::Foundation::IInspectable const& /* sender */, Windows::UI::Xaml::RoutedEventArgs const& /* args */)
  {
    cout << __func__ << endl;
  }
};

and the MainPage.xaml page is as follow

(和MainPage.xaml页面如下)

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button Click="ClickHandler">click</Button>
</Page>

but it seems that the MainPage::ClickHandler never got called , so , what is the right way ?

(但是似乎从未调用过MainPage :: ClickHandler,那么正确的方法是什么?)

many thanks!

(非常感谢!)

  ask by oldoldman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your MainPage.xaml is missing an x:Class attribute on the root Page declaration:

(您的MainPage.xaml在根Page声明上缺少x:Class属性 :)

<Page x:Class="MyNamespace.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button Click="ClickHandler">click</Button>
</Page>

Without that attribute, the XAML platform doesn't know, where to look for the ClickHandler to bind to the Click event.

(没有该属性,XAML平台将不知道在何处查找ClickHandler绑定到Click事件。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...