在Silverlight中,使用XAML剪贴画的正确方法是什么?
假设我已经在 XAML 中为 Silverlight 应用程序构建了一些剪贴画,或者从 http://www.xamalot.com,在我的应用程序中使用它的最佳方式是什么?
是否最好为每件艺术品创建一个用户控件?或者有更好的方法可以从资源字典中引用它吗?
更新:
提供的答案非常详细,似乎暗示将其用于比我想象的更大的目的。我真的只是想知道使用 XAML 剪贴画(例如)工具栏上的按钮的最佳方式。
Say I've built some clipart for my Silverlight application in XAML, or downloaded it from http://www.xamalot.com, what is the best way to use it in my app?
Is it best to create a user-control for each piece of art? Or is there a better way that I can reference it from a resource dictionary?
Update:
The answers provided are pretty detailed, and seem to be hinting at using this for a much larger purpose than I imagined. I really was just wanting to know the best way of consuming XAML clipart for use as (say) a button on a toolbar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个更好的方法。
您将为每件剪贴画拥有一个单独的文件,但没有网站使用的包装器,直接在资源字典中使用 Canvas 实例并不是最好的方法。
我们将从
ResourceDictionary
开始,但使其看起来像这样:-这有两件事,首先它使用
ControlTemplate
来包含图像,这是一个更有效的方法当图像可以多次使用时(例如当图像被用作图标时)的存储形式。其次,它使用一个 Viewbox,它兑现了基于矢量的图形的承诺,它允许将图像缩放到指定的大小。
您需要向项目添加一个新的
模板化自定义控件
,并将其命名为XamlImage
。你不需要对它做任何事情,它只需要存在即可。暂时让我们将此资源字典添加到 App.Xaml 中(它不太可能保留在其中)。
现在,您可以使用以下方式将此图像放置在页面中:-
现在,根据您的真实意图和数量,有多种前进方式,场景会有所不同,我无法详尽地发表评论。因此,采取两个极端...
如果您只想从中选择一些图像,那么您可以简单地创建更多文件并将它们添加到 app.xaml 的
MergedDictionaries
中。然而,最大的缺点是所有这些图像 Xamls 都会在应用程序启动时解析和加载,这可能是不可取的。另一方面,您可能有一个大型的分类 Xaml 剪贴画库。在这种情况下,您需要将它们放在文件夹结构中,并为每个文件包含一个标准大小的缩略图 png。充当目录的 Xml 文件,然后根据需要加载 Xaml 文件。在这种情况下,您可以从文件中删除
ResourceDictionary
并将ControlTemplate
作为根元素,使用XamlReader
加载模板,然后缓存模板在您自己的字典中(可能作为目录实现的一部分)。There is a better way.
You would have an individual file for each peice of clip art but not the wrapper that the site uses, a Canvas instance directly in a resource dictionary not he best way forward.
We'll start with a
ResourceDictionary
but make it look something like this:-This does two things, first it uses a
ControlTemplate
to contain the image, this is a more efficient form of storage when an image may be used multiple times such as when the image is being used as an icon.Secondly it uses a
Viewbox
which delivers on the promise of vector based graphics, it allows the image to be scaled to a specified size.You need to add to your project a new
Templated Custom Control
and call itXamlImage
. You don't need to do anything to it, it just needs to exist.For the time being lets just add this resource dictionary to the App.Xaml (thats not likely where it will remain).
Now you can place this image in a page using:-
Now there are several ways forward depending on your real intent and volumes, the scenarios are to varied for me to comment exhaustively. So taking the two extremes...
If you have just a few images you would like to pick from then you could simply create more files and add them to the
MergedDictionaries
of the app.xaml. However the big down side is that all those image Xamls are parsed and loaded at application startup which may not be desirable.On the other hand have you may have a large library of categorised Xaml clipart. In which case you will want to place them in a folder structure and include a standard sized thumbnail png for each. A Xml file to act as a catalog and then on demand load the Xaml files as needed. In this case you can ditch the
ResourceDictionary
from the files and have theControlTemplate
as root element, useXamlReader
to load the template then cache the template in your own dictionary (probably as part of a catalog implementation).您可以将 XAML 剪贴画对象作为 XML 存储在数据库中,然后通过 Web 服务按需下载它们。下载的 XAML 出现在您的客户端后,您可以执行以下操作:
You could store your XAML clipart objects in a database as XML, then download them on demand via a webservice. Once the downloaded XAML is present in your client you could do something like :