微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# WinForms - 无法在 TextBox 中键入某些键CorelDraw AddOn 环境问题代码

如何解决C# WinForms - 无法在 TextBox 中键入某些键CorelDraw AddOn 环境问题代码

我在用 C# 中的 TextBox 输入解决一个奇怪的行为方面没有取得成功。

环境

该项目是使用 Control AddOn 创建的,这种项目在 CorelDraw 菜单栏中创建一个像这样的按钮

Buttons in right side

单击此按钮,我可以打开一个新窗口来操作和控制 CorelDraw 功能自动执行任务等。

https://marketplace.visualstudio.com/items?itemName=bonus630.CorelDrawDockerTemplate

问题

当我单击按钮并打开一个包含 TextBox 的新窗口时,使用方法 .Show() 奇怪的是 TextBox 无法正常工作,只能插入数字和特定字母,我什至无法删除按退格。

但是如果我使用 .ShowDialog() 方法打开窗口,一切正常,我可以在 TextBox 中输入任何内容!但是我无法阻止用户与放置在后面的窗口(CorelDraw 窗口)的交互(ShowDialog 这样做),用户需要访问 CorelDraw 窗口而无需在需要时关闭 AddOn。

谁能告诉我我错过了什么?或者解释为什么会发生这种情况?我非常感谢您的帮助。

代码

ControlUI.xaml

<!-- Basic structure to create a button in CorelDraw menu bar (came with the extension) -->
<UserControl x:Class="MaisUmaTentativa.ControlUI"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MaisUmaTentativa"
             mc:Ignorable="d" 
             Height="24" Width="24" Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/Colors.xaml"/>
                <ResourceDictionary Source="Styles/Styles.xaml"/>
                <ResourceDictionary Source="Resources/Images.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <!--button in menu bar goes here-->
        <Button Content="A" Name="btn_Command" >
            <Button.ToolTip>
                <ToolTip>
                    <StackPanel>
                        <Label Content="Enter Caption" FontWeight="Bold" FontSize="12"/>
                        <Label Content="Click ME!" FontSize="11"/>
                    </StackPanel>
                </ToolTip>
            </Button.ToolTip>
        </Button>
    </Grid>
</UserControl>

ControlUI.xaml.cs (Code Behind)

using ...; // removed here to reduce code
using corel = Corel.Interop.Vgcore;

namespace MaisUmaTentativa
{
    public partial class ControlUI : UserControl
    {
        private corel.Application corelApp;
        private Styles.StylesController stylesController;

        public ControlUI(object app)
        {
            InitializeComponent();
            try
            {
                this.corelApp = app as corel.Application;
                stylesController = new Styles.StylesController(this.Resources,this.corelApp);
            }
            catch
            {
                global::System.Windows.MessageBox.Show("Vgcore Erro");
            }

            // handle click in button placed at menu bar
            // MainWindow is a WinForm with a TextBox and a Label
            btn_Command.Click += (s,e) => {
                MainWindow mainWindow = new MainWindow();
                mainWindow.ShowDialog();
            };
        }

        private void UserControl_Loaded(object sender,RoutedEventArgs e)
        {
            stylesController.LoadThemeFromPreference();
        }

    }
}

解决方法

正如@Jimi 所说,问题确实是消息循环没有在我的表单 MainWindow 中启动。

我只是将 Application.Run(this); 放在 MainWindow 窗体的构造函数中,TextBox 错误消失了。谢谢你的帮助?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。