网站建设最新流行,选择做网站销售的优势,免费设计logo的软件有哪些,西安有几个区WPF 实现冒泡排序可视化 实现冒泡排序代码就不过多讲解#xff0c;主要是实现动画效果思路#xff0c;本demo使用MVVM模式编写#xff0c;读者可自行参考部分核心代码#xff0c;即可实现如视频所示效果。 对于新手了解算法相关知识应该有些许帮助#xff0c;至于其它类型… WPF 实现冒泡排序可视化 实现冒泡排序代码就不过多讲解主要是实现动画效果思路本demo使用MVVM模式编写读者可自行参考部分核心代码即可实现如视频所示效果。 对于新手了解算法相关知识应该有些许帮助至于其它类型排序也可按该思路自行修改实现。 直接上代码页面布局.xaml代码如下 UserControl x:ClassWpf_MetroListBox.Views.Test.CanvasLabelMovexmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:Wpf_MetroListBox.Views.Testmc:Ignorabled d:DesignHeight450 d:DesignWidth800GridGrid.RowDefinitionsRowDefinition Height380/RowDefinitionRowDefinition Height*/RowDefinition/Grid.RowDefinitionsCanvas x:Namecanvas Grid.Row0 BackgroundWhite/CanvasGrid Grid.Row1Grid.ColumnDefinitionsColumnDefinition/ColumnDefinitionColumnDefinition/ColumnDefinition/Grid.ColumnDefinitionsButton Content创建序列 HorizontalAlignmentCenter Grid.Column0 Width70 Height35 Command{Binding CommitCommand} Style{DynamicResource btn-info-m}/ButtonButton Content执行排序 HorizontalAlignmentCenter Grid.Column1 Width70 Height35 Command{Binding QueryCommand} Style{DynamicResource btn-info-m}/Button/Grid/Grid/UserControl
逻辑代码实现如下
public partial class CanvasLabelMove : UserControl{CanvasLabelMoveViewModel vm;private Random random new Random();private ListLabel labels new ListLabel();private int[] array;private int delay 1000; // 动画延迟时间毫秒public CanvasLabelMove(){InitializeComponent();vm new CanvasLabelMoveViewModel();this.DataContext vm;this.Publish(EventNames.MainWindowLoadControlEvent, 排序可视化);this.Subscriberstring(EventNames.DataGridFocusChangedEvent, async contentStr { if (contentStr.Equals(1)){InitializeArrayAndLabels();}else if (contentStr.Equals(2)){StartBubbleSortAnimation();}});}private void InitializeArrayAndLabels(){int size 18; // 数组大小 控制标签个数array new int[size];labels.Clear();canvas.Children.Clear();HashSetint randomNumbers new HashSetint();Random random new Random();//生成不重复随机数while (randomNumbers.Count size){int nextNumber random.Next(22, 200);randomNumbers.Add(nextNumber);}int j 0;foreach (int number in randomNumbers){array[j] number;j;}for (int i 0; i size; i){Label label new Label{Content array[i].ToString(),FontSize 12,Width 35,Height array[i],Margin new Thickness(50, 0, 0, 0),Foreground Brushes.Black,Background Brushes.BlanchedAlmond,VerticalContentAlignment VerticalAlignment.Center,HorizontalContentAlignment HorizontalAlignment.Center}; Canvas.SetLeft(label, i * 50); // 设置Label的X坐标Canvas.SetBottom(label, (canvas.ActualHeight / 2) - 100); // 设置Label的Y坐标在中间canvas.Children.Add(label);labels.Add(label);}}private async void StartBubbleSortAnimation(){for (int j 0; j array.Length - 1; j){for (int i 0; i array.Length - 2; i){if (array[i] array[i 1]){Swap(i, i 1);// 更新Label位置AnimateSwap(i, i 1);await Task.Delay(delay);}}}}private void Swap(int i, int j){int temp array[i];array[i] array[j];array[j] temp;}private async void AnimateSwap(int i, int j){// 临时存储Label的位置double label1X Canvas.GetLeft(labels[i]);double label2X Canvas.GetLeft(labels[j]);await Task.Delay(delay / 10); // 控制动画速度var animation1 new DoubleAnimation(label2X, TimeSpan.FromSeconds(0.5));var animation2 new DoubleAnimation(label1X, TimeSpan.FromSeconds(0.5));labels[i].BeginAnimation(Canvas.LeftProperty, animation1);labels[j].BeginAnimation(Canvas.LeftProperty, animation2);// 最终位置//Canvas.SetLeft(labels[i], label2X);//Canvas.SetLeft(labels[j], label1X);Label tempLabel labels[i];labels[i] labels[j];labels[j] tempLabel;}}public class CanvasLabelMoveViewModel : BaseValidViewModel{public CanvasLabelMoveViewModel(){}protected override void ExecuteCommitCommand(){this.Publish(EventNames.DataGridFocusChangedEvent, 1);}protected override void ExecuteQueryCommand(){this.Publish(EventNames.DataGridFocusChangedEvent, 2);}}
文章转载自: http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.bmfqg.cn.gov.cn.bmfqg.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.wdhzk.cn.gov.cn.wdhzk.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.kzpy.cn.gov.cn.kzpy.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn