WPF整理-自定义一个扩展标记(custom markup extension)
2013-06-27 21:27
295 查看
"Markup extensions are used to extend the capabilities of XAML, by providing declarative
operations that need more than just setting some properties. These can be used to do pretty
much anything, so caution is advised – these extensions must preserve the declarative nature
of XAML, so that non-declarative operations are avoided; these should be handled by normal
C# code."
假如我们需要实现下面的扩展标记,这个标记扩展用来提供个随机数。
我们可以这样实现这个标记扩展。
1.添加一个名为CustomMarkupExtension的类库,添加一个RandomExtension.cs类,让它继承自MarkupExtension。因为MarkupExtension类在System.Xaml程序集中,因此需要添加该程序集引用。
为实现标记扩展,我们还需要实现MarkupExtension类的ProvideValue方法。
RandomExtension.cs如下:
OK,完成。
2.使用这个标记扩展。我们新建一个名为TestRandom的WPF程序,添加CustomMarkupExtension类库的引用。
在需要使用的页面中,添加一个映射:
注意这个映射和前面的一些映射的不同之处是:因为clr-namespace不在TestRandom程序集中,因此必须注明所在程序集名称!
这样我们就可以使用如下:
使用方法看懂类没有?没有?关注那个构造函数。也可参考DebugLZQ的博文:WPF整理-XAML构建后台类对象
设计器中效果如下:
运行之,效果如下:
operations that need more than just setting some properties. These can be used to do pretty
much anything, so caution is advised – these extensions must preserve the declarative nature
of XAML, so that non-declarative operations are avoided; these should be handled by normal
C# code."
假如我们需要实现下面的扩展标记,这个标记扩展用来提供个随机数。
<TextBlock FontSize="{ mext:Random 10,100}" Text="DebugLZQ" x:Name="text1"/>
我们可以这样实现这个标记扩展。
1.添加一个名为CustomMarkupExtension的类库,添加一个RandomExtension.cs类,让它继承自MarkupExtension。因为MarkupExtension类在System.Xaml程序集中,因此需要添加该程序集引用。
为实现标记扩展,我们还需要实现MarkupExtension类的ProvideValue方法。
RandomExtension.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Markup; namespace CustomMarkupExtension { public class RandomExtension:MarkupExtension { readonly int _from, _to; public RandomExtension(int from,int to) { _from = from; _to = to; } public RandomExtension(int to):this(0,to) { } static readonly Random _rdn = new Random(); public override object ProvideValue(IServiceProvider serviceProvider) { return (double)_rdn.Next(_from, _to); } } }
OK,完成。
2.使用这个标记扩展。我们新建一个名为TestRandom的WPF程序,添加CustomMarkupExtension类库的引用。
在需要使用的页面中,添加一个映射:
<Window x:Class="TestRandom.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mext="clr-namespace:CustomMarkupExtension;assembly=CustomMarkupExtension"
注意这个映射和前面的一些映射的不同之处是:因为clr-namespace不在TestRandom程序集中,因此必须注明所在程序集名称!
这样我们就可以使用如下:
<StackPanel> <TextBlock FontSize="{ mext:Random 10,100}" Text="DebugLZQ" x:Name="text1"/> <TextBlock Text="{Binding FontSize, ElementName=text1}"/> </StackPanel>
使用方法看懂类没有?没有?关注那个构造函数。也可参考DebugLZQ的博文:WPF整理-XAML构建后台类对象
设计器中效果如下:
运行之,效果如下:
相关文章推荐
- BilinearGradientBrushExtension, custom brush in WPF using MarkupExtension instead
- WPF:实现自定义标记扩展
- 扩展Jquery自定义的一个错误警告控件ErrorProvider
- WPF之Markup Extension
- swift 中 UIColor 的一个简单扩展 UIColor+Extension
- WPF,Silverlight与XAML读书笔记第三 - 标记扩展
- 项目实战_Python.编写一个高性能可扩展支持自定义的插件式监控系统? 推荐
- [WPF]如何自定义一个User Control 并在Page.xaml里使用它
- WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension
- 不要重复造轮子,撸一个可扩展的自定义直方图
- 自定义view学习-手把手教你制作一个可扩展日历控件
- wpf中xaml的类型转换器与标记扩展
- Z.ExtensionMethods 一个强大的开源扩展库
- WPF中的ComponentResourceKey 标记扩展
- 整理并扩展一个阴影渲染的实现
- iOS8新特性扩展(Extension)应用之四——自定义键盘控件
- 谈我的SiteFactory文章发布方式一个小扩展,从模型、标签、模板到自定义插件,基于ashx的
- WP、Win10开发或者WPF开发时绘制自定义窗体~例如:一个手机
- WPF学习:4.类型转换和标记扩展