In WinUI-3 Facing issue (Showing error "System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'")

huangapple go评论74阅读模式
英文:

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.


&lt;ComboBox 
    IsEditable=&quot;True&quot;
    x:Name=&quot;userList&quot;
    ItemsSource=&quot;{x:Bind UserViewModel.Users,Mode=TwoWay}&quot;
    KeyUp=&quot;AddUsers&quot;
    SelectedValue=&quot;{x:Bind SelectedValue,Mode=TwoWay}&quot; /&gt;

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;
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月19日 14:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定