uwp - 组合框的默认选定索引使我的 UWP 应用程序崩溃

标签 uwp winrt-xaml uwp-xaml c++-cx

为什么默认选择的索引不起作用?它因平台异常而崩溃:

<ComboBox x:Name="dCmbControl" x:Uid="dCmbControl" SelectionChanged="ComboBox_SelectionChanged" SelectedIndex="0" ItemsSource="{x:Bind abc}"/>

RumTime 错误:

Exception thrown at 0x00007FFDEF7F7788 (KernelBase.dll) in abc.exe: 0x40080201: WinRT originate error (parameters: 0x00000000802B000A, 0x000000000000003E, 0x00000066746FBB90).
Exception thrown at 0x00007FFDEF7F7788 in abc.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x00000066746FC0E0. HRESULT:0x802B000A The text associated with this error code could not be found.

我做错了什么?

最佳答案

SelectedIndex 设置为 0 时,ComboBox 尝试访问其第一个子项以选择它。加载 XAML 时,将构建控件并将 SelectedIndex 初始化为 0。这将更新相关的 SelectedItem 属性,但由于此时仍然没有可用的项目, 引发异常。

不幸的是,引发的异常不包含任何相关细节(在 C# 中也是如此)。

您可以在 XAML 中将该值设置为 -1,并在加载组合框后将其更新为您想要的值(通过隐藏代码或通过绑定(bind))。

关于uwp - 组合框的默认选定索引使我的 UWP 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177687/

相关文章:

c# - 在 UserControl 中捕获 Esc 键

uwp - 如何像 Windows Build 一样创建 UWP 安装程序

c# - 如何将 Base64 字符串转换为图像,然后将其绑定(bind)到 Metro Style App 中的 GridView?

wpf - 从 Universal App 共享项目中引用图像资源

c# - 我得到 'A project cannot have more than one ApplicationXaml item'

c++ - 在窗口混合现实门户上运行我的解决方案时出现内核调试器错误

c# - UWP:从HID捕获卡读取输入字节

c++ - 错误 C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : cannot convert argument 1 from 'const winrt::guid' to 'const IID &'

c# - 如何使用 XAML/C# 在我的 UWP 应用程序中允许双向数据绑定(bind)?

c# - UWP:在 XAML 中创建阴影