跨平臺WPF音樂商店應用程序

目錄

一 簡介

二 設計思路

三 源碼


一 簡介

支持在線檢索音樂,支持實時瀏覽當前收藏的音樂及音樂數據的持久化。

二 設計思路

采用MVVM架構,前后端分離,子界面彈出始終位于主界面的中心。

三 源碼

視窗引導啟動源碼:

namespace Avalonia.MusicStore
{public class ViewLocator : IDataTemplate{public Control? Build(object? data){if (data is null)return null;var name = data.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);var type = Type.GetType(name);if (type != null){var control = (Control)Activator.CreateInstance(type)!;control.DataContext = data;return control;}return new TextBlock { Text = "Not Found: " + name };}public bool Match(object? data){return data is ViewModelBase;}}
}using Avalonia;
using Avalonia.ReactiveUI;
using System;namespace Avalonia.MusicStore
{internal sealed class Program{// Initialization code. Don't use any Avalonia, third-party APIs or any// SynchronizationContext-reliant code before AppMain is called: things aren't initialized// yet and stuff might break.[STAThread]public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);// Avalonia configuration, don't remove; also used by visual designer.public static AppBuilder BuildAvaloniaApp()=> AppBuilder.Configure<App>().UsePlatformDetect().WithInterFont().LogToTrace().UseReactiveUI();}
}

模型源碼:

using iTunesSearch.Library;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;namespace Avalonia.MusicStore.Models
{public class Album{private static iTunesSearchManager s_SearchManager = new();public string Artist { get; set; }public string Title { get; set; }public string CoverUrl { get; set; }public Album(string artist, string title, string coverUrl){Artist = artist;Title = title;CoverUrl = coverUrl;}public static async Task<IEnumerable<Album>> SearchAsync(string searchTerm){var query = await s_SearchManager.GetAlbumsAsync(searchTerm).ConfigureAwait(false);return query.Albums.Select(x =>new Album(x.ArtistName, x.CollectionName,x.ArtworkUrl100.Replace("100x100bb", "600x600bb")));}private static HttpClient s_httpClient = new();private string CachePath => $"./Cache/{Artist} - {Title}";public async Task<Stream> LoadCoverBitmapAsync(){if (File.Exists(CachePath + ".bmp")){return File.OpenRead(CachePath + ".bmp");}else{var data = await s_httpClient.GetByteArrayAsync(CoverUrl);return new MemoryStream(data);}}public async Task SaveAsync(){if (!Directory.Exists("./Cache")){Directory.CreateDirectory("./Cache");}using (var fs = File.OpenWrite(CachePath)){await SaveToStreamAsync(this, fs);}}public Stream SaveCoverBitmapStream(){return File.OpenWrite(CachePath + ".bmp");}private static async Task SaveToStreamAsync(Album data, Stream stream){await JsonSerializer.SerializeAsync(stream, data).ConfigureAwait(false);}public static async Task<Album> LoadFromStream(Stream stream){return (await JsonSerializer.DeserializeAsync<Album>(stream).ConfigureAwait(false))!;}public static async Task<IEnumerable<Album>> LoadCachedAsync(){if (!Directory.Exists("./Cache")){Directory.CreateDirectory("./Cache");}var results = new List<Album>();foreach (var file in Directory.EnumerateFiles("./Cache")){if (!string.IsNullOrWhiteSpace(new DirectoryInfo(file).Extension)) continue;await using var fs = File.OpenRead(file);results.Add(await Album.LoadFromStream(fs).ConfigureAwait(false));}return results;}}
}

模型視圖源碼:

using Avalonia.Media.Imaging;
using Avalonia.MusicStore.Models;
using ReactiveUI;
using System.Threading.Tasks;namespace Avalonia.MusicStore.ViewModels
{public class AlbumViewModel : ViewModelBase{private readonly Album _album;public AlbumViewModel(Album album){_album = album;}public string Artist => _album.Artist;public string Title => _album.Title;private Bitmap? _cover;public Bitmap? Cover{get => _cover;private set => this.RaiseAndSetIfChanged(ref _cover, value);}public async Task LoadCover(){await using (var imageStream = await _album.LoadCoverBitmapAsync()){Cover = await Task.Run(() => Bitmap.DecodeToWidth(imageStream, 400));}}public async Task SaveToDiskAsync(){await _album.SaveAsync();if (Cover != null){var bitmap = Cover;await Task.Run(() =>{using (var fs = _album.SaveCoverBitmapStream()){bitmap.Save(fs);}});}}}
}
using Avalonia.MusicStore.Models;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Windows.Input;namespace Avalonia.MusicStore.ViewModels
{public class MainWindowViewModel : ViewModelBase{public ICommand BuyMusicCommand { get; }public Interaction<MusicStoreViewModel, AlbumViewModel?> ShowDialog { get; }public ObservableCollection<AlbumViewModel> Albums { get; } = new();public MainWindowViewModel(){ShowDialog = new Interaction<MusicStoreViewModel, AlbumViewModel?>();BuyMusicCommand = ReactiveCommand.CreateFromTask(async () =>{var store = new MusicStoreViewModel();var result = await ShowDialog.Handle(store);if (result != null){Albums.Add(result);await result.SaveToDiskAsync();}});RxApp.MainThreadScheduler.Schedule(LoadAlbums);}private async void LoadAlbums(){var albums = (await Album.LoadCachedAsync()).Select(x => new AlbumViewModel(x));foreach (var album in albums){Albums.Add(album);}foreach (var album in Albums.ToList()){await album.LoadCover();}}}
}
using Avalonia.MusicStore.Models;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading;namespace Avalonia.MusicStore.ViewModels
{public class MusicStoreViewModel : ViewModelBase{private string? _searchText;private bool _isBusy;public string? SearchText{get => _searchText;set => this.RaiseAndSetIfChanged(ref _searchText, value);}public bool IsBusy{get => _isBusy;set => this.RaiseAndSetIfChanged(ref _isBusy, value);}private AlbumViewModel? _selectedAlbum;public ObservableCollection<AlbumViewModel> SearchResults { get; } = new();public AlbumViewModel? SelectedAlbum{get => _selectedAlbum;set => this.RaiseAndSetIfChanged(ref _selectedAlbum, value);}public MusicStoreViewModel(){this.WhenAnyValue(x => x.SearchText).Throttle(TimeSpan.FromMilliseconds(400)).ObserveOn(RxApp.MainThreadScheduler).Subscribe(DoSearch!);BuyMusicCommand = ReactiveCommand.Create(() =>{return SelectedAlbum;});}private async void DoSearch(string s){IsBusy = true;SearchResults.Clear();_cancellationTokenSource?.Cancel();_cancellationTokenSource = new CancellationTokenSource();var cancellationToken = _cancellationTokenSource.Token;if (!string.IsNullOrWhiteSpace(s)){var albums = await Album.SearchAsync(s);foreach (var album in albums){var vm = new AlbumViewModel(album);SearchResults.Add(vm);}if (!cancellationToken.IsCancellationRequested){LoadCovers(cancellationToken);}}IsBusy = false;}private async void LoadCovers(CancellationToken cancellationToken){foreach (var album in SearchResults.ToList()){await album.LoadCover();if (cancellationToken.IsCancellationRequested){return;}}}private CancellationTokenSource? _cancellationTokenSource;public ReactiveCommand<Unit, AlbumViewModel?> BuyMusicCommand { get; }}
}
using ReactiveUI;namespace Avalonia.MusicStore.ViewModels
{public class ViewModelBase : ReactiveObject{}
}

視圖源碼:

<UserControlx:Class="Avalonia.MusicStore.Views.AlbumView"xmlns="https://github.com/avaloniaui"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:vm="using:Avalonia.MusicStore.ViewModels"Width="200"d:DesignHeight="450"d:DesignWidth="800"x:DataType="vm:AlbumViewModel"mc:Ignorable="d"><StackPanel Width="200" Spacing="5"><Border ClipToBounds="True" CornerRadius="10"><Panel Background="#7FFF22DD"><ImageWidth="200"Source="{Binding Cover}"Stretch="Uniform" /><Panel Height="200" IsVisible="{Binding Cover, Converter={x:Static ObjectConverters.IsNull}}"><PathIconWidth="75"Height="75"Data="{StaticResource music_regular}" /></Panel></Panel></Border><TextBlock HorizontalAlignment="Center" Text="{Binding Title}" /><TextBlock HorizontalAlignment="Center" Text="{Binding Artist}" /></StackPanel>
</UserControl>
<Windowx:Class="Avalonia.MusicStore.Views.MainWindow"xmlns="https://github.com/avaloniaui"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:views="clr-namespace:Avalonia.MusicStore.Views"xmlns:vm="using:Avalonia.MusicStore.ViewModels"Title="Avalonia.MusicStore"d:DesignHeight="450"d:DesignWidth="800"x:DataType="vm:MainWindowViewModel"Background="Transparent"ExtendClientAreaToDecorationsHint="True"Icon="/Assets/avalonia-logo.ico"TransparencyLevelHint="AcrylicBlur"WindowStartupLocation="CenterScreen"mc:Ignorable="d"><Panel><ExperimentalAcrylicBorder IsHitTestVisible="False"><ExperimentalAcrylicBorder.Material><ExperimentalAcrylicMaterialBackgroundSource="Digger"MaterialOpacity="0.65"TintColor="Black"TintOpacity="1" /></ExperimentalAcrylicBorder.Material></ExperimentalAcrylicBorder><Panel Margin="40"><ButtonHorizontalAlignment="Right"VerticalAlignment="Top"Command="{Binding BuyMusicCommand}"><PathIcon Data="{StaticResource store_microsoft_regular}" /></Button><ItemsControl Margin="0,40,0,0" ItemsSource="{Binding Albums}"><ItemsControl.ItemsPanel><ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemTemplate><DataTemplate><views:AlbumView Margin="0,0,20,20" /></DataTemplate></ItemsControl.ItemTemplate></ItemsControl></Panel></Panel></Window>
using Avalonia.MusicStore.ViewModels;
using Avalonia.ReactiveUI;
using ReactiveUI;
using System.Threading.Tasks;namespace Avalonia.MusicStore.Views
{public partial class MainWindow : ReactiveWindow<MainWindowViewModel>{public MainWindow(){InitializeComponent();this.WhenActivated(action => action(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync)));}private async Task DoShowDialogAsync(InteractionContext<MusicStoreViewModel,AlbumViewModel?> interaction){var dialog = new MusicStoreWindow();dialog.DataContext = interaction.Input;var result = await dialog.ShowDialog<AlbumViewModel?>(this);interaction.SetOutput(result);}}
}
<UserControlx:Class="Avalonia.MusicStore.Views.MusicStoreView"xmlns="https://github.com/avaloniaui"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:vm="using:Avalonia.MusicStore.ViewModels"d:DesignHeight="450"d:DesignWidth="800"x:DataType="vm:MusicStoreViewModel"mc:Ignorable="d"><DockPanel><StackPanel DockPanel.Dock="Top"><TextBox Text="{Binding SearchText}" Watermark="Search for Albums...." /><ProgressBar IsIndeterminate="True" IsVisible="{Binding IsBusy}" /></StackPanel><ButtonHorizontalAlignment="Center"Command="{Binding BuyMusicCommand}"Content="Buy Album"DockPanel.Dock="Bottom" /><ListBoxMargin="0,20"Background="Transparent"ItemsSource="{Binding SearchResults}"SelectedItem="{Binding SelectedAlbum}"><ListBox.ItemsPanel><ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate></ListBox.ItemsPanel></ListBox></DockPanel>
</UserControl>
<Windowx:Class="Avalonia.MusicStore.Views.MusicStoreWindow"xmlns="https://github.com/avaloniaui"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:views="using:Avalonia.MusicStore.Views"Title="MusicStoreWindow"Width="1000"Height="550"ExtendClientAreaToDecorationsHint="True"TransparencyLevelHint="AcrylicBlur"WindowStartupLocation="CenterOwner"mc:Ignorable="d"><Panel><ExperimentalAcrylicBorder IsHitTestVisible="False"><ExperimentalAcrylicBorder.Material><ExperimentalAcrylicMaterialBackgroundSource="Digger"MaterialOpacity="0.65"TintColor="Black"TintOpacity="1" /></ExperimentalAcrylicBorder.Material></ExperimentalAcrylicBorder><Panel Margin="40"><views:MusicStoreView /></Panel></Panel>
</Window>
using Avalonia.MusicStore.ViewModels;
using Avalonia.ReactiveUI;
using ReactiveUI;
using System;namespace Avalonia.MusicStore.Views
{public partial class MusicStoreWindow : ReactiveWindow<MusicStoreViewModel>{public MusicStoreWindow(){InitializeComponent();this.WhenActivated(action => action(ViewModel!.BuyMusicCommand.Subscribe(Close)));}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/46241.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/46241.shtml
英文地址,請注明出處:http://en.pswp.cn/web/46241.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MySQL(8)事務

目錄 1.事務; 1.事務: 1.1 如果CURD不加限制會這么樣子? 可能造成數據同時被修改, 數據修改的結果是未知的.(可以想一下之前的搶票線程問題) 1.2 事務概念: 事務就是一組DML語句組成&#xff0c;這些語句在邏輯上存在相關性&#xff0c;這一組DML語句要么全部成功&#xff0…

基于python旅游景點滿意度分析設計與實現

1.1研究背景與意義 1.1.1研究背景 隨著旅游業的快速發展&#xff0c;滿意度分析成為評估旅游景點質量和提升游客體驗的重要手段。海口市作為中國的旅游城市之一&#xff0c;其旅游景點吸引了大量游客。然而&#xff0c;如何科學評估和提升海口市旅游景點的滿意度&#xff0c;…

中電金信-杭州工商銀行|面試真題|2024年

中電金信-杭州工商銀行 JAva集合用過哪些? ArrayList、LinkedList、HashSet、TreeSet、HashMap、LinkedHashMap、ConcurrentHashMap Arraylist和linkbist區別 ArrayList底層是數據&#xff0c;查詢快&#xff0c;增刪慢&#xff0c;線程不安全&#xff0c;效率高LikedList 底…

【概率論三】參數估計:點估計(矩估計、極大似然法)、區間估計

文章目錄 一. 點估計1. 矩估計法2. 極大似然法2.1. 似然函數2.2. 極大似然估計法 3. 評價估計量的標準3.1. 無偏性3.2. 有效性3.3. 一致性 二. 區間估計1. 區間估計的概念2. 正態總體參數的區間估計 參數估計講什么 由樣本來確定未知參數參數估計分為點估計與區間估計 一. 點估…

算法:二叉樹相關

目錄 題目一&#xff1a;單值二叉樹 題目二&#xff1a;二叉樹的最大深度 題目三&#xff1a;相同的樹 題目四&#xff1a;對稱二叉樹 題目五&#xff1a;另一棵樹的子樹 題目六&#xff1a;二叉樹的前序遍歷 題目七&#xff1a;二叉樹遍歷 題目八&#xff1a;根據二叉…

linux搭建mysql主從復制(一主一從)

目錄 0、環境部署 1、主服務器配置 1.1 修改mysql配置文件 1.2 重啟mysql 1.3 為從服務器授權 1.4 查看二進制日志坐標 2、從服務器配置 2.1 修改mysql配置文件 2.2 重啟mysql 2.3 配置主從同步 2.4 開啟主從復制 3、驗證主從復制 3.1 主服務器上創建test…

微服務拆分流程 (黑馬商城拆分商品服務)

1. 創建新module - maven模塊&#xff0c;并引入依賴&#xff08;可以復制 把不需要的依賴刪掉 &#xff09; 2. 新建包com.hmall.xx&#xff08;業務名&#xff09;&#xff0c;添加和修改啟動類&#xff0c;新建mapper包、domain包 - service包 - controller包 3. 拷貝并修…

4款良心軟件,免費又實用,內存滿了都舍不得卸載

以下幾款高質量軟件&#xff0c;若是不曾體驗&#xff0c;實在是遺憾可惜。 PDF Guru 這是一款開源免費的PDF編輯軟件&#xff0c;打開之后功能一目了然。 可以拆分、合并PDF&#xff0c;也可以給PDF添加水印和密碼&#xff0c;同時也可以去除別人PDF里的水印或密碼&#xff0…

HouseCrafter:平面草稿至3D室內場景的革新之旅

在室內設計、房地產展示和影視布景設計等領域,將平面草稿圖快速轉換為立體的3D場景一直是一個迫切的需求。HouseCrafter,一個創新的AI室內設計方案,正致力于解決這一挑戰。本文將探索HouseCrafter如何將這一過程自動化并提升至新的高度。 一、定位:AI室內設計的革新者 Ho…

Scala之OOP講解

Scala OOP 前序 Scala 為純粹OOP1、不支持基本類型&#xff1a;一切皆為對象 Byte,Int,...2、不支持靜態關鍵字&#xff1a;static 3、支持類型推斷【通過判斷泛型的父子關系來確定泛型類的父子關系>協變&#xff0c;逆變&#xff0c;不變】和類型預定&#xff0c; 動靜…

【iOS】類對象的結構分析

目錄 對象的分類object_getClass和class方法isa流程和繼承鏈分析isa流程實例驗證類的繼承鏈實例驗證 類的結構cache_t結構bits分析實例驗證屬性properties方法methods協議protocolsro類方法 類結構流程圖解 對象的分類 OC中的對象主要可以分為3種&#xff1a;實例對象&#xf…

【React】JSX基礎

一、簡介 JSX是JavaScript XML的縮寫&#xff0c;它是一種在JavaScript代碼中編寫類似HTML模板的結構的方法。JSX是React框架中構建用戶界面&#xff08;UI&#xff09;的核心方式之一。 1.什么是JSX JSX允許開發者使用類似HTML的聲明式模板來構建組件。它結合了HTML的直觀性…

TDesign組件庫日常應用的一些注意事項

【前言】Element&#xff08;餓了么開源組件庫&#xff09;在國內使用的普及率和覆蓋率高于TDesign-vue&#xff08;騰訊開源組件庫&#xff09;&#xff0c;這也導致日常開發遇到組件使用上的疑惑時&#xff0c;網上幾乎搜索不到其文章解決方案&#xff0c;只能深挖官方文檔或…

2024.7.17 ABAP面試題目總結

2024.7.17 用的SAP什么平臺&#xff0c;S4/HANA嗎&#xff0c;有用過ECC嗎 S4/HANA&#xff0c;沒用過ECC 會不會CDS VIEW 不會 會不會FIORI 不會 銀企直連里面的邏輯了解不 不了解&#xff0c;做過&#xff0c;但是只能算很簡單的修改 SAP做增強&#xff0c;如何創建…

網絡安全-網絡安全及其防護措施7

31.防病毒和惡意軟件保護 防病毒和惡意軟件防護的定義和作用 防病毒和惡意軟件防護是一種保護計算機和網絡免受病毒、木馬、間諜軟件等惡意軟件侵害的安全措施。通過防護措施&#xff0c;可以檢測、阻止和清除惡意軟件&#xff0c;確保系統和數據的安全。其主要作用包括&…

C++右值引用和移動語義

目錄 概念&#xff1a; 左值引用和右值引用 概念&#xff1a; 注意&#xff1a; 左值引用的意義 作函數參數 函數引用返回 右值引用的意義 誕生背景 移動構造 移動賦值 其他應用 萬能引用和完美轉發 默認的移動構造和移動賦值 概念&#xff1a; 左值&#xff1a;顧…

List數據的幾種數據輸出方式

一、問題引入 在Java中&#xff0c;查詢List集合是一項常見的任務&#xff0c;它可以通過多種方式實現&#xff0c;以滿足不同的需求。下面&#xff0c;List數據的幾種數據輸出方式。 二、實例 /*** 查詢所有用戶信息* return*/ List<User> getAllUser(); <select…

Git【撤銷遠程提交記錄】

在實際開發中&#xff0c;你是否遇到過錯誤的提交了代碼&#xff0c;想要刪掉本次提交記錄的情況&#xff0c;你可以按照如下方法實現。 1、使用 git revert 如果你想要保留歷史記錄&#xff0c;并且對遠程倉庫其他使用者的影響最小&#xff0c;你可以使用 git revert 命令。這…

conda 使用

首先要安裝Miniconda的環境 下面是一下命令關于如何使用conda 查看當前環境列表&#xff1a; conda env list 創建環境不指定路徑&#xff1a; conda create --name p38 python3.8 創建新環境并指定路徑&#xff1a; conda create --prefix /data/p38 python3.8激活新環境&a…

VulnHub:CK00

靶場搭建 靶機下載地址&#xff1a;CK: 00 ~ VulnHub 下載后&#xff0c;在vmware中打開靶機。 修改網絡配置為NAT 處理器修改為2 啟動靶機 靶機ip掃描不到的解決辦法 靶機開機時一直按shift或者esc直到進入GRUB界面。 按e進入編輯模式&#xff0c;找到ro&#xff0c;修…