敢于尝试,就等于你已经向成功迈出了第一步 --Aaronyang的博客(www.ayjs.net)-www.8mi.me
=============时隔两年后再看WPF==========
因为以前的经验,所以继承FrameworkElement,我就简写继承FWE ,继承UIElement就写继承UIE
后面重头戏就是blend中的开发,不想写的千篇一律。如果期待,左侧有关注按钮。
1. v1.0菜单
新建WPF项目,右键项目-属性-资源
导入几个项目用到的图标
第一种:带图标MenuItem指定图标方式
自己在项目里面新建一个文件夹,比如叫icons,然后把图片拷贝进去,Source写法,是相对路径 程序集名称;component/正常路径
<MenuItem Header="打开"> <MenuItem.Icon> <Image Width="16" Height="16" Source="/blend1;component/icons/user_edit.png"/> </MenuItem.Icon> </MenuItem>
可视化操作,鼠标放到Image上,按F4设置属性
也可以生成该方式路径
第二种:
使用程序集中的资源,有人说在xaml中不能使用,只能在后台代码,使用wpf pack uri的知识去使用
其实,我们在xaml中可以使用数据绑定的方式使用
但是奇怪的没有用..但是如果是字符串的资源就可以显示出来了,估计这里缺少个值转换器,需要增加一个。
算了吧,假如我们在后台需要怎么写,我们定义这个Image的name为imgHistoryMenu
OK,我们在窗体的Loaded中加载代码:
private void Window_Loaded(object sender, RoutedEventArgs e) { var imghistoryImage = blend1.Properties.Resources.folder_magnify; MemoryStream memory = new MemoryStream(); imghistoryImage.Save(memory, System.Drawing.Imaging.ImageFormat.Png); ImageSourceConverter converter = new ImageSourceConverter(); ImageSource source = (ImageSource)converter.ConvertFrom(memory); imgHistoryMenu.Source = source; }
效果:
当然,我真的不想在后台编写任何代码,我只想在xaml中使用..
第三种:也是我最推荐大家用的矢量图标方式,使用Viewbox+Path的方式
<MenuItem Header="打印"> <MenuItem.Icon> <Viewbox Width="16" Height="16"> <Path Data="M30.754,48.363001L42.636803,48.363001 42.935001,49.115 30.754,49.115z M21.746382,48.363001L27.137999,48.363001 27.137999,49.115 21.429998,49.115z M20.6849,45.4153L17.523399,51.042898 46.934898,51.042898 43.773399,45.4153 39.369801,45.4153 39.369801,45.790197 22.7057,45.790197 22.8633,45.4153z M18.976601,25.266799C17.4596,25.266799,16.2292,26.4987,16.2292,28.015599L16.2292,37.561098C16.2292,39.079397,17.4596,40.309797,18.976601,40.309797L21.2995,40.309797 43.158798,40.309797 45.4818,40.309797C46.997398,40.309797,48.229198,39.079397,48.229198,37.561098L48.229198,28.015599C48.229198,26.4987,46.997398,25.266799,45.4818,25.266799z M2.7473803,14.789L61.251301,14.789C62.770802,14.789,64,16.019401,64,17.536401L64,42.666498C64,44.184797,62.770802,45.4153,61.251301,45.4153L46.026001,45.4153 50.2878,53.004999 14.1706,53.004999 18.432301,45.4153 2.7473803,45.4153C1.23175,45.4153,0,44.184797,0,42.666498L0,17.536401C0,16.019401,1.23175,14.789,2.7473803,14.789z M48.229,4.7119999L51.174999,4.7119999 51.174999,13.610999 48.229,13.610999z M12.697,4.7119999L15.642,4.7119999 15.642,13.610999 12.697,13.610999z M17.98934,0L46.010761,0C46.987385,0,47.778999,0.79034328,47.778999,1.7669096L47.778999,11.254 16.221001,11.254 16.221001,1.7669096C16.221001,0.79034328,17.012718,0,17.98934,0z" Stretch="Uniform" Fill="#FF5389DF" Width="138" Height="138" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"> <Path.RenderTransform> <TransformGroup> <TransformGroup.Children> <RotateTransform Angle="0" /> <ScaleTransform ScaleX="1" ScaleY="1" /> </TransformGroup.Children> </TransformGroup> </Path.RenderTransform> </Path> </Viewbox> </MenuItem.Icon> </MenuItem>
Viewbox宽和高决定了内部矢量path的大小
当然,你说这个Path我们怎么搞,我推荐使用 Metro Studio 3软件,内部可以选择很多图标,选择图标后,可以生成xaml的代码,嵌入即可使用。
下载:链接:http://pan.baidu.com/s/1dD0B7Vz 密码:qnvz
当然,你也可以使用ai作图,然后做成字体,貌似字体然后可以转换成xaml,当然我以前使用过Blend提供的blend design工具,他跟photoshop很像,但是比它弱很多,但是可以画出基本的path了。
当然也有在线生成xaml图标xaml代码的网站:http://www.thexamlproject.com
好了,时间不早了,我早点睡了,下次再啰嗦吧。
老实承认:这篇文章有点凑自己的任务,文章有点水,因为最近挺忙的,到家后也没什么时间写了,还请见谅。关于路由事件的知识,还在赶。
======安徽六安=========www.ayjs.net==========aaronyang========杨洋========www.8mi.me==========