閱讀導航:app
- 1、先看效果
- 2、本文背景
- 3、代碼實現
- 4、文章參考
- 5、代碼下載
1、先看效果
2、本文背景
YouTube Design com WPF 大神處習得,經常使用的遮罩對話框實現,使用的開源 C# WPF控件庫 MaterialDesignInXAML ,本站曾有介紹:開源C# WPF控件庫《MaterialDesignInXAML》。ide
3、代碼實現
3.1 添加Nuget庫
站長使用.Net Core 3.1建立的WPF工程,建立「ScreenOverlayMessage」解決方案後,須要添加兩個Nuget庫:MaterialDesignThemes和MaterialDesignColors,上圖的效果是使用該控件庫實現的,很是強大。佈局
![C# WPF抽屜效果實現(C# WPF Material Design UI: Navigation Drawer & PopUp Menu)](http://static.javashuo.com/static/loading.gif)
3.2 工程結構
不須要截圖,只修改了兩個文件,App.xaml添加MD控件4個樣式文件,MainWindow主窗口實現效果。學習
3.3 App.xaml引入MD控件樣式
<Application x:Class="DropDownMenu.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DropDownMenu" StartupUri="MainWindow.xaml"> <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> </Application>
3.4 主窗體
MainWindow.xaml,總體佈局,看上圖加上下面的界面代碼,添加界面左上角logo圖標、左側導航菜單等,下面便是所有代碼。ui
<Window x:Class="ScreenOverlayMessage.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:local="clr-namespace:ScreenOverlayMessage" mc:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" MouseDown="Window_MouseDown" Title="MainWindow" Height="576" Width="1024" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="#FF423A3A"> <Grid> <materialDesign:DialogHost BorderBrush="{DynamicResource MaterialDesignDivider}"> <materialDesign:DialogHost.DialogContent> <Grid Width="300" Height="150" HorizontalAlignment="Center"> <StackPanel Orientation="Horizontal" Margin="15"> <materialDesign:PackIcon Kind="Folder" Foreground="{StaticResource PrimaryHueMidBrush}" Width="50" Height="50"/> <TextBlock Foreground="Gray" Width="200" Margin="15 5" TextWrapping="Wrap"> 容許應用程序訪問您計算機上的照片、媒體和文件? </TextBlock> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="15"> <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center"> 拒絕 </Button> <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center"> 容許 </Button> </StackPanel> </Grid> </materialDesign:DialogHost.DialogContent> <Grid> <StackPanel Width="200" HorizontalAlignment="Left" Background="#FF472076"> <Grid Height="150" Background="White"> <Image Source="https://img.dotnet9.com/logo.png"/> </Grid> <Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"> <StackPanel Orientation="Horizontal" Height="30"> <materialDesign:PackIcon Kind="PhotoAlbum" Width="20" Height="20" VerticalAlignment="Center"/> <TextBlock Text="照片" Margin="20 0" FontSize="15" VerticalAlignment="Center"/> </StackPanel> </Button> <Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"> <StackPanel Orientation="Horizontal" Height="30"> <materialDesign:PackIcon Kind="Music" Width="20" Height="20" VerticalAlignment="Center"/> <TextBlock Text="音樂" Margin="20 0" FontSize="15" VerticalAlignment="Center"/> </StackPanel> </Button> </StackPanel> </Grid> </materialDesign:DialogHost> </Grid> </Window>
重點講解:spa
- materialDesign:DialogHost:設置遮罩對話框覆蓋的地方,即彈出遮罩對話框後,背後有陰影的位置。
- materialDesign:DialogHost.DialogContent:對話框內容,即彈出的對話框配置
後臺有個拖動窗體代碼:.net
private void Window_MouseDown(object sender, MouseButtonEventArgs e) { DragMove(); }
4、文章參考
建議直接打開大神視頻學習,他的YouTube上還有不少代碼視頻哦,參考:
參考視頻: Design com WPF: https://www.youtube.com/watch?v=Dwi9o3K73XM
5、代碼下載
文章中代碼已經所有貼出。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
轉載請註明本文地址:https://dotnet9.com/6769.html
歡迎掃描下方二維碼關注 Dotnet9 的微信公衆號,本站會及時推送最新技術文章(微信公衆號「dotnet9_com」):
若有收穫,請大力轉發,給Dotnet9贊助和支持,謝謝你們對dotnet技術的關注和支持 。