GDI+ 和 WPF 之间的 C# 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/271686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:01:04  来源:igfitidea点击:

C# transition between GDI+ and WPF

c#wpfgdi+

提问by TK.

I'm consideringmigrating my c# application from using custom GDI+ drawn controls to a WPF application with custom controls etc. I would like to know what's involved and what to expect.

我正在考虑将我的 c# 应用程序从使用自定义 GDI+ 绘制控件迁移到具有自定义控件等的 WPF 应用程序。我想知道所涉及的内容以及预期的内容。

Are there any resources people can recommend that might help? Or indeed any personal experiences that might be beneficial?

人们可以推荐任何可能有帮助的资源吗?或者确实有任何可能有益的个人经历?

采纳答案by cplotts

(I apologize in advance for the long post ... there was just so much I wanted to convey ... I hope it helps you.)

(我提前为长篇道歉......我想传达的东西太多了......我希望它对你有所帮助。)

This is what we are doing now (migrating a Windows Forms application with heavy use of custom (GDI+) drawn controls to WPF). In fact, my role on the team was to build these GDI+ controls ... and now to build the WPF ones.

这就是我们现在正在做的事情(将大量使用自定义 (GDI+) 绘制控件的 Windows 窗体应用程序迁移到 WPF)。事实上,我在团队中的角色是构建这些 GDI+ 控件……现在构建 WPF 控件。

I agree with Bijington that making your application completely 100% WPF from the ground up is the way to go ... if you can convince the powers that be to go that route. However, we ourselves are converting our Windows Forms application in-place, taking advantage of the WPF interopcapabilities. There are some limitations, but overall it has been an effective approach (and not as frustrating as I would have expected).

我同意 Bijington 的观点,即从头开始使您的应用程序完全 100% WPF 是要走的路……如果您能说服那些走这条路的权力。但是,我们自己正在就地转换我们的 Windows 窗体应用程序,利用WPF 互操作功能。有一些限制,但总的来说它是一种有效的方法(并不像我预期的那样令人沮丧)。

What I would suggest is that you take one of your GDI+ controls and build the same control in WPF.And then, when you are finished, throw it away and do it again. You will invariably learn something during the first effort ... and discover that there is a better way to do it instead. I would start with something small ... a custom button is a good place to begin.

我建议您使用 GDI+ 控件之一并在 WPF 中构建相同的控件。然后,当你完成时,把它扔掉,然后再做一次。你总是会在第一次努力中学到一些东西……然后发现有更好的方法来代替。我会从一些小东西开始……自定义按钮是一个很好的起点。

Doing the above will give you a taste for what is going to be required for everything else you want to do.

执行上述操作将使您了解您想做的所有其他事情所需的内容。

One thing I would warn you about is WPF's learning curve, especially if you are coming from a Windows Forms background ... and especially if you are going to be building custom looking controls. As Abe has mentioned, it is a completely different world. WPF definitely brings a lot of power, but that power comes at a cost of learning how to use it. Abe mentions how custom controls in WPF are 'lookless' and that their 'look' can be provided with a ControlTemplate. This is just one of many ways in WPF to provide custom looking pieces of your user interface.

我要警告你的一件事是 WPF 的学习曲线,特别是如果你来自 Windows 窗体背景......尤其是如果你要构建自定义外观控件。正如安倍所说,这是一个完全不同的世界。WPF 肯定会带来很多功能,但这种功能是以学习如何使用它为代价的。Abe 提到 WPF 中的自定义控件是如何“无外观”的,并且它们的“外观”可以通过 ControlTemplate 提供。这只是 WPF 中提供自定义外观的用户界面部分的众多方法之一。

Let me enumerate some of those additional ways:

让我列举一些额外的方法:

  1. Style an existing control using the styling capabilities of WPF.
  2. Take advantage of WPF's content model and/or controls derived from ContentControl. This allow you to stick arbitrary looking 'content' into visuals of a control (e.g. maybe sticking a custom drawn shape into the middle of a button).
  3. Compose a control out of other controls/elements by taking advantage of UserControl.
  4. Derive from an existing control/class in WPF, extending it's behavior and providing a different default set of visuals.
  5. Derive from FrameworkElement, creating a custom WPF element, by overriding some or all of the MeasureOverride, ArrangeOverride, and OnRender methods.
  6. And more .... if you can believe it.
  1. 使用 WPF 的样式功能设置现有控件的样式。
  2. 利用 WPF 的内容模型和/或从 ContentControl 派生的控件。这允许您将任意外观的“内容”粘贴到控件的视觉效果中(例如,可能将自定义绘制的形状粘贴到按钮的中间)。
  3. 利用 UserControl 将其他控件/元素组合成一个控件。
  4. 从 WPF 中的现有控件/类派生,扩展其行为并提供一组不同的默认视觉效果。
  5. 通过覆盖部分或全部 MeasureOverride、ArrangeOverride 和 OnRender 方法,从 FrameworkElement 派生,创建自定义 WPF 元素。
  6. 还有更多......如果你能相信的话。

In Windows Forms, it was like they gave you a hammer (UserControl) and a screwdriver (Control). However, in WPF ... they have given you the whole toolbox with all 100 tools. And this is part of the reason for the bigger than normal learning curve. However, now you can take that saw that you never had before and use it to saw off the end of a 2x4 instead of using the hammer and/or screwdriver to try and do the same thing.

在 Windows 窗体中,就像他们给了你一把锤子 (UserControl) 和一把螺丝刀 (Control)。但是,在 WPF 中……他们为您提供了包含所有 100 个工具的整个工具箱。这也是学习曲线大于正常的部分原因。但是,现在您可以拿起以前从未有过的锯子,用它来锯掉 2x4 的末端,而不是使用锤子和/或螺丝刀来尝试做同样的事情。

Resources

资源

(The good news is that there are a lot out of resources out there to help you.)

(好消息是有很多资源可以帮助您。)

  1. Books
    • Programming WPF by Chris Sells & Ian Griffiths (in particular, chapter 18)
    • Pro WPF by Matthew MacDonald (in particular, chapter 24)
    • WPF Unleashed by Adam Nathan (in particular, chapter 16)
    • Applications = Code + Markup by Charles Petzold (in particular, chapters 10, 11, & 12)
    • Essential WPF by Chris Anderson (in particular, chapter 3)

      My favorite books are Charles Petzold's book and Adam Nathan's book. However, chapter 18 of Programming WPF by Sells & Griffiths is really great overview of the subject, and in particular coverage of the question: Do I really need a custom control?
  2. Forums
    • The WPF Forum
    • StackOverflow
      Here are two posts in particular that you will want to take a look at (one, two).
  3. MSDN
    I agree with Bijington that the MSDN documentationexcellent.

  4. Blogs
    In oneof the two StackOverflow posts that I reference in the Forums section above, I point to a set of blogs on my 'must read' list. In particular, I would especially point to the blogs of Pavan Podilaand Kevin Moore. Kevin Moore used to be the WPF program manger for the WPF controls and he has a nice set of controls called the WPF Bag-o-Tricksthat are useful, but more importantly, controls that you can learn from.

  5. Samples, Samples, and more Samples
    There are just a ton of samples out there. Almost too many! I would point to Family.Show(it was created as an end-to-end reference sample for WPF) and I would point to the WPF SDK samplesand in particular to the Control Customization sampleswithin that section.

  1. 图书
    • Chris Sells 和 Ian Griffiths 编写的 WPF 编程(特别是第 18 章)
    • Matthew MacDonald 的 Pro WPF(特别是第 24 章)
    • WPF 由 Adam Nathan 释放(特别是第 16 章)
    • Applications = Code + Markup by Charles Petzold(特别是第 10、11 和 12 章)
    • Essential WPF by Chris Anderson(特别是第 3 章)

      我最喜欢的书是 Charles Petzold 的书和 Adam Nathan 的书。然而,Sells & Griffiths 的 Programming WPF 第 18 章对这个主题进行了很好的概述,特别是对这个问题的覆盖:我真的需要自定义控件吗?
  2. 论坛
    • WPF论坛
    • StackOverflow
      这里有两篇您特别想看的帖子()。
  3. MSDN
    我同意 Bijington 的观点,认为MSDN 文档非常好。

  4. 博客
    一个我在上面的论坛部分引用两个StackOverflow的帖子,我点了一组我的“必读”名单上的博客。特别是,我要特别指出Pavan PodilaKevin Moore的博客。Kevin Moore 曾经是 WPF 控件的 WPF 程序经理,他有一组很好的控件,称为WPF Bag-o-Tricks,它们很有用,但更重要的是,您可以从中学习控件。

  5. 样本、样本和更多样本
    那里只有大量样本。几乎太多了!我会指向Family.Show(它是作为 WPF 的端到端参考示例创建的),我会指向WPF SDK 示例,特别是该部分中的控件自定义示例

回答by Bijington

This is actually a project i am working on at the moment although I have been lucky enough to be able to start again. If you can do this I would strongly recommend it, obviously keeping the core functionality of the old system/controls but start afresh. It is always nice to start from a base and to know where things went wrong/right previously.

这实际上是我目前正在进行的一个项目,尽管我很幸运能够重新开始。如果你能做到这一点,我强烈推荐它,显然保留旧系统/控件的核心功能,但重新开始。从基础开始并知道以前哪里出错/正确总是很好的。

Personally i have found the msdn websiteextremely useful, if not stackoverflow is a very good place for knowledge. I would be happy to give pointers if you need any.

我个人发现msdn 网站非常有用,如果不是的话,stackoverflow 是一个非常好的知识场所。如果你需要的话,我很乐意给你指点。

Also if you ever have any questions about databinding this is the place to go: bea costashe has covered most stuff there.

此外,如果您对数据绑定有任何疑问,那么这就是您要去的地方: bea costa她已经涵盖了那里的大部分内容。

On an extra note we have experienced a huge improvement in performance with using wpf over GDI.

额外注意的是,我们通过使用 wpf 而不是 GDI 体验到了性能的巨大提升。

回答by Abe Heidebrecht

There is a paradigm shift in how you develop controls for WPF. Instead of defining all the behavior and look for a control, you only define the intended behavior.

为 WPF 开发控件的方式发生了范式转变。您只需定义预期的行为,而不是定义所有行为并寻找控件。

This is the hardest aspect of migrating to WPF. Your control class defines a contract of behavior, and exposes properties that will be used to render, and a ControlTemplate is used to define how the control looks.

这是迁移到 WPF 最困难的方面。您的控件类定义了行为契约,并公开了将用于呈现的属性,并且 ControlTemplate 用于定义控件的外观。

This is also one of the most powerful features of WPF; at any point in the future, a consumer of your control can change how it looks without changing how it behaves. This allows for much easier theming of your controls.

这也是WPF最强大的功能之一;在未来的任何时候,您控件的使用者都可以改变它的外观而不改变它的行为方式。这样可以更轻松地对控件进行主题化。