阅读导航
使用简单动画实现抽屉式菜单
使用 .net core 3.1 创建名为 “animatedcolorfulmenu” 的wpf模板项目,添加1个nuget库:materialdesignthemes,版本为最新预览版3.1.0-ci948。
解决方案主要文件目录组织结构:
文件【app.xaml】,在 startupuri 中设置启动的视图【mainwindow.xaml】,并在【application.resources】节点增加 materialdesignthemes库的样式文件:
<application.resources> <resourcedictionary> <resourcedictionary.mergeddictionaries> <resourcedictionary source="pack://application:,,,/materialdesignthemes.wpf;component/themes/materialdesigntheme.light.xaml" /> <resourcedictionary source="pack://application:,,,/materialdesignthemes.wpf;component/themes/materialdesigntheme.defaults.xaml" /> <resourcedictionary source="pack://application:,,,/materialdesigncolors;component/themes/recommended/primary/materialdesigncolor.blue.xaml" /> <resourcedictionary source="pack://application:,,,/materialdesigncolors;component/themes/recommended/accent/materialdesigncolor.indigo.xaml" /> </resourcedictionary.mergeddictionaries> </resourcedictionary> </application.resources>
文件【mainwindow.xaml】,代码不多,主要看左侧菜单,启动时,菜单在显示窗体左侧-150位置;点击展开菜单,使用简单的动画,慢慢呈现在显示窗体左侧,源码如下:
<window x:class="animatedcolorfulmenu.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:materialdesign="http://materialdesigninxaml.net/winfx/xaml/themes" mc:ignorable="d" height="600" width="1080" resizemode="noresize" windowstartuplocation="centerscreen" windowstyle="none"> <window.resources> <storyboard x:key="closemenu"> <doubleanimationusingkeyframes storyboard.targetproperty="(uielement.rendertransform).(transformgroup.children)[3].(translatetransform.x)" storyboard.targetname="gridmenu"> <easingdoublekeyframe keytime="0" value="150"/> <easingdoublekeyframe keytime="0:0:0.5" value="0"/> </doubleanimationusingkeyframes> <doubleanimationusingkeyframes storyboard.targetproperty="(uielement.opacity)" storyboard.targetname="gridbackground"> <easingdoublekeyframe keytime="0" value="1"/> <easingdoublekeyframe keytime="0:0:0.5" value="0"/> </doubleanimationusingkeyframes> </storyboard> <storyboard x:key="openmenu"> <doubleanimationusingkeyframes storyboard.targetproperty="(uielement.rendertransform).(transformgroup.children)[3].(translatetransform.x)" storyboard.targetname="gridmenu"> <easingdoublekeyframe keytime="0" value="0"/> <easingdoublekeyframe keytime="0:0:0.5" value="150"/> </doubleanimationusingkeyframes> <doubleanimationusingkeyframes storyboard.targetproperty="(uielement.opacity)" storyboard.targetname="gridbackground"> <easingdoublekeyframe keytime="0" value="0"/> <easingdoublekeyframe keytime="0:0:0.5" value="1"/> </doubleanimationusingkeyframes> </storyboard> </window.resources> <window.triggers> <eventtrigger routedevent="buttonbase.click" sourcename="buttonclose"> <beginstoryboard x:name="closemenu_beginstoryboard" storyboard="{staticresource closemenu}"/> </eventtrigger> <eventtrigger routedevent="buttonbase.click" sourcename="buttonopen"> <beginstoryboard storyboard="{staticresource openmenu}"/> </eventtrigger> </window.triggers> <grid> <grid x:name="gridbackground" background="#55313131" opacity="0"/> <button x:name="buttonopen" horizontalalignment="left" verticalalignment="top" background="{x:null}" borderbrush="{x:null}" width="30" height="30" padding="0"> <materialdesign:packicon kind="menu" foreground="#ff313131"/> </button> <!--左侧抽屉菜单,默认在显示窗体之外,点击菜单图标再通过简单的动画呈现出来--> <grid x:name="gridmenu" width="150" horizontalalignment="left" margin="-150 0 0 0" background="white" rendertransformorigin="0.5,0.5"> <grid.rendertransform> <transformgroup> <scaletransform/> <skewtransform/> <rotatetransform/> <translatetransform/> </transformgroup> </grid.rendertransform> <stackpanel> <image height="140" source="https://img.dotnet9.com/logo-foot.png" stretch="fill"/> <listview foreground="#ff313131" fontfamily="champagne & limousines" fontsize="18"> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="recycle" width="20" height="20" foreground="gray" margin="5" verticalalignment="center"/> <textblock text="回收" margin="10"/> </stackpanel> </listviewitem> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="helpcircleoutline" width="20" height="20" foreground="#fff08033" margin="5" verticalalignment="center"/> <textblock text="帮助" margin="10"/> </stackpanel> </listviewitem> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="lightbulb" width="20" height="20" foreground="green" margin="5" verticalalignment="center"/> <textblock text="发送反馈" margin="10"/> </stackpanel> </listviewitem> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="heart" width="20" height="20" foreground="#ffd41515" margin="5" verticalalignment="center"/> <textblock text="推荐" margin="10"/> </stackpanel> </listviewitem> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="starcircle" width="20" height="20" foreground="#ffe6a701" margin="5" verticalalignment="center"/> <textblock text="溢价认购" margin="10"/> </stackpanel> </listviewitem> <listviewitem height="45" padding="0"> <stackpanel orientation="horizontal" margin="10 0"> <materialdesign:packicon kind="settings" width="20" height="20" foreground="#ff0069c1" margin="5" verticalalignment="center"/> <textblock text="设置" margin="10"/> </stackpanel> </listviewitem> </listview> </stackpanel> <button x:name="buttonclose" horizontalalignment="right" verticalalignment="top" background="{x:null}" foreground="#ccc" borderbrush="{x:null}" width="30" height="30" padding="0"> <materialdesign:packicon kind="close"/> </button> </grid> </grid> </window>
效果图实现代码在文中已经全部给出,可直接copy,按解决方案目录组织代码文件即可运行。
除非注明,文章均由 dotnet9 整理发布,欢迎转载。
转载请注明本文地址:
欢迎扫描下方二维码关注 dotnet9 的微信公众号,本站会及时推送最新技术文章
时间如流水,只能流去不流回!
点击《【阅读原文】》,本站还有更多技术类文章等着您哦!!!
此刻顺便为我点个《【再看】》可好?
如对本文有疑问, 点击进行留言回复!!
《UnityAPI.Particle粒子》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Particle+lifetime+startColor+立钻哥哥++OK++)
8寸扫码三防军工平板手持终端,双色注塑模/康宁大猩猩玻璃/10点电容屏
信创舆情一线--十五部门印发指导意见进一步促进服务型制造发展
网友评论