閱讀導航
本文背景程式碼實現本文參考1.本文背景聊天軟體大家用的比較多,用WPF做個聯絡人列表試試。
本文效果如下:
聯絡人列表
2.程式碼實現使用 .Net CORE 3.1 建立名為 “Chat” 的WPF專案,新增 MaterialDesignThemes(3.0.1)、MaterialDesignColors(1.2.2)兩個Nuget庫,文中部分圖片可在文末視訊配套原始碼中下載。
2.1 引入MD控制元件樣式檔案使用MD控制元件的常規操作,需要在App.xaml中引入4個樣式檔案
<Application x:Class="Chat.App" xmlns="/file/2020/01/11/20200111184757_583.jpg.aspx xmlns:x="/file/2020/01/11/20200111184759_585.jpg.aspx StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Green.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>
2.2 介面佈局純粹的佈局程式碼:
<Window x:Class="Chat.MainWindow" xmlns="/file/2020/01/11/20200111184757_583.jpg.aspx xmlns:materialDesign="/file/2020/01/11/20200111184800_586.jpg xmlns:d="/file/2020/01/11/20200111184800_587.jpg.aspx xmlns:x="/file/2020/01/11/20200111184759_585.jpg.aspx xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Height="600" Width="1080" ResizeMode="NoResize" MouseLeftButtonDown="Window_MouseLeftButtonDown" WindowStartupLocation="CenterScreen" WindowStyle="None"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="270"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="270"/> </Grid.ColumnDefinitions> <Grid Grid.Column="1" Background="#FFE4E4E4"/> <StackPanel Grid.Column="0" Background="{StaticResource PrimaryHueDarkBrush}"> <StackPanel Orientation="Horizontal" Background="White"> <Image Width="210" Height="80" Source="Assets/logo.png"/> <Button Style="{StaticResource MaterialDesignFlatButton}"> <materialDesign:PackIcon Kind="PlusCircle" Width="24" Height="24"/> </Button> </StackPanel> <TextBox Margin="20 10" Style="{StaticResource MaterialDesignFloatingHintTextBox}" materialDesign:HintAssist.Hint="搜尋" Foreground="White"/> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button Style="{StaticResource MaterialDesignFlatButton}" Grid.Column="0"> <materialDesign:PackIcon Kind="History" Foreground="White"/> </Button> <Button Style="{StaticResource MaterialDesignFlatButton}" Grid.Column="1"> <materialDesign:PackIcon Kind="People" Foreground="White"/> </Button> <Button Style="{StaticResource MaterialDesignFlatButton}" Grid.Column="2"> <materialDesign:PackIcon Kind="Contacts" Foreground="White"/> </Button> <Button Style="{StaticResource MaterialDesignFlatButton}" Grid.Column="3"> <materialDesign:PackIcon Kind="Archive" Foreground="White"/> </Button> </Grid> <ListView> <ListViewItem HorizontalAlignment="Stretch"> <Grid HorizontalAlignment="Center" Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"/> <ColumnDefinition Width="150"/> <ColumnDefinition Width="50*"/> </Grid.ColumnDefinitions> <Border Width="40" Height="40" CornerRadius="25" BorderBrush="White" BorderThickness="0.6"> <Border.Background> <ImageBrush ImageSource="/file/2020/01/11/20200111184803_589.jpg.png"/> </Border.Background> </Border> <Border Width="10" Height="10" VerticalAlignment="Bottom" Margin="5" HorizontalAlignment="Right" CornerRadius="15" Background="LightGreen"/> <StackPanel Grid.Column="1"> <TextBlock Text="Dotnet9.com" Margin="10 0"/> <TextBlock Text="一個熱衷於網際網路分享精神的程式設計師的網站!" Margin="10 0" TextTrimming="CharacterEllipsis" Opacity="0.6" FontSize="11"/> </StackPanel> <Border Grid.Column="2" Width="20" Height="20" CornerRadius="15" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"> <TextBlock FontSize="11" Text="9" Foreground="{StaticResource PrimaryHueDarkBrush}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </Grid> </ListViewItem> </ListView> </StackPanel> </Grid></Window>
2.2.3 窗體拖動後臺程式碼
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){ DragMove();}
本文略短,原作者視訊也有22分鐘,看視訊學習吧。
3.參考學習視訊:C# WPF Design UI – 1/3 – Contact List視訊配套原始碼:Chat除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
最新評論