如何获取 XAML 按钮的内容?

How to get the Content of a XAML Button?

如何获取 XAML 按钮的内容?

Visual Studio 社区 15.9.4,安装了 C++/WinRT 扩展。

我可以从点击处理程序设置按钮的内容,但无法获取当前内容。

来自 MainPage.xaml 的按钮定义:

<Button x:Name="myButton" Click="ClickHandler">Click Me</Button>

来自 MainPage.cpp 的点击处理程序定义:

void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
  myButton().Content(box_value(L"Clicked"));
}

我还发现此代码可用于设置内容:

void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
  Button sendButton = winrt::unbox_value<Button>(sender);

  sendButton.Content(box_value(L"Clicked"));
}

我尝试获取内容的代码无法编译。

在发布原始问题时,触发了一个想法,我尝试过并且有效。 MainPage.cpp 点击处理程序中添加了以下内容:

IInspectable sendButtonContent = sendButton.Content();
hstring sendButtonString = unbox_value<hstring>(sendButtonContent);

代码暂停时,点击Button后,sendButtonString的值为"Clicked"。