来自ComboBox的C ++ XAML选定项

时间:2016-01-31 09:41:41

标签: xaml combobox c++-cx

我目前正在尝试为自己创建一个简单的C ++ Windows Phone 8.1应用程序,并且我遇到了这个简单的问题。我无法找到C ++类似代码的任何示例,仅适用于C#。

我的代码基于一个简单的"Hello world"程序。在Mainpage.xaml中,我介绍了以下ComboBox

<ComboBox x:Name="AttackDice" HorizontalAlignment="Left" Height="70"   
              Margin="37,70,0,0" VerticalAlignment="Top"   
              Width="320">
        <ComboBoxItem Content="One attack dice"/>
        <ComboBoxItem Content="Two attack dice" IsSelected="True"/>
        <ComboBoxItem Content="Three attack dice" />
        <ComboBoxItem Content="Four attack dice"/>
        <ComboBoxItem Content="Five attack dice"/>
        <ComboBoxItem Content="Six attack dice"/>
</ComboBox>

我还有一个简单的按钮,它在我的Mainpage.xaml.cpp中触发了以下事件

void HelloWorld::MainPage::RollDice_Button_Click(Platform::Object^ sender,
    Windows::UI::Xaml::RoutedEventArgs^ e)
{
    AttackResultTextBlock->Text = "Number of dice rolled: "
 + AttackDice->SelectedItem->ToString();
}

代码应该检索选定的ComboBoxItem,并输出所选数量的(攻击)骰子。但是,输出是:

  

Windows.UI.Xaml.Controls.ComboBoxItem

在C#中,正确的方法是(根据我在网上找到的一些指南)

Text = ((ComboBoxItem)AttackDice.SelectedItem).Content.ToString();

但我不确定如何在C ++中这样做。

1 个答案:

答案 0 :(得分:1)

以下C#代码

Text = ((ComboBoxItem)AttackDice.SelectedItem).Content.ToString();

可以转换为C ++ / CX。 C#中的强制转换(参见Casting and Type Conversions)在C ++ / CX中的safe_cast是<{3}}:

InvalidCastException