如何在设计时在棱镜模块中使用资源字典?
我在 silverlight 应用程序中使用 prism 框架,并在单独的 XAP 中使用多个模块。
我在 shell 项目中定义了一个资源字典。在我的模块中,我可以很好地使用资源,但由于模块与 shell 分离,直到它们在运行时加载为止,设计器不会显示它们或识别它们。
有没有办法让模块在设计时了解我的资源,而无需在每个视图 xaml 中合并我的资源文件?
我的资源文件位于“公共”项目中。
I am using prism framework in a silverlight app with multiple modules in separate XAPs.
I have a resource dictionary defined in my in my shell project. In my modules I can use the resources fine, but since the modules are decoupled from the shell until they are loaded at runtime the designer does not show them or recognize them.
Is there a way to make the modules aware of my resources at design time without merging my resource file in every view xaml?
My resource files are in a "common" project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我肯定有设计时资源的解决方案。
优点:
让我们考虑以下解决方案:
您可以在 MyApp.Common 中定义画笔、隐式样式、模板等。
使用我的 SharedResourceDictionary 在所有项目中包含 ResourceDictionary。在设计时,它将为每个设计者加载ResourceDictionary,在运行时,ResourceDictionary 将仅在必要时加载。
使用示例:
在 App.xaml 中包含 SharedResourceDictionary
在设计器找不到的地方包含 SharedResourceDictionary
一些共享资源,例如在 MyApp.Module1/UserControl1.xaml 中
来源:
I think I have definitely solution for design-time resources.
Benefits:
Let's consider following solution:
you can define brushes, implicit styles, templates etc in MyApp.Common.
use my SharedResourceDictionary to include the ResourceDictionary in all projects. At design-time it will load the ResourceDictionary for each designer, at runtime the ResourceDictionary will be loaded only when necessary.
Usage example:
include SharedResourceDictionary in App.xaml
include SharedResourceDictionary everywhere designer fails to find
some share resource, e.g. in MyApp.Module1/UserControl1.xaml
Source:
我发现有几个解决方案:
1)当您创建模块项目时,将 App.xaml 留在项目中而不是删除它,并在其中实例化您的资源,就像它本身是它自己的应用程序一样(如果您已经删除了新的应用程序类,您还可以将其添加到项目中)。当您的模块加载到 shell 中时,该文件将被忽略,因此它本质上仅在设计时有效。这在 Visual Studio 和 Blend 中效果很好,但如果您有很多模块,内存占用可能会成为问题。
2) 使用设计时间资源。有关此处设置的一些信息: http://adamkinney.com/blog/2010/05/04/design-time-resources-in-expression-blend-4-rc/。这仅提供混合支持,并且您的视图将被删除 Visual Studio 中的所有样式和格式。这对我来说并不理想,因为我喜欢在 Visual Studio 中处理 UI 的某些方面。似乎也没有手动设置设计时资源的记录方法。
I have found there are a couple of solutions to this:
1) When you create a module project, leave the App.xaml in the project instead of deleting it and instantiate your resources in there just as if it were its own application by itself (you can also add a new Application class to the project if you have already deleted it). When your module is loaded into the shell that file will be ignored so it's essentially only valid during design time. This works well in visual studio and blend although if you have many modules, memory footprint may become a problem.
2) Using design time resources. Some info about setting this up here: http://adamkinney.com/blog/2010/05/04/design-time-resources-in-expression-blend-4-rc/. This offers only blend support and your views will be stripped of all styles and formatting in visual studio. This was not ideal for me because I like working on certain aspects of the UI in visual studio. There also doesn't seem to be a documented way of manually setting up design time resources.
将资源从 Shell 迁移到共享组件并使设计人员工作正常的个人经验小指南
基于阅读此类问题和在互联网上搜索相同/相似问题的一些想法。我写这篇文章主要是因为问题 2(如下),该问题与此问题相关,恕我直言。
所以,我们有相同的设计,所有样式和资源都在 Shell 中。这产生了 2 个问题:
发现)
发现)
因此我们将所有样式迁移到共享程序集(资源)。
要解决第一个问题,您需要像 Liero 提出的那样,即向每个 UserControl 添加资源字典。我没有尝试他的 SharedDictionary,但普通的 ResourceDictionary 肯定会带回上下文帮助并删除蓝色下划线。然而设计师仍然没有正确露面。
那么第二个问题。有一个小技巧可以在设计时将样式引入设计器,仅描述在本文中。基本上,您将一个名为 DesignTimeResources.xaml 的资源字典添加到您的项目中,其中包含对资源的引用:
然后将其移动到 Properties 文件夹。比手动编辑项目文件并将该文件的项目更改为:
基本上,如果您添加设计时资源,Blend 将生成一个文件。 VS 无法创建它,但可以很好地读取它。项目文件的编辑表明您基本上不希望发布该文件。
这里还有两个小问题,也许会对某人有所帮助。
clr-namespace:XXX; assembly=Resources
”等命名空间中引用这些控件。您应该删除“; assembly=Resources ”部分,因为它现在是同一个程序集。
我们已经在用户控件中引入了一些本地资源,如下所示:
因此,首先我只是将新的 ResourceDictionary 添加到此块中,它要求我提供 x:Key。我习惯于直接向 UserControl.Resources 添加资源,以至于我首先没有意识到为了合并另一个字典,您需要通常可以跳过的
标记。所以它看起来像这样:Small own-experience guide for migrating resources from Shell to shared essembly and making designer work just fine
Some thoughts based on reading such questions and searching internet on the same/similar problem. I'm writing this primarily because of problem 2 (below), which is related to this issue, IMHO.
So, we had the same design, all styles and resources were in Shell. This produced 2 problems:
found)
found)
So we migrated all styles to shared assembly (Resources).
To solve the first problem you would need sth like Liero proposed, i.e. add resource dictionary to each UserControl. I didn't try his SharedDictionary, but normal ResourceDictionary definitely brings context help back and removes blue-underscore lines. Designer however still didn't show up properly.
So the second problem. There is a small trick to bring styles to designer at design time only described in this article. Basically you add a resource dictionary named DesignTimeResources.xaml to your project that contains reference to your resources:
Than move it to Properties folder. Than edit manually project file and change the item for this file to this:
Basically it's a file that Blend would generate if you add design time resources. VS cannot create it, although can read it just fine. The editing of project file says that you don't want basically this file in release.
Two minor gotchas here also, perhaps it will help somebody.
clr-namespace:XXX;assembly=Resources
". The ";assembly=Resources
"-part you should delete, as it is the same assembly now.We already head some local resources in our UserControls, like this:
So at first I just added new ResourceDictionary into this block, which asked me to provide an x:Key. I was so used to add resources directly to UserControl.Resources, that I didn't first realise that in order to merge another dictionary you would need
<ResourceDictionary>
tag that normally you could skip. So it will look like this:如果您希望为您的视图提供设计时数据,我建议您阅读这篇文章 。它展示了如何使用 Blend 在项目中创建设计时数据,这些数据不包含在应用程序的发布版本中。
希望有帮助。
If you're looking to provide design time data for your views may I suggest reading this article. It shows how to use Blend to create design time data within your project which is not included in the release builds of the application.
Hope it helps.