如何从 .Net 5 Winforms 应用程序访问 Dispatcher [英] How to access Dispatcher from a .Net 5 Winforms app

查看:24
本文介绍了如何从 .Net 5 Winforms 应用程序访问 Dispatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 .Net 5.0 应用程序中使用 System.Windows.Threading.Dispatcher 类?

根据在线文档,它应该可用.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher?view=net-5.0.

通常,在 .Net Framework 应用程序中,我只会添加对程序集 WindowsBase.dll 的引用.但由于我使用的是 .Net 5.0 项目,因此我只能引用 Nuget 包.
无法将程序集添加到 .NetVisual Studio 2019 中的核心应用.

但是,似乎没有 WindowsBase Nuget 包.
https://www.nuget.org/packages?q=WindowsBase.>

有一个名为 Microsoft.Windows.Compatibility 的包,根据其描述听起来应该包含该类;但它没有.
https://www.nuget.org/包/Microsoft.Windows.Compatibility/6.0.0-preview.5.21301.5

有什么建议吗?

限制:

  • 该项目广泛使用 WinForms.将其转换为 WPF 是不可行的.
  • 我使用 Dispatcher 的原因是,它使不同线程上的对象可以调用对 UI 的修改.创建这些对象时,UI 并不存在,因此无法将控件传递给它们(而且,对于业务对象来说,了解按钮和其他内容的 IMO 也不是很优雅).

解决方案

仔细查看后,我意识到截至今天 (17/Jun/2021) Nuget Package Microsoft.Windows.Compatibility 是仍处于预发布.这意味着当其他人阅读此线程时,该类可能已经包含在内.

如果没有,我找到了一个解决方法:

  1. 创建 WPF 类库
  2. 添加一个 DispatcherWrapper 类
  3. 添加私有 Dispatcher 属性.
  4. 将所有(需要的)Dispatcher 方法添加到 DispatcherWrapper 类
  5. 将 WinForms 应用中的项目引用添加到 WPF 库.
  6. 使用 DispatcherWrapper 类而不是 Dispatcher.

不过,在阅读了 Jimi 的评论后,我认为在处理 Winforms 应用程序时,使用 SynchronizationContext 而不是 Dispatcher 会更优雅.>

How can I use class System.Windows.Threading.Dispatcher from within a .Net 5.0 app?

According to the online documentation, it should be available.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher?view=net-5.0.

Normally, in a .Net Framework app, I would just add a reference to assembly WindowsBase.dll. But since I'm using a .Net 5.0 project I can only reference Nuget Packages.
Cannot add assemblies to .Net Core application in Visual Studio 2019.

However, there does not seem to be a WindowsBase Nuget package.
https://www.nuget.org/packages?q=WindowsBase.

There is a package called Microsoft.Windows.Compatibility which according to its descriptions sounds like it should include the class; but it does not.
https://www.nuget.org/packages/Microsoft.Windows.Compatibility/6.0.0-preview.5.21301.5

Any suggestions?

Restrictions:

  • The project uses WinForms extensively. It's not viable to convert it to WPF.
  • The reason I use Dispatcher, it so that objects on different threads can call modifications to the UI. The UI does not exist when these objects are created, so passing controls to them is not an option (also, not very elegant IMO for business objects to know about buttons and stuff).

解决方案

After looking carefully, I realized that as of today (17/Jun/2021) The Nuget Package Microsoft.Windows.Compatibility is still in pre-release. This means that by the time other people read this thread the class might be already included.

If not, I found a workaround:

  1. Create a WPF class library
  2. Add a DispatcherWrapper class
  3. Add a private Dispatcher attribute.
  4. Add all the (needed) Dispatcher methods to the DispatcherWrapper class
  5. Add a project reference from the WinForms app to the WPF library.
  6. Use the DispatcherWrapper class instead of Dispatcher.

However, after reading the comment from Jimi, I think that, when working on a Winforms application, it is more elegant to use SynchronizationContext instead of Dispatcher.

这篇关于如何从 .Net 5 Winforms 应用程序访问 Dispatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆