如何将主题从 Silverlight 移植到 WPF?
这很“简单”!
我刚刚遇到Rudi Grobler 的这篇博文表示将主题从 Silverlight 移植到 WPF 非常“容易”。不幸的是,他没有说如何去做。
下载并安装
我已安装 WPF 工具包 和 Codeplex 的 Silverlight 工具包。
我还去挖了我感兴趣的主题的源代码(BureauBlue) (警告,这需要一点时间来加载),并将其粘贴到我的测试项目中的新资源字典文件中。
哦,参考文献被破坏了
很多蓝色的波浪线。
替代文本 http://img32.imageshack.us/img32/6032/brokenreferences.jpg
这里没有参考文献
因此,我去添加参考文献并发现......它们没有列出。
替代文本 http://img35.imageshack.us/img35/7466/addreferencedialog.jpg
啊,它们就在那里
幸运的是,经过一番广泛的浏览后我能够找到它们。
替代文本 http://img269.imageshack.us/img269/3830/addreferencedialogbrows.jpg
所以我添加了它们
找到丢失的参考文献后,我尝试添加它们。这实际上似乎解决了损坏的 xmlns
链接(至少蓝色波浪线消失了),但是当我构建项目时,我收到了一个错误:
错误 1 未知生成错误,“无法解析对程序集“System.Windows,版本=2.0.5.0,文化=中性,PublicKeyToken=7cec85d7bea7798e”的依赖关系,因为它尚未预加载。使用 ReflectionOnly API 时,必须通过 ReflectionOnlyAssemblyResolve 事件预加载或按需加载依赖程序集。 Nmt概念
而且,所以我问...
如何使 Silverlight 主题在 WPF 中工作?
更新!
好吧,我想我已经弄清楚了。我决定从 DataGrid
开始。我弄清楚了 Silverlight 程序集的所有 WPF 等效项是什么,并且我实际上让我的项目得以编译和运行。然后我看到了结果...
alt text http://img44.imageshack.us/img44/2418/porteddatagrid .jpg
标题在某种程度上是正确的(除了缺少排序箭头),但其余部分看起来像是发生了某种涉及棕褐色砖块的可怕事故。
以下是 Silverlight Toolkit 网页上的示例。
替代文字 http://img196.imageshack.us/img196/997/silverlightdatagrid.jpg
所以,我甚至没有接近。
我做了什么
请允许我解释一下我做了什么才导致了这个混乱。
DataGridFrozenGrid
首先,我必须从这个中获取DataGridFrozenGrid
Silverlight Toolkit源代码页因为WPF从未听说过这样的类。
BureauBlue
然后我粘贴了的关键部分 BureauBlue 的源代码(再次警告:这个宝贝加载速度很慢)。
关键部分,我的意思是:
- 文件开头的所有画笔资源加上
- 每个控件的
样式:
- DataGridColumnHeader
- DataGridCell
- DataGridRowHeader
- DataGridRow
奇怪的是,原始文件不包含 DataGrid
本身的样式(如果我是这样,请纠正我)错了,但我使用了“查找”和其他所有功能)。
银光 --> WPF
接下来,我将 xmlns
引用转换为 WPF 等效项。以下是我的 ResourceDictionary
元素的结果:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mwc="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:mwcp="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
xmlns:swcp="clr-namespace:System.Windows.Controls.Primitives;assembly=CommonLibraryWpf"
xmlns:sw="clr-namespace:System.Windows;assembly=WPFToolkit">
我当然编辑了命名空间标签以匹配。
编辑直到它起作用
当我完成所有这些工作后,我仍然遇到了一些小问题。某些 x:Name
元素中包含空格。编译器不允许这样做,所以我不得不用下划线替换空格。
以下是相关片段:
<!-- Important: all underscores used to be spaces -->
<sw:VisualState
x:Name="MouseOver_CurrentRow_Selected">
<sw:VisualState
x:Name="Normal_CurrentRow">
<sw:VisualState
x:Name="Normal_Selected">
<sw:VisualState
x:Name="Normal_EditingRow">
<sw:VisualState
x:Name="Normal_AlternatingRow" />
<sw:VisualState
x:Name="Normal_Selected">
<sw:VisualState
x:Name="MouseOver_Selected">
<sw:VisualState
x:Name="Unfocused_Selected">
更改这些名称似乎是一个非常糟糕的主意 - 并且可能是我所有问题的原因 - 但我不知道还能做什么来编译这些东西。
我必须做的另一个更改是:开头的一些 SolidColorBrush
和 LinearGradientBrush
项目使用 x:Name
而不是 x :键
。我将它们全部更改为 x:Key
。也许这也是一个坏主意,但同样,编译器创造了我。
这对你有帮助吗?
如果你仍然和我在一起,有什么建议吗?
It's "easy"!
I just came across this blog post by Rudi Grobler that says it's "easy" to port a theme from Silverlight to WPF. Unfortunately, he doesn't say how to do it.
Download and install
I have installed both the WPF Toolkit and Silverlight Toolkit from Codeplex.
I also went and dug up the source code for the theme I'm interested in (BureauBlue) (warning, this takes a little while to load), and pasted that into a fresh Resource Dictionary file in my test project.
Ut oh, broken references
Lots of blue squiggly lines.
alt text http://img32.imageshack.us/img32/6032/brokenreferences.jpg
No references here
So, I went to add the references and discovered...they aren't listed.
alt text http://img35.imageshack.us/img35/7466/addreferencedialog.jpg
Ahh, there they are
Fortunately, I was able to find them after some extensive browsing.
alt text http://img269.imageshack.us/img269/3830/addreferencedialogbrows.jpg
And so I added them
Having found the missing references, I attempted to add them. This actually seemed to resolve the broken xmlns
links (at least the blue squiggly lines went away), but when I built my project I got an error:
Error 1 Unknown build error, 'Cannot resolve dependency to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' NmtConcept
And, so I ask...
How do I make a Silverlight theme work in WPF?
Update!
Well, I thought I figured it out. I decided to start with the DataGrid
. I figured out what all the WPF equivalents were for the Silverlight assemblies, and I actually got my project to compile and run. And then I saw the result...
alt text http://img44.imageshack.us/img44/2418/porteddatagrid.jpg
The header is somewhat correct (except for the missing sort arrows), but the rest of it looks like there was some kind of horrible accident involving tan colored bricks.
Here's the example on the Silverlight Toolkit webpage.
alt text http://img196.imageshack.us/img196/997/silverlightdatagrid.jpg
So, I didn't even come close.
What I did
Please allow me to explain what I did to arrive at this mess.
DataGridFrozenGrid
First, I had to grab DataGridFrozenGrid
from this Silverlight Toolkit source code page because WPF had never heard of such a class.
BureauBlue
Then I pasted in the key pieces from the source code for BureauBlue (again, warning: this baby is slow to load).
By key pieces, I mean:
- All the brush resources at the beginning of the file plus
The
Style
for each of these controls:- DataGridColumnHeader
- DataGridCell
- DataGridRowHeader
- DataGridRow
Strangely, the original file contained no style for the DataGrid
itself (please correct me if I'm wrong, but I used Find and everything).
Silverlight --> WPF
Next, I converted the xmlns
references to WPF equivalents. Here's how my ResourceDictionary
element turned out:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mwc="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:mwcp="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
xmlns:swcp="clr-namespace:System.Windows.Controls.Primitives;assembly=CommonLibraryWpf"
xmlns:sw="clr-namespace:System.Windows;assembly=WPFToolkit">
I of course edited the namespace tags to match.
Edit until it works
Once I had all this working, I still had a few minor problems. Some of the x:Name
elements had spaces in them. The compiler wouldn't allow this, so I had to replace the spaces with underscores.
Here are the relevant snippets:
<!-- Important: all underscores used to be spaces -->
<sw:VisualState
x:Name="MouseOver_CurrentRow_Selected">
<sw:VisualState
x:Name="Normal_CurrentRow">
<sw:VisualState
x:Name="Normal_Selected">
<sw:VisualState
x:Name="Normal_EditingRow">
<sw:VisualState
x:Name="Normal_AlternatingRow" />
<sw:VisualState
x:Name="Normal_Selected">
<sw:VisualState
x:Name="MouseOver_Selected">
<sw:VisualState
x:Name="Unfocused_Selected">
Changing these names seemed like a very bad idea--and may be the cause of all my problems--but I didn't know what else to do to get the thing to compile.
The other change I had to make was: some of the SolidColorBrush
and LinearGradientBrush
items at the beginning used x:Name
instead of x:Key
. I changed all of them to x:Key
. Perhaps this was also a bad idea, but again, the compiler made me.
Does this help you help me?
If you're still with me after all that, got any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我基本上得出的结论是,我正在尝试将方形钉子装入圆孔中(尽管声称这很“容易”)。 Silverlight
DataGrid
与 WPF ToolkitDataGrid
差别太大。因此,目前我只是尝试将默认数据网格的样式设置为看起来像 BureauBlue。这是一项相当艰苦的工作,而且远非完美,但相对于移植风格来说,这是一个明确的改进。替代文本 http://img26.imageshack.us/img26/7163/styleddatagrid.jpg< /a>
I basically came to the conclusion that I was trying to fit a square peg into a round hole (despite claims that it's "easy"). The Silverlight
DataGrid
is just too different from the WPF ToolkitDataGrid
. So, for the moment, I've just been trying to style the default data grid to look like BureauBlue. It's quite painstaking work and far from perfect, but it's a definite improvement over the ported style.alt text http://img26.imageshack.us/img26/7163/styleddatagrid.jpg
我猜想未解析的引用是对 System.Windows.dll 的 Silverlight 版本。您必须更改主题项目中的引用以指向 dll 的 WPF 版本。
您还可以查看 WPF 主题 项目(由 Rudi Grobler 协调),该项目已经有了 BureauBlue。
I would guess that the unresolved reference is to the Silverlight version of the System.Windows.dll. You will have to change the references in the theme project to point to the WPF version of the dlls.
You can also look at the WPF themes project (coordinated by Rudi Grobler), which already has the BureauBlue.