#c wpf xaml使用
NOTE: If you haven't read the first post in this series, I would encourage you do to that first, or check out the BabySmash category. Also check out http://windowsclient.net/ for more developer info on WPF.
注意:如果您还没有阅读本系列的第一篇文章,我建议您先阅读该文章,或者查看BabySmash类别。 另请访问http://windowsclient.net/以获取有关WPF的更多开发人员信息。
BACKGROUND: This is one of a series of posts on learning WPF. I wrote an application for my 2 year old using WPF, but as I'm a Win32-minded programmer, my working app is full of Win32-isms. It's not a good example of a WPF application even though it uses the technology. I'm calling on community (that's you, Dear Reader) to blog about your solutions to different (horrible) selections of my code. You can get the code http://www.codeplex.com/babysmash. Post your solutions on your blog, in the comments, or in the Issue Tracker and we'll all learn WPF together. Pick a single line, a section, or subsystem or the whole app!
背景:这是有关学习WPF的一系列文章之一。 我使用WPF为2岁的孩子编写了一个应用程序,但是由于我是位Win32程序员,所以我的工作应用程序充满了Win32-ism。 即使它使用WPF技术,也不是WPF应用程序的好例子。 我正在呼吁社区(即您,亲爱的读者)在博客中介绍您针对我的代码的不同(糟糕)选择的解决方案。 您可以获取代码http://www.codeplex.com/babysmash 。 将您的解决方案发布到您的博客,评论或问题跟踪器中,我们将一起学习WPF。 选择一条线,一个部分,子系统或整个应用程序!
BabySmash is full of shapes and stuff and those shapes a similar look. They have the same stroke (outline color) and stroke thickness (outline width) for example.
BabySmash充满了各种形状和东西,这些形状看起来很相似。 例如,它们具有相同的笔划(轮廓颜色)和笔划厚度(轮廓宽度)。
There are a lot of places in my code where I not only repeat the assignment of these styles.
在代码中有很多地方,我不仅重复了这些样式的分配。
Shape = Shape = new Path()
{
Data = MakeCharacterGeometry(nameToDisplay),
Fill = fill,
Stroke = Brushes.Black,
StrokeThickness = 5,
Height = 400
};
This kind of object initializer code is copy/pasted all over. It's not DRY. (Don't Repeat Yourself) First I started looking for object oriented ways to solve this issue. I figured I'd put in some base class that would do the work, or make a Utility (gasp!) class to do this tedium.
此类对象初始化程序代码被全部复制/粘贴。 不是干的。 (不要重复自己)首先,我开始寻找面向对象的方法来解决此问题。 我想我会放入一些可以完成这项工作的基类,或者创建一个Utility(gasp!)类来完成此乏味的工作。
Ideally I wanted this stuff in one place (hence DRY) and I wanted to be able to apply it to my shapes. Later, I realized I also wanted to occasionally apply these properties to some shapes and not others. At that point, my object-oriented idea started to fall down.
理想情况下,我希望将这些材料放在一个地方(因此干燥),并且希望能够将其应用于我的形状。 后来,我意识到我还想偶尔将这些属性应用于某些形状,而不是其他形状。 那时,我的面向对象的想法开始崩溃。
I felt (and still feel) like most of the Shape stuff for BabySmash belongs in the XAML markup, and folks on CodePlex agreed. Sherwin Rice kindly pointed me towards Styles.
我觉得(并且仍然觉得)BabySmash的大多数Shape内容都属于XAML标记,并且CodePlex上的人都同意。 宣威·莱斯(Sherwin Rice)友善地将我引向Styles。
He suggested storing these properties in named bundles of styles in the XAML. This gives me the arbitrary flexibility I needed.
他建议将这些属性存储在XAML中的命名样式集中。 这给了我所需的任意灵活性。
<Style x:Key="circle" TargetType="Ellipse">
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="400"/>
<Setter Property="StrokeThickness" Value="5"/>
<Setter Property="Stroke" Value="Black"/>
</Style>
However, as I started moving most of my shape's details over into XAML, things started repeating again. I was trading one kind of "markup" (the C# kind) for another. Poop. Well, turns out you can base styles on styles, so I was able to keep it DRY again.
但是,当我开始将形状的大部分细节移到XAML中时,事情又开始重复。 我正在用一种“标记”(C#类型)交换另一种。 船尾。 好吧,事实证明您可以将样式基于样式,因此我能够再次将其保持为DRY。
<Style x:Key="BabySmashBaseStyle" TargetType="Shape">
<Setter Property="StrokeThickness" Value="5"/>
<Setter Property="Stroke" Value="Black"/>
</Style>
<Style x:Key="trapezoid" TargetType="Path"
BasedOn="{StaticResource BabySmashBaseStyle}">
<Setter Property="Data" Value="F1 M 257.147,126.953L 543.657,126.953L 640.333,448.287L 160.333,448.287L 257.147,126.953 Z"/>
</Style>
<Style x:Key="star" TargetType="BabySmash:Star"
BasedOn="{StaticResource BabySmashBaseStyle}">
<Setter Property="NumberOfPoints" Value="5"/>
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="400"/>
</Style>
These all live in <Application.Resources> and I can apply them as I like:
这些都存在于<Application.Resources>中,我可以根据需要应用它们:
Shape s = new Ellipse();
s.Style = Application.Current.Resources[Name] as Style;
I appear to have just touched the surface of Styles, but I'm continuing to dig in. WPF is starting to make sense to me. Just a smidge.
我似乎刚刚接触过Styles的表面,但是我仍在继续挖掘。WPF对我来说已经很有意义。 只是个小东西。
Related Links
相关链接
翻译自: https://www.hanselman.com/blog/learning-wpf-with-babysmash-keeping-it-dry-with-xaml-styles
#c wpf xaml使用