C# UWP VS2017 ComboBox event error: unable to activate Windows store app

C# UWP VS2017 ComboBox event error: unable to activate Windows store app

我尝试为 UWP 应用程序中的组合框创建一个事件处理程序,当我将值更改为某个项目时,表单上的一些控件会被隐藏。问题是,当我选择不调试就开始时,出现错误:无法激活 Windows 商店应用程序。现在我不知道这是由代码还是其他原因引起的。当我从代码中删除事件时,问题就消失了,如果我只从事件处理程序中删除主体,问题仍然存在,所以我相当确定问题不在主体中。

这是 C# + XAML 代码:

private void RoleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (roleComboBox.SelectionBoxItem.ToString() == "Coach")
        {
            positionTextBlock.Visibility = Visibility.Collapsed;
            positionComboBox.Visibility = Visibility.Collapsed;
        }
    }

<ComboBox x:Name="roleComboBox" HorizontalAlignment="Left" Margin="200,84,0,0" VerticalAlignment="Top" Width="140" SelectionChanged="RoleComboBox_SelectionChanged">
        <ComboBoxItem IsSelected="True">-Choose a role-</ComboBoxItem>
        <ComboBoxItem>Player</ComboBoxItem>
        <ComboBoxItem>Coach</ComboBoxItem>
        <ComboBoxItem>Trainer</ComboBoxItem>
    </ComboBox>

我一开始以为问题出在VS2017的某个地方(2019也试过),然后尝试了很多在网上找到的关于这个问题的解决方案。在尝试了 10 多个小时的解决方案后(我从没想过问题出在代码中,因为互联网上的所有问题都将其描述为调试器的问题)我试图评论我编码的最后一部分,因为当时出现了问题并且在那之前不在那里。这解决了我的问题,所以我将错误指出给了事件处理程序。

问题是你把ComboBoxItem放在了ComboBox中,所以选择的item类型是ComboBoxItem,我们需要把它转换成ComboBoxItem然后得到Content 属性喜欢下面的内容。

private void RoleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = roleComboBox.SelectedItem as ComboBoxItem;
    var value = item.Content;
    if ((roleComboBox.SelectedItem as ComboBoxItem).Content.ToString() == "Coach")
    {
        positionTextBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
      
    }
}