在 WPF/Silverlight 和 GDI 之间共享库时出现问题+由于 System.Windows.Point / System.Drawing.PointF 差异
在我目前正在处理的项目中 http://sourcecodecloud.codeplex.com/ 我有一个包含重要内容的库几何与布局算法。它们完全独立于图形引擎。
该应用程序最初是为 GDI+ 编写的,现在我将实现它的 Silverlight 和/或 WPF 端口。问题是我的所有算法都使用 System.Drawing.PointF、SizeF、RectangleF 结构。它们都是基于float
的。相应的WPF/Silverlight类是double
。
问题是,有人有这方面的经验吗?最好的方法是什么?
- 为“大小”、“点”、“矩形”等创建自己的包装器,可以包装这两种变体。
- 坚持使用 System.Drawing 或 System.Windows 并将其投射到另一个中。负面影响是对“外星人”组件的不必要的引用。
- 还有其他魔法吗?
In the project I am currently working on http://sourcecodecloud.codeplex.com/ I have a library containing nontrivial geometry & layout algorithms. They are completely graphic engine independent.
The app was initially written for GDI+, now I am going to implement a Silverlight and/or WPF port of it. The problem is that all my algorithms use System.Drawing.PointF
, SizeF
, RectangleF
structures. They are all float
based. Corresponding WPF / Silverlight classes are double
.
The question is, have anyone experience on that? What is the best way?
- Create own wrappers for Size, Point, Rectangle etc. which can wrap both variants.
- Stick either with System.Drawing or System.Windows and cast it in another. Negative impact is unneeded reference on the 'alien' assembly.
- Some other magic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种可能适用的方法是使用命名空间别名。您可以使用别名而不是实际类型和编译器指令在它们之间进行切换。由于命名空间别名是每个文件的,如果代码分布在许多文件中,它可能不合适。
如果您使用 var ,您可以消除大量检查、强制转换并保持性能。您仍然需要查看采用和返回特定类型的函数,例如 Math.Sin 等
One method that might be applicable is to use namespace aliases. You use an alias instead of the actual type and a compiler directive to switch between them. As namespace aliases are per file it might not be suitable if the code is spread over lots of files.
If you use
var
you can eliminate a lot of the checks, casts and keep the performance. You'll still need to look at functions that take and return specific types such as Math.Sin e.t.c.我将从 便携式库工具 作为基础。其中的任何类型都适合在您的所有项目中使用。对于其中不存在的任何类型,我将创建您自己的抽象类型,并实现派生类型,这些派生类型分别包装每个框架的相关类型。尽可能使用您通过自己的代码创建的抽象类型。
I would begin with Portable Library Tools as your base. Any types in there are suitable for use across all your projects. For any types not present in there, I would create your own abstract types, and implement derived types which wrap the relevant type from each framework separately. Use the abstract types you create as much as possible from your own code.
我会使用 WPF 类型,并使用一些全局(扩展?)方法来转换为 GDI。它们比 Gdi 更完整、更明智。缺点是它们是双基的,所以它们更大。但凭借我们现在拥有的所有记忆力,你不太可能对此有任何问题。
I'd use the WPF types, and use some global (extension?) method to convert to GDI. They are more complete and more sensible than the Gdi ones. The downside is they are double based, so they are bigger. But with all that memory we have nowadays you are not likely to have a problem with that.
我正在开发一个包含大量几何和图形计算的制图/GIS 应用程序。最初我直接在渲染代码中使用 GDI+ 的类,如
Point
和PointF
,但它逐渐成为一个问题,因为我想支持 Cairo、SVG、Direct2D 和其他图形引擎。所以最后我制作了自己的基于接口的层次结构,例如IPointF2
、IPointD2
、IPointD3
等。它当然并不完美。谈到性能,但我没有看到更好的方法来将算法代码与图形引擎分开。我不久前写过一些关于这个主题的东西。
I'm developing a cartography/GIS application with a lot of geometry and graphics calculations. Initially I was using GDI+'s classes like
Point
andPointF
directly in the rendering code, but it increasingly became a problem because I wanted to support Cairo, SVG, Direct2D and other graphics engines. So in the end I made my own interface-based hierarchy, likeIPointF2
,IPointD2
,IPointD3
etc. It certainly isn't perfect when it comes to performance, but I don't see a better way of keeping your algorithmic code separate from the graphics engine.I wrote something on this subject a while ago.