将 Button.Enabled 绑定到 C# dll 中的属性

发布于 2024-11-01 00:09:11 字数 1239 浏览 1 评论 0原文

我目前正在编写一个 C# dll,用作希望使用它的 GUI 应用程序(Windows 窗体)的后端引擎。引擎可以处于几种预定义状态之一,例如加载、空闲、工作等,由后台工作人员控制。我希望使用此 dll 的 GUI 应用程序能够执行操作并将控件属性绑定到引擎内的属性,特别是引擎所处的当前状态。例如,我希望使用该引擎的应用程序基于禁用操作和控件发动机是否正在运行。为此,我在引擎中创建了一个只读属性,如下所示:

public Class Engine
{
    // Make the class a singleton
    private static Engine instance;
    public static Engine Instance
    {
        get
        {
            if (instance == null)
                instance = new Engine();
            return instance;
        }
    }

    // ... Class code altering the engines current state

    public bool Property IsActive
    {
        get
        {
            return (EngineState != State.Idle);
        }
    }
}

其中 State 是所有可能状态的枚举器,EngineState 是当前状态。为了测试这个属性,我编写了一个快速的 Windows 窗体应用程序,我希望在引擎处于活动状态时启用一个按钮。为此,我尝试像这样绑定 Enabled 属性:

public Class TestApp
{
    Engine MyEngine = Engine.Instance;

    TestApp
    {
        Button_DoAction.DataBindings.Add("Enabled", MyEngine, "IsActive");
    }

    // ... Button handlers etc.
}

此绑定似乎在应用程序首次加载时起作用,但是当 dll 中的属性 IsActive 发生更改(即引擎从 Active 变为 Idle)时,按钮的 Enabled 属性不会起作用改变。

所以我只是想知道我想做的事情是否可能? Engine 是单例并且是在外部 dll 中编写的这一事实是否意味着有区别?任何反馈都会有很大的帮助。

提前致谢。

I'm currently writing a C# dll to be used as a back end Engine for GUI applications (windows forms) that wish to use it. The Engine can be in one of several pre-defined states, e.g. Loading, Idle, Working etc. which is controlled by a backgroundworker. I want the GUI applications which use this dll to be able to perform actions and bind control properties to properties within the engine, particularly the current state the engine is in. For example, I want applications which use the engine to disable actions and controls based on whether the Engine is running or not. To do this I've created a read only property within the engine as follows:

public Class Engine
{
    // Make the class a singleton
    private static Engine instance;
    public static Engine Instance
    {
        get
        {
            if (instance == null)
                instance = new Engine();
            return instance;
        }
    }

    // ... Class code altering the engines current state

    public bool Property IsActive
    {
        get
        {
            return (EngineState != State.Idle);
        }
    }
}

Where State is an enumerator of all possible states and EngineState is the current state. To test this property out I've written a quick windows form application where I want a button to be Enabled if the engine is Active. To do this I've tried binding the Enabled property like so:

public Class TestApp
{
    Engine MyEngine = Engine.Instance;

    TestApp
    {
        Button_DoAction.DataBindings.Add("Enabled", MyEngine, "IsActive");
    }

    // ... Button handlers etc.
}

This binding seems to work when the application first loads but when the property IsActive changes within the dll, i.e. Engine goes from Active to Idle, the Enabled property of the button does not change.

So I'm just wondering whether what I'm trying to do is possible? Does the fact that Engine is a singleton and is written in an external dll mean make a difference? Any feedback would be a great help.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简美 2024-11-08 00:09:11

您的引擎需要实现 System.ComponentModel.INotifyPropertyChanged

接口有一个事件“ProperyChanged”,当某些内容发生更改时您的引擎必须触发该事件(至少如果您希望对更改做出反应)

请参阅 MSDN http://msdn.microsoft.com/en-gb/library /system.componentmodel.inotifypropertychanged%28v=vs.95%29.aspx

your engine needs to implement System.ComponentModel.INotifyPropertyChanged

the interface has an event "ProperyChanged" which your engine must fire when something has changed (at least if you want something to react on that change)

see MSDN http://msdn.microsoft.com/en-gb/library/system.componentmodel.inotifypropertychanged%28v=vs.95%29.aspx

旧故 2024-11-08 00:09:11

如果您按照文本建议使用 Windows 窗体,则需要引发事件或手动滚动委托回调。

然后,表单可以订阅事件/委托并采取相应的行动。

如果您按照代码建议使用 WPF,请采纳 DarkSquirrel42 的建议并实现 INotifyPropertyChanged界面。

If you are using windows forms as the text suggests you'll need to raise an event or hand roll a delegate call back.

The form can then subscribe to the event/delegate and act accordingly.

If you aree using WPF as the code suggests then take the advice of DarkSquirrel42 and implement the INotifyPropertyChanged interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文