【问题标题】:Label text not updating on a form instantiated from a DLL标签文本未在从 DLL 实例化的表单上更新
【发布时间】:2016-10-23 09:24:00
【问题描述】:

我已经从 .dll 加载了一个 Windows 窗体,它显示正确,我可以执行在该 .dll 中创建的方法。

注意 Form1 窗口中的文本标签。在我的主窗口中,我单击调试,然后选择一个应将文本标签更改为另一个字符串的选项。但是字符串拒绝更改。我检查了 Form1 中的方法正在触发,并且 label.text 已更改但显示从未更改。

注意:我测试过的其他控件(文本框/列表框等​​)也会发生这种情况。

public void Command(string cmd, string param1, string param2, string param3)
        {
            if (cmd == "TEST")
            {
                this.label1.Text = "This should now change";
                MessageBox.Show("DONE");
            }
        }

MessageBox 按预期显示,label.text 已更改,并且所有事件都正确触发(我为有效的标签创建了单击事件),但似乎标签实际上并未更新。我也尝试在标签上使用 Refresh。

如果可能的话,还有一个问题,:) 有没有一种特殊的方法可以在我可以提供给 Form1 的主窗体上创建回调?我猜是某种代表?

我以这种方式从 dll 加载

    try
    {
        Type interfaceType = typeof(IPlugin);
        // Fetch all the types that implement the interface IPlugin and are a class
        Type[] types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes())
            .Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
            .ToArray();
        foreach (Type type in types)
        {
            // Create a new instance of all found types
            PluginLists.All.Add((IPlugin)Activator.CreateInstance(type));
            Console.WriteLine("Plugin loaded: {0}", type.ToString());
        }
        Console.WriteLine("Plugins loaded: {0}", PluginLists.All.Count);
    }

而且,每个 dll 都实现了一个启动表单的方法

public void Plugin_Start(DockPanel _dockPanel)
{
    //
    var miscForm = new frmMiscellaneousTest();
    miscForm.Show(_dockPanel, DockState.DockRight);
}

感谢您提供的任何帮助,非常感谢。

【问题讨论】:

  • 调用无效方法
  • @vivek:我已经尝试过了,很抱歉没有在我的 OP 中提及它。它不会改变,:(
  • 您是否尝试在更改值后刷新控件或整个表单。 Form1.Refresh() 或 Label1.Refresh()
  • 是的,我都试过了。我可能缺少一些简单的东西,:/ 我在 Form1 中的方法中的代码现在是:` if (cmd == "TEST") { label1.Text = "This should now change"; bool test = label1.InvokeRequired;标签1.无效(真);标签1.更新();标签1.刷新(); this.Invalidate(true); this.Update(); this.Refresh(); } ` 似乎什么也没发生,:(
  • 你使用简单的 Winforms 还是一些框架?我知道 DevExpress Controls 总是有像 AllowEdit 或 sth 这样的属性。如果这是假的,文本不会改变。

标签: c# winforms dll visual-studio-2015 label


【解决方案1】:

我正在使用下面的代码(现在注释掉)来显示表单,但是我完全忘记了我已经在表单中并且不需要再次实例化它,:(

public void Plugin_Start()
{
    //
    this.Show();
    //var MiscForm = new frmMiscellaneousTest();
    //MiscForm.Show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多