您如何将主题从Silverlight移植到WPF? [英] How do you port a theme from Silverlight to WPF?

查看:128
本文介绍了您如何将主题从Silverlight移植到WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单"!

我刚遇到 Rudi Grobler的这篇博客文章表示将主题从Silverlight移植到WPF是容易的".不幸的是,他没有说怎么做.

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.

下载并安装

我已经安装了 WPF工具包我还去了我感兴趣的主题的源代码(BureauBlue)(警告,这需要一些时间才能加载),然后将其粘贴到我的测试项目中的新资源字典文件中.

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.

哦,引用不完整

很多蓝色的波浪线.

替代文本http://img32.imageshack.us/img32/6032/brokenreferences.jpg

此处未引用

所以,我去添加引用并发现...没有列出.

So, I went to add the references and discovered...they aren't listed.

替代文本http://img35.imageshack.us/img35/7466/addreferencedialog.jpg

啊,那里有

幸运的是,经过一段时间的浏览之后,我能够找到它们.

Fortunately, I was able to find them after some extensive browsing.

替代文本http://img269.imageshack.us/img269/3830/addreferencedialogbrows.jpg

所以我添加了它们

找到丢失的参考文献后,我尝试添加它们.这实际上似乎可以解决断开的xmlns链接(至少蓝色的波浪线消失了),但是在我构建项目时出现错误:

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:

错误1未知的生成错误,无法解析对程序集'System.Windows,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e的依赖",因为尚未预先加载.使用ReflectionOnly API时,必须通过ReflectionOnlyAssemblyResolve事件预先加载或按需加载相关程序集. NmtConcept

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


然后,我问...

如何在WPF中使用Silverlight主题?

How do I make a Silverlight theme work in WPF?

更新!

好吧,我以为我知道了.我决定从DataGrid开始.我弄清楚了Silverlight程序集的所有WPF等效项是什么,实际上我可以编译并运行我的项目.然后我看到了结果...

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...

替代文本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.

以下是上的示例Silverlight工具包网页.

替代文本http://img196.imageshack.us/img196/997/silverlightdatagrid.jpg

所以,我什至没有接近.

So, I didn't even come close.

我做什么

请允许我解释一下为解决这一混乱局面所做的事情.

Please allow me to explain what I did to arrive at this mess.

DataGridFrozenGrid

首先,我必须从此Silverlight Toolkit源代码页面中获取DataGridFrozenGrid,因为WPF从未听说过这样的课程.

First, I had to grab DataGridFrozenGrid from this Silverlight Toolkit source code page because WPF had never heard of such a class.

BureauBlue

然后我从 BureauBlue的源代码粘贴了关键段/a>(再次警告:这名婴儿装载缓慢).

Then I pasted in the key pieces from the source code for BureauBlue (again, warning: this baby is slow to load).

通过关键片段,我的意思是:

  1. 文件开头的所有画笔资源加
  2. 以下每个控件的Style:

  • DataGridColumnHeader
  • DataGridCell
  • DataGridRowHeader
  • DataGridRow

奇怪的是,原始文件不包含DataGrid本身的样式(如果我输入错了,请更正我,但我使用了Find和所有内容).

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

接下来,我将xmlns引用转换为WPF等效项.这是我的ResourceDictionary元素的结果:

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.

编辑直到可用

一旦我完成了所有这些工作,我仍然会遇到一些小问题.某些x:Name元素中有空格.编译器不允许这样做,因此我不得不用下划线替换空格.

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.

以下是相关片段:

<!-- 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.

我必须做的另一个更改是:开始时,某些SolidColorBrushLinearGradientBrush项目使用了x:Name而不是x:Key.我将它们全部更改为x:Key.也许这也不是个好主意,但是编译器又使我失望了.

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.

这对您有帮助吗?

如果您仍然与我在一起,有什么建议吗?

If you're still with me after all that, got any suggestions?

推荐答案

我基本上得出的结论是,我试图将方形钉插入圆孔中(尽管声称它很容易"). Silverlight DataGrid与WPF工具包DataGrid有点不同.因此,目前,我一直在尝试将默认数据网格的样式设置为BureauBlue.这是一项非常艰苦的工作,远未达到完美的水平,但相对于已移植的​​样式而言,这是绝对的改进.

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 Toolkit DataGrid. 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.

替代文本http://img26.imageshack.us/img26/7163/styleddatagrid .jpg

这篇关于您如何将主题从Silverlight移植到WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆