微信公眾號: "Dotnet9" ,網站: "Dotnet9" ,問題或建議: "請網站留言" , 如果對您有所幫助: "歡迎贊賞" 。 簡易音樂播放器主界面設計 .NET CORE(C ) WPF開發 閱讀導航 1. 本文背景 2. 代碼實現 3. 本文參考 4. 源碼 1. 本文背景 繼續 Ma ...
簡易音樂播放器主界面設計 - .NET CORE(C#) WPF開發
閱讀導航
- 本文背景
- 代碼實現
- 本文參考
- 源碼
1. 本文背景
繼續 MaterialDesignThemes 開源控制項庫學習,尤其是它的圖標組件,本文設計的音樂播放器主界面設計使用該組件較多。
2. 代碼實現
使用 .NET CORE 3.1 創建名為 “Player” 的WPF模板項目,添加1個Nuget庫:MaterialDesignThemes.3.1.0-ci981。
解決方案主要文件目錄組織結構:
- Player
- App.xaml
- MainWindow.xaml
- MainWindow.xaml.cs
2.1 App.xaml文件引入樣式
文件【App.xaml】,在 StartupUri 中設置啟動的視圖【MainWindow.xaml】,併在【Application.Resources】節點增加 MaterialDesignThemes庫的樣式文件:
<Application x:Class="Player.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Player"
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>
2.2 MainWindow.xaml音樂播放器主窗體
文件【MainWindow.xaml】,設計主界面,源碼如下:
<Window x:Class="Player.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MouseLeftButtonDown="MoveWindow_MouseLeftButtonDown"
Title="播放器" Height="500" Width="300" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Foreground="LightSteelBlue">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid x:Name="Grid">
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Fill="Transparent" />
<Border x:Name="Rectangle1" CornerRadius="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Background="{TemplateBinding Background}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Tag" Value="Horizontal">
<Setter TargetName="Rectangle1" Property="Width" Value="Auto" />
<Setter TargetName="Rectangle1" Property="Height" Value="7" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--ScrollBars-->
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled" Value="false" />
<Setter Property="Foreground" Value="LightGray" />
<Setter Property="Background" Value="DarkGray" />
<Setter Property="Width" Value="10" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="GridRoot" Width="19" Background="{x:Null}">
<Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false">
<Track.Thumb>
<Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" />
</Track.Thumb>
<Track.IncreaseRepeatButton>
<RepeatButton x:Name="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />
</Track.IncreaseRepeatButton>
<Track.DecreaseRepeatButton>
<RepeatButton x:Name="PageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" />
</Track.DecreaseRepeatButton>
</Track>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="Thumb" Property="IsMouseOver" Value="true">
<Setter Value="{DynamicResource ButtonSelectBrush}" TargetName="Thumb" Property="Background" />
</Trigger>
<Trigger SourceName="Thumb" Property="IsDragging" Value="true">
<Setter Value="{DynamicResource DarkBrush}" TargetName="Thumb" Property="Background" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Orientation" Value="Horizontal">
<Setter TargetName="GridRoot" Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
<Setter TargetName="PART_Track" Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="12" />
<Setter TargetName="Thumb" Property="Tag" Value="Horizontal" />
<Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />
<Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid Background="Black">
<Grid Background="#44444444" Margin="10" Height="300" VerticalAlignment="Top">
<Grid VerticalAlignment="Top">
<Button x:Name="ButtonFechar" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Right" Width="20" Height="20" Margin="10,0" Click="ButtonFechar_Click">
<materialDesign:PackIcon Kind="Close" VerticalAlignment="Center" Width="20" Height="20">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Left" Width="20" Height="20" Margin="10,0">
<materialDesign:PackIcon Kind="Plus" VerticalAlignment="Center" Width="20" Height="20">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<TextBlock Text="野狼disco" Margin="5" HorizontalAlignment="Center" />
</Grid>
<TextBlock Text="寶石Gem & 陳偉霆" Margin="25" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<Grid VerticalAlignment="Top" Margin="0,50">
<Ellipse Width="150" Height="150" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop x:Name="c1" Color="Black" Offset="0.71"/>
<GradientStop Color="#FFB85219"/>
<GradientStop x:Name="c2" Color="#FEB14F18" Offset="0.6"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
<Ellipse Width="145" Height="145" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="#FF0C0604" Offset="1"/>
<GradientStop Color="#FF210900" Offset="0.047"/>
<GradientStop Color="#FF1D0800" Offset="0.602"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse Width="135" Height="135">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131233622.png" Stretch="Uniform"/>
</Ellipse.Fill>
</Ellipse>
<Ellipse Fill="#7F000000" Width="135" Height="135"/>
</Grid>
<Grid VerticalAlignment="Bottom" Margin="5">
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Left">
<materialDesign:PackIcon Kind="RotateRight" VerticalAlignment="Center" Width="30" Height="30">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<Button x:Name="Anterior" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Left" Margin="50,0" Click="Anterior_Click">
<materialDesign:PackIcon Kind="ChevronLeft" VerticalAlignment="Center" Width="30" Height="30">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="#00000000" BorderBrush="#70702222" HorizontalAlignment="Center">
<Button.Effect>
<DropShadowEffect Color="#FFD67619" RenderingBias="Quality" BlurRadius="40" Direction="0"/>
</Button.Effect>
<materialDesign:PackIcon Kind="Pause" VerticalAlignment="Center" Width="30" Height="30">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<Button x:Name="Proxima" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Right" Margin="50,0" Click="Proxima_Click">
<materialDesign:PackIcon Kind="ChevronRight" VerticalAlignment="Center" Width="30" Height="30">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Right">
<materialDesign:PackIcon Kind="ShuffleVariant" VerticalAlignment="Center" Width="30" Height="30">
<materialDesign:PackIcon.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFD69016"/>
<GradientStop Color="#FFD6511E" Offset="0.747"/>
<GradientStop Color="#FF9B330D" Offset="0.807"/>
</LinearGradientBrush>
</materialDesign:PackIcon.Foreground>
</materialDesign:PackIcon>
</Button>
</Grid>
</Grid>
<ListView VerticalAlignment="Bottom" Height="150" Margin="5" Foreground="LightSteelBlue">
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="01" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131234152.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="我的夢 - 張靚穎" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2016" VerticalAlignment="Center"/>
<TextBlock Text="4:04" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="02" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131234746.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="涼涼 – 楊宗緯 & 張碧晨" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2017" VerticalAlignment="Center"/>
<TextBlock Text="3:20" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="03" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131235020.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="如果這就是愛情 – 張靚穎" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2017" VerticalAlignment="Center"/>
<TextBlock Text="2:57" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="04" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131235218.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="女兒國 – 張靚穎" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2016" VerticalAlignment="Center"/>
<TextBlock Text="3:28" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="05" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131235356.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="另一個天堂 – 王力巨集 & 張靚穎" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2017" VerticalAlignment="Center"/>
<TextBlock Text="3:22" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="06" Margin="5" VerticalAlignment="Center"/>
<Ellipse Width="30" Height="30">
<Ellipse.Fill>
<ImageBrush ImageSource="https://dotnet9.com/wp-content/uploads/2020/01/20200131235528.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="我們說好的 – 張靚穎" Margin="10,0" VerticalAlignment="Center" Width="100" TextTrimming="CharacterEllipsis"/>
<TextBlock Text="2017" VerticalAlignment="Center"/>
<TextBlock Text="4:02" Margin="10,0" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
</ListView>
</Grid>
</Window>
簡單說明:
- 界面中按鈕使用開源控制項庫MD的【PackIcon】組件,統一風格使用了自定義前景色【Foreground】。
- 列表控制項【ListView】用於展示音樂播放列表,方便演示,每一項寫死的,實際使用需要封裝成模板,方便MVVM數據綁定。
- 列表控制項【ListView】的豎直滾動條樣式進行了修改,可看資源定義,改為了整體和黑色背景比較搭配的白色。
下麵是後臺代碼:文件【MainWindow.xaml.cs】,關閉窗體、窗體移動、上一首及下一首按鈕簡單點擊等事件處理,因為是演示事例,所以寫的簡單。
using System.Windows;
using System.Windows.Input;
namespace Player
{
/// <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 Proxima_Click(object sender, RoutedEventArgs e)
{
if (c1.Offset >= 0)
{
c1.Offset -= 0.01;
c2.Offset -= 0.01;
}
else
{
c1.Offset = 1;
c2.Offset = 0.89;
}
}
private void Anterior_Click(object sender, RoutedEventArgs e)
{
if (c2.Offset <= 1)
{
c1.Offset += 0.01;
c2.Offset += 0.01;
}
else
{
c1.Offset = 0.11;
c2.Offset = 0;
}
}
private void MoveWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
}
}
3.本文參考
4.源碼
演示代碼已全部奉上,為了方便演示,代碼中的圖片使用本站外鏈,代碼可直接拷貝並按代碼結構組織編譯即可運行。
可運行Demo點擊即可下載: 【音樂播放器】。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
轉載請註明本文地址:https://dotnet9.com/7981.html
歡迎掃描下方二維碼關註 Dotnet9 的微信公眾號,本站會及時推送最新技術文章
時間如流水,只能流去不流回!
點擊《【閱讀原文】》,本站還有更多技術類文章等著您哦!!!
此刻順便為我點個《【再看】》可好?