更新调用UI函数DLL

发布于 2024-12-23 16:45:21 字数 274 浏览 0 评论 0原文

我试图弄清楚如何从正在执行某些任务的 DLL 更新我的 UI。

我不明白 DLL 如何知道 UI 的任何信息。

希望能解释得更清楚:

我有任务 1、任务 2 和 UI。
task1 和task2 都是DLL,对UI 不了解。
我如何告诉 DLL 调用 UI 中的某些函数?

希望我足够清楚,我对 C# 的了解还远远没有达到我想要的水平。

提前致谢。

PS 希望不会让事情变得更糟 这两个任务都有自己的 appDomain

I am trying to figure out how I can update my UI from an DLL that is doing some task.

I cant figure out how the DLL can know anything of the UI.

To hopefully explain more clear:

I have let's say task1, task2 and UI.
task1 and task2 are both DLL's and dont know anything of UI.
How can I tell the DLL's to call some function in UI?

Hopefully I am clear enough, my knowledge of C# is far from where I want it to be.

Thanks in advance.

PS to hopefully not make it worse both tasks have their own appDomain's

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

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

发布评论

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

评论(1

层林尽染 2024-12-30 16:45:21

您不会告诉他们更新 UI,您会引发一个事件来指示某些内容已发生更改,这可能很有趣 - 然后您使 UI 在您的实例上订阅该事件,并相应地更新 UI。

因此,假设您希望在类上的任何属性发生更改时引发一个事件(方便的是,有 INotifyPropertyChanged 接口,它甚至描述了您将要执行此操作,以及很多事情会“注意到”并“做正确的事情”,例如 BindingList

因此,您声明您的类具有属性:

public event PropertyChangedEventHandler PropertyChanged;

然后,要引发事件,您只需调用:

if (PropertyChanged != null)
{
    PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
    PropertyChanged(this, e);
}

订阅事件(在您的客户端应用程序/UI 层)就像拥有事件处理程序一样简单:

protected void yourClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     // Update the UI -- might need to check whether you need too invoke, if you're multi-threaded
}

添加接线

YourClass foo = new YourClass();
foo.PropertyChanged += new PropertyChangedEventHandler(this.yourClass_PropertyChanged);

You don't tell them to update the UI, you raise an event that indicates something has changed, that might be interesting - you then make the UI subscribe to that event on your instances, and update the UI accordingly.

So, let's say you want to raise an event whenever any of the properties on a class change (conveniently, there is the INotifyPropertyChanged interface that even describes that you are going to do this, and a lot of things will "notice" and "do the right thing", such as BindingList<T>)

So you declare that your class has properties:

public event PropertyChangedEventHandler PropertyChanged;

Then, to raise the event, you simply call:

if (PropertyChanged != null)
{
    PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
    PropertyChanged(this, e);
}

To subscribe to the events (in your client application/UI layer) it's as simple as having your event hadler:

protected void yourClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     // Update the UI -- might need to check whether you need too invoke, if you're multi-threaded
}

Add wiring up

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