英文:
In WinUI-3 Facing issue (Showing error "System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'")
问题
我正在开发 WinUI3 项目。当我尝试在 ComboBox 中添加项目时,遇到了以下问题。
我有一个 ComboBox,它是可编辑的。当我在该 ComboBox 的输入面板中输入值时,我希望在 ComboBox 的项目列表中显示,但是遇到了一些问题。
这是我的 XAML 文件的代码:
<ComboBox
IsEditable="True"
x:Name="userList"
ItemsSource="{x:Bind UserViewModel.Users,Mode=TwoWay}"
KeyUp="AddUsers"
SelectedValue="{x:Bind SelectedValue,Mode=TwoWay}" />
这是 XAML 代码后台的文件(xaml.cs):
private void AddUsers(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
try
{
if (e.Key == VirtualKey.Enter)
{
ComboBox cmb = (ComboBox)sender;
string enteredValue = cmb.Text;
if (!string.IsNullOrEmpty(enteredValue))
{
userList.Items.Add(enteredValue); // 在这一行发生异常
userList.Text = string.Empty;
}
}
}
我正在这样做,但是我遇到了一个错误,错误消息如下:
"System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'"
[这是错误消息] https://i.stack.imgur.com/HUeLG.png
英文:
I am working on winUi3 project. When I am trying to add item in ComboBox That time facing this issue.
I have a ComboBox and it's editable, when I am entering value in that ComboBox input panel I want to display on List of ComboBox but facing some problem there.
This is code of my xaml file.
<ComboBox
IsEditable="True"
x:Name="userList"
ItemsSource="{x:Bind UserViewModel.Users,Mode=TwoWay}"
KeyUp="AddUsers"
SelectedValue="{x:Bind SelectedValue,Mode=TwoWay}" />
This is Code Behind File xaml.cs
private void AddUsers(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
try
{
if (e.Key == VirtualKey.Enter)
{
ComboBox cmb = (ComboBox)sender;
string enteredValue = cmb.Text;
if (!string.IsNullOrEmpty(enteredValue))
{
userList.Items.Add(enteredValue); // In this Line exception occurring
userList.Text = string.Empty;
}
}
}
I am doing Like this but I am facing issue that time a error is occurring that is,
"System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'"
[This is error] https://i.stack.imgur.com/HUeLG.png
答案1
得分: 1
你需要将项目添加到绑定源:
private void AddUsers(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
ComboBox cmb = (ComboBox)sender;
string enteredValue = cmb.Text;
if (!string.IsNullOrEmpty(enteredValue))
{
UserViewModel.Users.Add(enteredValue);
userList.Text = string.Empty;
}
}
}
英文:
You need to add items to the binding source:
private void AddUsers(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
ComboBox cmb = (ComboBox)sender;
string enteredValue = cmb.Text;
if (!string.IsNullOrEmpty(enteredValue))
{
UserViewModel.Users.Add(enteredValue);
userList.Text = string.Empty;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论