.NET CORE(C#) WPF 方便的实现用户控件切换(祝大家新年快乐)
.NET CORE(C#) WPF 方便的实现用户控件切换(祝大家新年快乐)
快到2020年了,祝大家新年快乐,今年2019最后一更,谢谢大家支持!
阅读导航
- 本文背景
- 代码实现
- 本文参考
- 源码
1. 本文背景
一个系统主界面,放上一个菜单,点击菜单在客户区切换不同的展示界面,这是很常规的设计,见下面展示效果图:

左侧一个菜单,点击菜单,右侧切换界面,界面切换动画使用MD控件的组件实现(自己使用动画也能实现)。
2. 代码实现
使用 .NET CORE 3.1 创建名为 “MenuChange” 的WPF模板项目,添加1个Nuget库:MaterialDesignThemes,版本为最新预览版3.1.0-ci948。
解决方案主要文件目录组织结构:
- AnimatedMenu
- App.xaml
- MainWindow.xaml
- MainWindow.xaml.cs
- ..[数个演示模块用户控件]
2.1 引入样式
文件【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>
2.2 演示窗体
文件【MainWindow.xaml】,布局代码、动画代码都在此文件中,源码如下:
<Window x:Class="MenuChange.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="600" Width="1024" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="None" >
<Grid Background="#FFEEEEEE" MouseDown="Grid_MouseDown">
<StackPanel VerticalAlignment="Top">
<Grid Background="#FF0069C0" Height="10"/>
<Grid Margin="5">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="Dotnet9.com" VerticalAlignment="Center" FontSize="20" FontFamily="Champagne & Limousines" Margin="20 0"/>
<Button Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray">
<materialDesign:PackIcon Kind="Wechat"/>
</Button>
<Button Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray">
<materialDesign:PackIcon Kind="Qqchat"/>
</Button>
<Button Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray">
<materialDesign:PackIcon Kind="GithubBox"/>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="10 0">
<Button Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray">
<materialDesign:PackIcon Kind="BellOutline"/>
</Button>
<Button Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray" Margin="0 0 10 0">
<materialDesign:PackIcon Kind="Settings"/>
</Button>
<Button x:Name="ButtonFechar" Width="30" Height="30" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Foreground="Gray" Click="ButtonFechar_Click">
<materialDesign:PackIcon Kind="Close"/>
</Button>
</StackPanel>
</Grid>
</StackPanel>
<Grid Margin="250 55 0 0">
<Grid x:Name="GridPrincipal" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<Grid Width="250" HorizontalAlignment="Left" Background="#FF222222">
<materialDesign:TransitioningContent x:Name="TrainsitionigContentSlide" OpeningEffect="{materialDesign:TransitionEffect SlideInFromLeft, Duration=0:0:0.2}">
<Grid x:Name="GridCursor" Margin="0 100 0 0" Background="#FF0069C0" Width="10" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top"/>
</materialDesign:TransitioningContent>
<Image Source="https://img.dotnet9.com/logo-head.png" VerticalAlignment="Top"/>
<Image Source="https://img.dotnet9.com/logo-foot.png" VerticalAlignment="Bottom"/>
<ListView x:Name="ListViewMenu" Margin="0 100" Foreground="LightGray" FontFamily="Champagne & Limousines" FontSize="18" SelectionChanged="ListViewMenu_SelectionChanged" SelectedIndex="0">
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Home" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="首页" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="LanguageCsharp" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="WPF" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="LanguageCsharp" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="Winform" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="LanguageCsharp" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="ASP.NET CORE" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="LanguageCsharp" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="Xamarin.Forms" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="LanguageCpp" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="C++" FontSize="17" VerticalAlignment="Center" Margin="20 0"/>
</StackPanel>
</ListViewItem>
</ListView>
</Grid>
</Grid>
</Window>
简单说明下:
- "GridPrincipal" 作为客户区子模块界面容器,展示新界面时,先移除旧用户控件,再添加新用户控件(站长以前使用时,是遍历容器中所有用户界面,对非选择用户控件作隐藏操作,然后添加新用户控件或者对已存在的被选择用户控件作显示操作)。
- 左侧菜单项使用 "ListView" 进行布局,实际开发需要运用模板,使用MVVM做成动态菜单,方便扩展。
- 右侧切换的子模块界面应该使用Prism或者其他框架(自已实现的模块dll)实现的子界面,方便动态扩展(需要和左侧菜单项进行关联)。
文件【MainWindow.xaml.cs】,后台关闭窗体、菜单点击切换子模块界面、窗体移动等事件处理:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MenuChange
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonFechar_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = ListViewMenu.SelectedIndex;
MoveCursorMenu(index);
switch (index)
{
case 0:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlMain());
break;
case 1:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlWPF());
break;
case 2:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlWinform());
break;
case 3:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlASPNETCORE());
break;
case 4:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlXamarinForms());
break;
case 5:
GridPrincipal.Children.Clear();
GridPrincipal.Children.Add(new UserControlCPP());
break;
default:
break;
}
}
private void MoveCursorMenu(int index)
{
TrainsitionigContentSlide.OnApplyTemplate();
GridCursor.Margin = new Thickness(0, (100 + (60 * index)), 0, 0);
}
}
}
方便演示,点击菜单,切换子用户控件时时写死的,见上面的说明,左侧菜单及右侧切换的子用户控件需要进行关联配置,方便扩展,建议使用Prism的模块化开发。
2.3 演示主模块
文件【UserControlMain.xaml】,只展示其中一个子模块用户控件吧,其他类似,文末有源码、可运行Demo供下载参考。
注意: 需要使用MD控件的 TransitioningContent 组件将用户控件可视区域包裹起来,用于使用MD的移动切换动画,其中 OpeningEffect 见名思意,即是展示此用户控件时,动画如何播放,见下面代码,SlideInFromLeft 是指示展示时由左向右缓动,相反 SlideInFromRight即是由右向左缓动,其他属性可查阅MD官网或者Demo研究。
<UserControl x:Class="MenuChange.UserControlMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="auto" Width="auto">
<Grid>
<materialDesign:TransitioningContent x:Name="TrainsitionigContentSlide" OpeningEffect="{materialDesign:TransitionEffect SlideInFromLeft, Duration=0:0:0.8}">
<StackPanel Background="#33ff0000">
<TextBox HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="White" Text="https://dotnet9.com"/>
<Image Source="https://img.dotnet9.com/20200124165746.png"/>
</StackPanel>
</materialDesign:TransitioningContent>
</Grid>
</UserControl>
已奉上关键代码,全部代码文末有下载链接...
3.本文参考
4.源码
站长方便演示,文中的图片使用的本站外链图片:
除非注明,文章均由 Dotnet9 整理发布,欢迎转载。
转载请注明本文地址:https://dotnet9.com/7743.html
欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章
时间如流水,只能流去不流回!
点击《【阅读原文】》,本站还有更多技术类文章等着您哦!!!
此刻顺便为我点个《【再看】》可好?
.NET CORE(C#) WPF 方便的实现用户控件切换(祝大家新年快乐)的更多相关文章
- 在wpf窗体上添加用户控件
1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...
- wpf 水波进度条 用户控件
之前看公司web前端做了个 圆形的水波纹 进度条,就想用wpf 做一个,奈何自己太菜 一直做不出来,在看过 “普通的地球人” 的 “ WPF实现三星手机充电界面 博客之后 我也来照葫芦画个瓢. 废话不 ...
- WPF MVVM 用户控件完成分页
项目中经常会有分页查询的情况,在WPF中我们可以通过用户控件完成分页 一下为分页控件的页面代码, <UserControl x:Class="Foundation.UCtrl.Next ...
- 浅尝辄止WPF自定义用户控件(实现颜色调制器)
主要利用用户控件实现一个自定义的颜色调制控件,实现一个小小的功能,具体实现界面如下. 首先自己新建一个wpf的用户控件类,我就放在我的wpf项目的一个文件夹下面,因为是一个很小的东西,所以就没有用mv ...
- WPF自定义控件(五)の用户控件(完结)
用户控件,WPF中是继承自UserControl的控件,我们可以在里面融合我们的业务逻辑. 示例:(一个厌恶选择的用户控件) 后端: using iMicClassBase; using iMicCl ...
- WPF用户控件库 嵌入外部(VLC)exe
综合网上资源完成的自己的第一篇博客 ------------------------------------------------------------------------ 网上类似的贴子挺多 ...
- WPF 用户控件嵌入网页
WPF使用用户控件嵌入网页,直接使用WebBrowser或Frame会产生报错,报错信息如下: 1.使用WebBrowser,<WebBrowser Source="http://19 ...
- 创建WPF用户控件
wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢? ...
- WPF Prism MVVM 中 弹出新窗体. 放入用户控件
原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...
随机推荐
- Windows下安装Hadoop、Spark和HBase
1.Hadoop 安装Hadoop:下载hadoop-2.7.1.tar.gz,并解压到你想要的目录下,我放在D:\Library\hadoop-2.7.1. 配置Hadoop环境变量:HADOOP_ ...
- Object-c的字符串处理常用方法
Object-c的字符串处理常用方法 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { ...
- 《ASP.NET Core 高性能系列》关于.NET Core的配置信息的若干事项
1.配置文件的相关闲话 Core自身对于配置文件不是必须品,但由上文分析可知ASP.NET Core默认采用appsettings.json作为配置文件,关于配置信息的优先等级 命令行>环境变量 ...
- Ubuntu 入门笔记(1)
在阿里云上申请了一个云服务器,开始学习Linux.我选择的是Ubuntu 14.04 ,在登录时就绕了我好长时间,输入用户名是有显示的,但是输入密码就没有反应了,查找了之后才发现原来这是Ubuntu ...
- webpack--运行npm run dev自动打开浏览器运行首页的两种方式以及热加载
作为开发人员,我们在修改了代码之后,在vscode终端运行npm run dev指令后,希望它可以自动打开浏览器方便我们调试,有两种方式可以实现: 自动打开浏览器的两种方式: 方式一: 1.webpa ...
- linux下面误删root里面的文件夹 恢复方法
手残吧 /root/ 里面的文件删除了. .mkdir /root cp -a /etc/skel/.[!.]* /root 主要是吧 /etc/skel/里面的文件拷贝回去就行了~~~哈.. 转自: ...
- java加解密算法--DES
ECB import sun.misc.BASE64Decoder; import javax.crypto.*; import javax.crypto.spec.DESKeySpec; impor ...
- 手把手实操教程!使用k3s运行轻量级VM
前 言 k3s作为轻量级的Kubernetes发行版,运行容器是基本功能.VM的管理原本是IaaS平台的基本能力,随着Kubernetes的不断发展,VM也可以纳入其管理体系.结合Container和 ...
- pyinstaller相关问题 & pygame文件打包成exe文件 & 武装飞船 & 飞机大战
自己照书写了一个飞机大战游戏的python程序,想把它打包成一个exe文件,在查阅相关教程并经过数次尝试后终于成功. 安装打包应用 pyinstaller 在cmd命令窗口下pip install p ...
- Visual Studio 2015 配置 Python 环境
Visual Studio 2015可以在安装时选择安装Python环境,首次使用VS2015执行python时需要配置环境变量: 配置VS2015的环境前需要先下载Python并安装: https: ...
