微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言

如果对您有所帮助:欢迎赞赏

.NET CORE(C#) WPF 方便的实现用户控件切换(祝大家新年快乐)

快到2020年了,祝大家新年快乐,今年2019最后一更,谢谢大家支持!

阅读导航

  1. 本文背景
  2. 代码实现
  3. 本文参考
  4. 源码

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 &amp; 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 &amp; 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>

简单说明下:

  1. "GridPrincipal" 作为客户区子模块界面容器,展示新界面时,先移除旧用户控件,再添加新用户控件(站长以前使用时,是遍历容器中所有用户界面,对非选择用户控件作隐藏操作,然后添加新用户控件或者对已存在的被选择用户控件作显示操作)。
  2. 左侧菜单项使用 "ListView" 进行布局,实际开发需要运用模板,使用MVVM做成动态菜单,方便扩展。
  3. 右侧切换的子模块界面应该使用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.本文参考

  1. 视频一:C# WPF Material Design UI: Fast Food Sales,配套源码:Pizzaria1
  2. C# WPF开源控件库《MaterialDesignInXAML》

4.源码

站长方便演示,文中的图片使用的本站外链图片:

演示Demo下载


除非注明,文章均由 Dotnet9 整理发布,欢迎转载。


转载请注明本文地址:https://dotnet9.com/7743.html


欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章



时间如流水,只能流去不流回!

点击《【阅读原文】》,本站还有更多技术类文章等着您哦!!!

此刻顺便为我点个《【再看】》可好?

.NET CORE(C#) WPF 方便的实现用户控件切换(祝大家新年快乐)的更多相关文章

  1. 在wpf窗体上添加用户控件

    1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...

  2. wpf 水波进度条 用户控件

    之前看公司web前端做了个 圆形的水波纹 进度条,就想用wpf 做一个,奈何自己太菜 一直做不出来,在看过 “普通的地球人” 的 “ WPF实现三星手机充电界面 博客之后 我也来照葫芦画个瓢. 废话不 ...

  3. WPF MVVM 用户控件完成分页

    项目中经常会有分页查询的情况,在WPF中我们可以通过用户控件完成分页 一下为分页控件的页面代码, <UserControl x:Class="Foundation.UCtrl.Next ...

  4. 浅尝辄止WPF自定义用户控件(实现颜色调制器)

    主要利用用户控件实现一个自定义的颜色调制控件,实现一个小小的功能,具体实现界面如下. 首先自己新建一个wpf的用户控件类,我就放在我的wpf项目的一个文件夹下面,因为是一个很小的东西,所以就没有用mv ...

  5. WPF自定义控件(五)の用户控件(完结)

    用户控件,WPF中是继承自UserControl的控件,我们可以在里面融合我们的业务逻辑. 示例:(一个厌恶选择的用户控件) 后端: using iMicClassBase; using iMicCl ...

  6. WPF用户控件库 嵌入外部(VLC)exe

    综合网上资源完成的自己的第一篇博客 ------------------------------------------------------------------------ 网上类似的贴子挺多 ...

  7. WPF 用户控件嵌入网页

    WPF使用用户控件嵌入网页,直接使用WebBrowser或Frame会产生报错,报错信息如下: 1.使用WebBrowser,<WebBrowser Source="http://19 ...

  8. 创建WPF用户控件

    wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢? ...

  9. WPF Prism MVVM 中 弹出新窗体. 放入用户控件

    原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...

随机推荐

  1. c#数字图像处理(十一)图像旋转

    如果平面上的点绕原点逆时针旋转θº,则其坐标变换公式为: x'=xcosθ+ysinθ   y=-xsinθ+ycosθ 其中,(x, y)为原图坐标,(x’, y’)为旋转后的坐标.它的逆变换公式为 ...

  2. 异想家Ubuntu安装的软件

    [替换国内源] https://developer.aliyun.com/mirror/ubuntu 我提供一个下载,方便第一次安装懒得敲命令: https://jfz.me/16.04/source ...

  3. [HAOI2015]树上操作(树链剖分)

    [HAOI2015]树上操作(luogu) Description 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增 ...

  4. URL各部分详解

    就以下面这个URL为例,介绍下普通URL的各部分组成 http://www.aspxfans.com:8080/news/index.asp?boardID=5&ID=24618&pa ...

  5. PyCharm安装和使用教程(Windows系统)

    说明: PyCharm 是一款功能强大的 Python 编辑器, 本文简单的介绍下PyCharm 在 Windows下是如何安装的. PyCharm 的下载地址:http://www.jetbrain ...

  6. 自定义HttpMessageConverter实现RestTemplate的exchange方法返回自定义格式数据

    一 概述 实现如下效果代码,且可正常获取到返回数据: ResponseEntity<JsonObject> resEntity = restTemplate .exchange(url, ...

  7. 使用 web3D 技术的风力发电场展示

    前言    风能是一种开发中的洁净能源,它取之不尽.用之不竭.当然,建风力发电场首先应考虑气象条件和社会自然条件.近年来,我国海上和陆上风电发展迅猛.海水.陆地为我们的风力发电提供了很好地质保障.正是 ...

  8. 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式

    在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...

  9. cd命令和roscd命令的区别,并解决环境变量问题

    cd命令和roscd命令都是切换到指定目录的命令.不同的是,cd是Linux系统的命令,在使用时必须指定目标目录的完整路径:而roscd是ros系统中的命令,它可以直接切换到指定目录(ros系统中的软 ...

  10. data structure test

    1.设计算法,对带头结点的单链表实现就地逆置.并给出单链表的存储结构(数据类型)的定义. #include <iostream> #include <cstdlib> #inc ...