This question intends to provide a list of solutions to common pitfalls, "gotcha's", or design issues when developing WPF applications. This can also include proper design-patterns as long as there is an explanation as to why it works best. Responses should be voted up or down based on how common the type of issue is. Here are the rules:
这个问题旨在提供一个解决常见缺陷、“陷阱”或开发WPF应用程序时设计问题的解决方案列表。这也可以包括适当的设计模式,只要有一个解释说明为什么它最有效。回答应该根据问题的普遍性来决定是向上还是向下。这是规则:
- One response per post. This will clearly give the most common issues the highest ranking.
- 每个帖子一个响应。这显然会给最常见的问题以最高的排名。
- It would be best to provide the link to the a related post or solution already living somewhere in SO land.
- 最好是提供链接到一个相关的职位或解决方案已经生活在某地。
7 个解决方案
#1
14
Problem : The major issue I have seen so far is that people start coding in WPF with the winform UI model in mind.
问题:我到目前为止看到的主要问题是,人们开始在WPF中编写代码时考虑到了winform UI模型。
Solution: WPF is not WinForms/MFC/Win32 So Forget all the UI side assumptions and norms you have used and learned while developing Windows based UI for last 20+ years.
解决方案:WPF不是WinForms/MFC/Win32,所以忘记过去20多年在开发基于Windows的UI时所使用和学习的所有UI方面的假设和规范吧。
It is very important to understand the core ideas behind this platform, This link- Major UI Development Breakthroughs in the new WPF platform will give an in depth view of WPF. Which lists out the following points. The highlighted ones are my favorite features of this platform.
了解这个平台背后的核心思想是非常重要的,这个链接——新的WPF平台的主要UI开发突破将会深入地了解WPF。列出了以下几点。突出显示的是这个平台我最喜欢的特性。
- Advanced Graphics
- 先进的图形
- Drawing Object Model
- 对象模型图
- Rich Application Text
- 丰富的应用程序文本
- Adaptable UI Layout
- 适应性强的UI布局
- Flexible Content Model
- 灵活的内容模型
- Lookless Controls
- Lookless控制
- Data-Driven UI
- 数据驱动的用户界面
- Consistent Styles
- 一致的风格
- Triggers
- 触发器
- Declarative Programming
- 声明性编程
#2
3
Not realising how bad the font rendering is at the start of a project and being told by the client they can't stand looking at it because of how fuzzy everything looks.
在项目开始时,没有意识到字体呈现有多糟糕,客户告诉他们,他们无法忍受看着它,因为一切看起来都很模糊。
#3
3
Problem: Using the M-V-VM design pattern, where do I instantiate the views? Does this happen in the ViewModel? SO Question 1, SO Question 2
问题:使用M-V-VM设计模式,在哪里实例化视图?这在ViewModel中发生吗?问题1,问题2
Solution: WPF development is most effective when using the M-V-VM pattern as opposed to other common patterns such as M-V-C. The tendency is to treat the ViewModel the same as you would the controller which would handle opening and creating views as well as the models. This is not the case in M-V-VM. Views are the only place where are the views should be created. The ViewModels should know nothing of the view. SO Answer 1, SO Answer 2
解决方案:当使用M-V-VM模式时,WPF开发是最有效的,而不是像M-V-C这样的其他常见模式。我们倾向于像处理打开和创建视图以及模型的控制器一样对待ViewModel。M-V-VM的情况并非如此。视图是应该创建视图的惟一位置。视图模型应该对视图一无所知。回答1,回答2
#4
2
Problem/Question: SO Question
问题/问题:所以的问题
How do I expose a DependencyProperty of a component in my user control to users? There are plenty of examples of how to expose a normal property by creating a new dependency property and binding, but none on how to expose a read-only property like FrameworkElement.ActualWidthProperty.
如何将用户控件中的组件的DependencyProperty公开给用户?关于如何通过创建一个新的依赖项属性和绑定来公开一个普通属性的例子有很多,但是没有关于如何公开一个只读属性的例子,比如FrameworkElement.ActualWidthProperty。
Solution: You need to expose a new Readonly DependencyProperty in your user control, and update it whenever your contained "component"'s ActualWidthProperty gets updated. This requires using DependecyPropertyDescriptor to get notified of changes that occur. SO Solution
解决方案:您需要在您的用户控件中公开一个新的Readonly dependency属性,并在包含的“组件”的ActualWidthProperty被更新时对其进行更新。这需要使用DependecyPropertyDescriptor来通知发生的变更。所以解决方案
#5
1
Getting data binding to work properly between properties defined in ContentControls (Windows, UserControls, etc..) and properties on elements that make up the controls content. For example. Let's say I have a Window that looks like this:
使数据绑定在ContentControls中定义的属性(Windows、UserControls等)和组成控件内容的元素上的属性之间正常工作。为例。假设我有一个这样的窗口:
<Window x:Name="MyWindow"....>
<TextBlock Text="{Binding Path=PropertyDefinedInMyWindow}" />
</Window>
Problem: No matter how often you update the "PropertyDefinedInMyWindow" it never gets reflected in the TextBlock. SO Question
问题:无论你多久更新一次“propertydefinmywindow”,它都不会在TextBlock中反映出来。所以问题
Solution: You need to set the DataContext of the Window or tell the binding which element the property lives on. SO Solution
解决方案:您需要设置窗口的DataContext,或者告诉绑定该属性所在的元素。所以解决方案
#6
0
Ivan Towlson did a really good presentation on this topic. Most of the information is in his slides, which you can get from here: http://hestia.typepad.com/flatlander/2008/08/codecamp-2008-.html
Ivan Towlson在这个主题上做了很好的报告。大部分信息都在他的幻灯片中,你可以从这里获得:http://hestia.typepad.com/flatlander/2008/08/codecamp-2008-.html
#7
0
Using code - behind in views, which makes baby FSM cry.
在视图中使用代码- behind,这会让婴儿FSM哭出来。
#1
14
Problem : The major issue I have seen so far is that people start coding in WPF with the winform UI model in mind.
问题:我到目前为止看到的主要问题是,人们开始在WPF中编写代码时考虑到了winform UI模型。
Solution: WPF is not WinForms/MFC/Win32 So Forget all the UI side assumptions and norms you have used and learned while developing Windows based UI for last 20+ years.
解决方案:WPF不是WinForms/MFC/Win32,所以忘记过去20多年在开发基于Windows的UI时所使用和学习的所有UI方面的假设和规范吧。
It is very important to understand the core ideas behind this platform, This link- Major UI Development Breakthroughs in the new WPF platform will give an in depth view of WPF. Which lists out the following points. The highlighted ones are my favorite features of this platform.
了解这个平台背后的核心思想是非常重要的,这个链接——新的WPF平台的主要UI开发突破将会深入地了解WPF。列出了以下几点。突出显示的是这个平台我最喜欢的特性。
- Advanced Graphics
- 先进的图形
- Drawing Object Model
- 对象模型图
- Rich Application Text
- 丰富的应用程序文本
- Adaptable UI Layout
- 适应性强的UI布局
- Flexible Content Model
- 灵活的内容模型
- Lookless Controls
- Lookless控制
- Data-Driven UI
- 数据驱动的用户界面
- Consistent Styles
- 一致的风格
- Triggers
- 触发器
- Declarative Programming
- 声明性编程
#2
3
Not realising how bad the font rendering is at the start of a project and being told by the client they can't stand looking at it because of how fuzzy everything looks.
在项目开始时,没有意识到字体呈现有多糟糕,客户告诉他们,他们无法忍受看着它,因为一切看起来都很模糊。
#3
3
Problem: Using the M-V-VM design pattern, where do I instantiate the views? Does this happen in the ViewModel? SO Question 1, SO Question 2
问题:使用M-V-VM设计模式,在哪里实例化视图?这在ViewModel中发生吗?问题1,问题2
Solution: WPF development is most effective when using the M-V-VM pattern as opposed to other common patterns such as M-V-C. The tendency is to treat the ViewModel the same as you would the controller which would handle opening and creating views as well as the models. This is not the case in M-V-VM. Views are the only place where are the views should be created. The ViewModels should know nothing of the view. SO Answer 1, SO Answer 2
解决方案:当使用M-V-VM模式时,WPF开发是最有效的,而不是像M-V-C这样的其他常见模式。我们倾向于像处理打开和创建视图以及模型的控制器一样对待ViewModel。M-V-VM的情况并非如此。视图是应该创建视图的惟一位置。视图模型应该对视图一无所知。回答1,回答2
#4
2
Problem/Question: SO Question
问题/问题:所以的问题
How do I expose a DependencyProperty of a component in my user control to users? There are plenty of examples of how to expose a normal property by creating a new dependency property and binding, but none on how to expose a read-only property like FrameworkElement.ActualWidthProperty.
如何将用户控件中的组件的DependencyProperty公开给用户?关于如何通过创建一个新的依赖项属性和绑定来公开一个普通属性的例子有很多,但是没有关于如何公开一个只读属性的例子,比如FrameworkElement.ActualWidthProperty。
Solution: You need to expose a new Readonly DependencyProperty in your user control, and update it whenever your contained "component"'s ActualWidthProperty gets updated. This requires using DependecyPropertyDescriptor to get notified of changes that occur. SO Solution
解决方案:您需要在您的用户控件中公开一个新的Readonly dependency属性,并在包含的“组件”的ActualWidthProperty被更新时对其进行更新。这需要使用DependecyPropertyDescriptor来通知发生的变更。所以解决方案
#5
1
Getting data binding to work properly between properties defined in ContentControls (Windows, UserControls, etc..) and properties on elements that make up the controls content. For example. Let's say I have a Window that looks like this:
使数据绑定在ContentControls中定义的属性(Windows、UserControls等)和组成控件内容的元素上的属性之间正常工作。为例。假设我有一个这样的窗口:
<Window x:Name="MyWindow"....>
<TextBlock Text="{Binding Path=PropertyDefinedInMyWindow}" />
</Window>
Problem: No matter how often you update the "PropertyDefinedInMyWindow" it never gets reflected in the TextBlock. SO Question
问题:无论你多久更新一次“propertydefinmywindow”,它都不会在TextBlock中反映出来。所以问题
Solution: You need to set the DataContext of the Window or tell the binding which element the property lives on. SO Solution
解决方案:您需要设置窗口的DataContext,或者告诉绑定该属性所在的元素。所以解决方案
#6
0
Ivan Towlson did a really good presentation on this topic. Most of the information is in his slides, which you can get from here: http://hestia.typepad.com/flatlander/2008/08/codecamp-2008-.html
Ivan Towlson在这个主题上做了很好的报告。大部分信息都在他的幻灯片中,你可以从这里获得:http://hestia.typepad.com/flatlander/2008/08/codecamp-2008-.html
#7
0
Using code - behind in views, which makes baby FSM cry.
在视图中使用代码- behind,这会让婴儿FSM哭出来。