WinForm组合框控件DotNet 4.8-VS2019中的错误

时间:2020-03-15 21:32:45

标签: c# .net winforms combobox

我正在用C#(不是ASP.NET!)编写Winforms应用程序,并且在组合框控件(是否已填充,是否选中,是否选中)上出现错误,如下所示:

托管调试助手“ NonComVisibleBaseClass”
Message =托管调试助手'NonComVisibleBaseClass':'进行QueryInterface调用,以请求COM可见托管类'ComboBoxUiaProvider'的类接口。但是,由于该类派生自非COM可见类'ComboBoxExAccessibleObject',因此QueryInterface调用将失败。这样做是为了防止非COM可见基类受到COM版本控制规则的约束。'

我没有发现任何错误(这是一个非常基本的应用程序,需要进行一些测试)

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;

namespace SpeechDemoApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private SpeechSynthesizer _SS = new SpeechSynthesizer();

        private void button1_Click(object sender, EventArgs e)
        {
            _SS.Speak(txtSpeech.Text.Trim());
        }


        private void button2_Click(object sender, EventArgs e)
        {
            var ivs = _SS.GetInstalledVoices();
            foreach (var iv in ivs)
            {
                ComboboxItem cboItem = new ComboboxItem();
                cboItem.Text = iv.VoiceInfo.Name;
                cboItem.Value = iv.VoiceInfo.Name;
                cboVoices.Items.Add(cboItem);
            }


        }

    }

    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }
}

组合框正确填充,但是,当您单击组合框时,会产生上述错误。我不知道发生了什么事。 请注意,这是一个WinForms应用程序(对不起,我一直要提起这个问题,因为没有人在回答之前会先阅读帖子),而不是WPF或ASP.NET应用程序。

1 个答案:

答案 0 :(得分:1)

在DotNetFramework 4.5、4.6 *,4,7 *中进行了重新编译,并测试了每个子版本,它们都工作正常。
这是一个DotNetFramework 4.8错误,显然,尚未向Microsoft报告/解决。 降级到4.7.2解决了该问题

要重现此问题-这是完整且有效的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpeechDemoApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private SpeechSynthesizer _SS = new SpeechSynthesizer();

        private void btnSpeak_Click(object sender, EventArgs e)
        {
            if (cboVoices.SelectedIndex >= 0)
            {
                _SS.SelectVoice(cboVoices.SelectedItem.ToString());
            }
            _SS.Volume = trackBar1.Value;
            _SS.SpeakAsync(txtPhrase.Text.Trim());
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            var ivs = _SS.GetInstalledVoices();
            foreach (var iv in ivs)
            {
                ComboboxItem cboItem = new ComboboxItem();
                cboItem.Text = iv.VoiceInfo.Name;
                cboItem.Value = iv.VoiceInfo.Name;
                cboVoices.Items.Add(cboItem);
            }

            if (cboVoices.Items.Count > 0)
            {
                cboVoices.SelectedIndex = 0;
            }
        }

        private void cboVoices_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _SS.SpeakAsyncCancelAll();
            base.OnClosing(e);
        }
    }



    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }
}

还有设计师:

namespace SpeechDemoApp
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cboVoices = new System.Windows.Forms.ComboBox();
            this.txtPhrase = new System.Windows.Forms.TextBox();
            this.btnSpeak = new System.Windows.Forms.Button();
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // cboVoices
            // 
            this.cboVoices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboVoices.FormattingEnabled = true;
            this.cboVoices.Location = new System.Drawing.Point(41, 37);
            this.cboVoices.Name = "cboVoices";
            this.cboVoices.Size = new System.Drawing.Size(144, 21);
            this.cboVoices.TabIndex = 0;
            this.cboVoices.SelectedIndexChanged += new System.EventHandler(this.cboVoices_SelectedIndexChanged);
            // 
            // txtPhrase
            // 
            this.txtPhrase.Location = new System.Drawing.Point(41, 82);
            this.txtPhrase.Name = "txtPhrase";
            this.txtPhrase.Size = new System.Drawing.Size(664, 20);
            this.txtPhrase.TabIndex = 1;
            this.txtPhrase.Text = "This is a test of the Emergency Webcast System. If this were an actual emergency," +
    " you would be bleeding from all of your orifices.";
            // 
            // btnSpeak
            // 
            this.btnSpeak.Location = new System.Drawing.Point(41, 127);
            this.btnSpeak.Name = "btnSpeak";
            this.btnSpeak.Size = new System.Drawing.Size(92, 23);
            this.btnSpeak.TabIndex = 2;
            this.btnSpeak.Text = "Speak";
            this.btnSpeak.UseVisualStyleBackColor = true;
            this.btnSpeak.Click += new System.EventHandler(this.btnSpeak_Click);
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(240, 127);
            this.trackBar1.Maximum = 100;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(465, 45);
            this.trackBar1.TabIndex = 3;
            this.trackBar1.Value = 80;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.trackBar1);
            this.Controls.Add(this.btnSpeak);
            this.Controls.Add(this.txtPhrase);
            this.Controls.Add(this.cboVoices);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ComboBox cboVoices;
        private System.Windows.Forms.TextBox txtPhrase;
        private System.Windows.Forms.Button btnSpeak;
        private System.Windows.Forms.TrackBar trackBar1;
    }
}
相关问题