C# 从 Windows 窗体转换到 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15681352/
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-10 17:34:00  来源:igfitidea点击:

Transitioning from Windows Forms to WPF

c#wpfwindowswinformswindows-forms-designer

提问by Matthew Layton

For a long time now, I have been stuck with Windows Forms development (started with VB6, and has continued through to C# .NET 4.5), and I have pretty much hit the limit of what Windows Forms can do, both using pure .NET, and special effects with Native Code.

很长一段时间以来,我一直坚持 Windows 窗体开发(从 VB6 开始,一直持续到 C# .NET 4.5),而且我几乎达到了 Windows 窗体可以做的极限,两者都使用纯 .NET ,以及带有原生代码的特殊效果。

I have tried to learn WPF and XAML, but I get stuck right at WPF's new designer. It really seems very difficult to use in comparison to the Windows Forms designer.

我曾尝试学习 WPF 和 XAML,但我被困在 WPF 的新设计器中。与 Windows 窗体设计器相比,它似乎真的很难使用。

I want to know if there are any alternatives to .NET's WPF designer, that are more suited to Windows Forms developers?

我想知道是否有任何替代 .NET 的 WPF 设计器,更适合 Windows 窗体开发人员?

采纳答案by Rachel

I like to blog about beginner articles for WPF, and there are a few in particular that may help you out:

我喜欢写关于 WPF 初学者文章的博客,特别是有一些可以帮助你:

To summarize, the biggest difference between Winforms and WPF is that in WPF your data layer (the DataContext) is your application, while in Winforms your UI layer is your application.

总而言之,Winforms 和 WPF 之间的最大区别在于,在 WPF 中,您的数据层 (the DataContext) 是您的应用程序,而在 Winforms 中,您的 UI 层是您的应用程序。

To look at it another way, with WPF your application consists of the objects you create, and you use Templates and other UI objects to tell WPF how to draw your application components.

换个角度来看,使用 WPF,您的应用程序由您创建的对象组成,您可以使用模板和其他 UI 对象来告诉 WPF 如何绘制应用程序组件。

That's the opposite of WinForms where you build your application out of UI objects, and then supply them with the data needed.

这与 WinForms 正好相反,在 WinForms 中,您使用 UI 对象构建应用程序,然后为它们提供所需的数据。

Because of this, the designer isn't actually used that much since your application components are designed in code, and the designer is only needed to draw a user-friendly interface that reflects your data classes (typically Modelsand ViewModels)

因此,由于您的应用程序组件是在代码中设计的,设计器实际上并没有被使用那么多,并且设计器只需要绘制一个反映您的数据类(通常是ModelsViewModels)的用户友好界面

And personally, I prefer to type all my XAML out by hand since it's faster and doesn't make as much of a mess as the drag/drop WPF designer does, although I do use the Designer on occasion to preview what my UI will look like.

就我个人而言,我更喜欢手动输入我所有的 XAML,因为它更快,并且不会像拖放 WPF 设计器那样弄得一团糟,尽管我有时会使用设计器来预览我的 UI 外观喜欢。

So to your answer your question about if there's other WPF designers suited for WinForms developers, I would suggest that instead of looking for another designer, instead look to learn how to use WPF in the way it's meant to be used. Using WPF like it's WinForms means you miss out on much of what makes it so great :)

因此,对于您关于是否有其他适合 WinForms 开发人员的 WPF 设计器的问题的回答,我建议您不要寻找其他设计器,而是要学习如何以预期的方式使用 WPF。像使用 WinForms 一样使用 WPF 意味着您会错过很多使它如此出色的东西:)

回答by mlemay

First of all, in WPF (XAML) in Visual Studio deisgner, you should always use the xaml code to build you UI and do not drag and drop you control! You need to keep your code clean. You can use Expression Blend to help you, it's more graphic oriented with drag and drop, but it's not free.

首先,在 Visual Studio deisgner 的 WPF (XAML) 中,您应该始终使用 xaml 代码来构建您的 UI,不要拖放您的控件!你需要保持你的代码干净。您可以使用 Expression Blend 来帮助您,它通过拖放更加面向图形,但它不是免费的。

It's not a big learning curve, but I think you should learn how to do your xaml by hand instead of searching for alternative.

这不是一个很大的学习曲线,但我认为您应该学习如何手动完成您的 xaml,而不是寻找替代方案。

回答by DHN

Well although, some people don't agree, I would also recomment to not use the VS designer. At least not to create an interface. If you may want to get a first impression of your implementation without starting the application, it's a good viewer at least as long no sophisticated things like Stylesand Templatesare used. But, IMHO, its drag and drop resultshould only be used as prototype and therefore be discarded after it's no longer needed.

好吧,虽然有些人不同意,但我也建议不要使用 VS 设计器。至少不要创建接口。如果您可能想在不启动应用程序的情况下获得对您的实现的第一印象,那么它是一个很好的查看器,至少只要不使用Styles和 之Templates类的复杂东西。但是,恕我直言,它的拖放结果只能用作原型,因此在不再需要后将其丢弃。

Here are some reasons which are important for me not to use it.

以下是一些对我来说不使用它很重要的原因。

  1. The VS designer is working with fix margins and alignments (which is usually not necessary, if you're using the layout controls), means you have to touch many controls, if the requirements are changed. If you're deep in XAML and the WPF mechanics you can create an applications which can be modified with small effort, regarding the look and feel.

  2. Since the designer is generating the xaml, the composition is not optimal and the UI may perform badly. I didn't measure it, it's just a feeling.

  1. VS 设计器正在使用固定边距和对齐方式(如果您使用布局控件,这通常不是必需的),这意味着如果需求发生变化,您必须触摸许多控件。如果您深入了解 XAML 和 WPF 机制,您可以创建一个可以轻松修改外观和感觉的应用程序。

  2. 由于设计人员正在生成 xaml,因此组合不是最佳的,UI 可能会表现不佳。我没有测量它,这只是一种感觉。

A much better alternative is MS Blend, although the start is everything else but easy. Its drag and drop resultis much better that the result of the VS designer.
But it's a pretty powerful tool, which helps you to use pretty powerful elements to create a state of the art UI. I recommend to visit at least a short workshop to get an idea of its opportunities.

一个更好的选择是MS Blend,尽管开始很简单。它的拖放结果比VS设计器的结果要好得多。
但它是一个非常强大的工具,它可以帮助您使用非常强大的元素来创建最先进的 UI。我建议至少参观一个简短的研讨会,以了解其机会。

Back to your question, IMHO, and I think many people agree, get yourself a good book e.g. WPF Unleashedand later, if you want to know more about the details, WPF Pro. There are a lot of features which are different to Winforms. You won't get to know them by using any designer. I think that's the best approach.

回到你的问题,恕我直言,我想很多人都同意,给自己买一本好书,例如WPF Unleashed和以后,如果你想了解更多关于细节的信息,WPF Pro。有很多不同的功能Winforms。你不会通过使用任何设计师来了解他们。我认为这是最好的方法。

Please also consider that there are many frameworks and libraries (e.g. MVVM light, WPFToolkit) out there, which are already solving some common problems. So it's not necessary to reinvent the wheel.

还请注意,有许多框架和库(例如MVVM lightWPFToolkit)已经在解决一些常见问题。所以没有必要重新发明轮子。

回答by peterG

I know this is an old question but for the benefit of anyone else looking at this, I think I should redress the balance a bit - reading some of the other answers, I get the feeling that some of the 'don't use the designer' sentiment comes from not using it properly. This tutorialis quite good to get you going and answers some of the criticisms in the other posts.

我知道这是一个老问题,但为了其他人的利益,我想我应该稍微调整一下平衡 - 阅读其他一些答案,我觉得有些“不使用设计师” '情绪来自于没有正确使用它。 本教程非常适合帮助您前进并回答其他帖子中的一些批评。

For instance, you can switch from the Winforms-like margin-based layout that is the default when you drop a control, to a more WPF-ish style by right-clicking and selecting 'Reset Layout'

例如,您可以通过右键单击并选择“重置布局”,从放置控件时默认的类似 Winforms 的基于边距的布局切换到更像 WPF 的样式

This videocovers similar ground.

该视频涵盖了类似的领域。

I still prefer the VS2010 designer on balance - VS2013 seems to be a bit buggy when dragging and dropping onto TabItems **, (which my current project uses a lot) - but the VS2013 Document Outline view lets you move things around in that view too, which can be a real plus.

总的来说,我仍然更喜欢 VS2010 设计器 - VS2013 在拖放到 TabItems ** 时似乎有点问题(我当前的项目经常使用它) - 但是 VS2013 文档大纲视图也允许您在该视图中移动内容,这可能是一个真正的优势。

Really, though, to get the most out of WPF and xaml you need to be reasonably fluent in both the designer view and the xaml view and switching between them; if you shy away from the designer, you are missing out on something that can help you a lot.

不过,实际上,要充分利用 WPF 和 xaml,您需要在设计器视图和 xaml 视图中都相当流畅并在它们之间切换;如果你回避设计师,你就会错过一些可以帮助你很多的东西。

** Edit - although this seems to have been improved in Update 3 for VS 2013, and in previews of VS14, to date I still get odd behaviour at times.

** 编辑 - 尽管这似乎在 VS 2013 的更新 3 中以及在 VS14 的预览中有所改进,但迄今为止我有时仍然会出现奇怪的行为。

回答by user853710

I've been through this process as you did. Afterwards I was teaching everyone in my company WPF. There are a couple of important lessons I have learned and everybody I know who works with WPF.

我和你一样经历了这个过程。之后我在公司里教大家WPF。我学到了一些重要的教训,我认识的每个使用 WPF 的人都学到了。

  1. If you are working with UI controls in the code behind, .... Then you are doing it wrong. There is absolutely no need for you to deal with UI controls in the code behind.
  2. You do not need the visual developer for clicking on it. You are much more productive by only dealing with XAML. Use Copy/Paste. Do not trust in your typing capabilities. It will save a lot of headaches.
  3. Think of the XAML just as a window that loks over the data. In the code behind you are changing the data. In XAML you are defining how the UI will interpret the data.
  4. Converters are amazing. As soon as you get a key amount of Converters, your productivity will rocket Sky high. They will take over the role of the crazy amount of control eventhandlers that hide or resize, or what ever about UI,
  1. 如果您在后面的代码中使用 UI 控件,.... 那您就做错了。您绝对不需要在后面的代码中处理 UI 控件。
  2. 您不需要可视化开发人员来单击它。通过只处理 XAML,您的工作效率会更高。使用复制/粘贴。不要相信你的打字能力。它会省去很多麻烦。
  3. 将 XAML 视为一个查看数据的窗口。在您背后的代码中,您正在更改数据。在 XAML 中,您定义 UI 将如何解释数据。
  4. 转换器是惊人的。一旦您获得关键数量的转换器,您的生产力就会飙升。他们将接管大量隐藏或调整大小的控制事件处理程序的角色,或者有关 UI 的任何内容,

It makes UI development fun. Especially once you find out how it likes to play along with Asyc processes. It really takes away a lot of the headaches that were caused by Winforms.

它使 UI 开发变得有趣。尤其是当您发现它喜欢如何与 Asyc 进程一起使用时。它确实消除了许多由 Winforms 引起的头痛。