溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 中怎么利用WPF實(shí)現(xiàn)一個(gè)遮罩對(duì)話框

發(fā)布時(shí)間:2021-07-07 15:11:55 來源:億速云 閱讀:936 作者:Leah 欄目:大數(shù)據(jù)

這篇文章給大家介紹C# 中怎么利用WPF實(shí)現(xiàn)一個(gè)遮罩對(duì)話框,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

 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圖標(biāo)、左側(cè)導(dǎo)航菜單等,下面即是全部代碼。

<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">
                           允許應(yīng)用程序訪問您計(jì)算機(jī)上的照片、媒體和文件?
                       </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>

重點(diǎn)講解:

  • materialDesign:DialogHost:設(shè)置遮罩對(duì)話框覆蓋的地方,即彈出遮罩對(duì)話框后,背后有陰影的位置。

  • materialDesign:DialogHost.DialogContent:對(duì)話框內(nèi)容,即彈出的對(duì)話框配置

后臺(tái)有個(gè)拖動(dòng)窗體代碼:

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
   DragMove();
}

關(guān)于C# 中怎么利用WPF實(shí)現(xiàn)一個(gè)遮罩對(duì)話框就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

wpf
AI