• WPF项目-按着键盘方向键,移动格子盒子效果


     1  界面展示前端代码,写入

     

    1. <Window x:Class="WpfApp1.移动方块.WindowModelBox"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfApp1.移动方块"
    7. mc:Ignorable="d"
    8. Title="WindowModelBox" Height="450" Width="800" KeyDown="Grid_KeyDown">
    9. <Grid ShowGridLines="True" Background="Teal" Name="gridContent" >
    10. <Grid.ColumnDefinitions>
    11. <ColumnDefinition>ColumnDefinition>
    12. <ColumnDefinition>ColumnDefinition>
    13. <ColumnDefinition>ColumnDefinition>
    14. Grid.ColumnDefinitions>
    15. <Grid.RowDefinitions>
    16. <RowDefinition> RowDefinition>
    17. <RowDefinition>RowDefinition>
    18. <RowDefinition>RowDefinition>
    19. Grid.RowDefinitions>
    20. <Border Name="b1" Background="Transparent" Grid.Row="0" Grid.Column="0">Border>
    21. <Border Name="b2" Background="white" Grid.Row="0" Grid.Column="1">Border>
    22. <Border Name="b3" Background="Transparent" Grid.Row="0" Grid.Column="2">Border>
    23. <Border Name="b4" Background="Transparent" Grid.Row="1" Grid.Column="0">Border>
    24. <Border Name="b5" Background="Transparent" Grid.Row="1" Grid.Column="1">Border>
    25. <Border Name="b6" Background="Transparent" Grid.Row="1" Grid.Column="2">Border>
    26. <Border Name="b7" Background="Transparent" Grid.Row="2" Grid.Column="0">Border>
    27. <Border Name="b8" Background="Transparent" Grid.Row="2" Grid.Column="1">Border>
    28. <Border Name="b9" Background="Transparent" Grid.Row="2" Grid.Column="2">Border>
    29. Grid>
    30. Window>

     2 C# 逻辑类处理,跟业务类基本一样。变化的是业务场景而已。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.Windows;
    5. using System.Windows.Controls;
    6. using System.Windows.Data;
    7. using System.Windows.Documents;
    8. using System.Windows.Input;
    9. using System.Windows.Media;
    10. using System.Windows.Media.Imaging;
    11. using System.Windows.Shapes;
    12. namespace WpfApp1.移动方块
    13. {
    14. ///
    15. /// WindowModelBox.xaml 的交互逻辑
    16. ///
    17. public partial class WindowModelBox : Window
    18. {
    19. public WindowModelBox()
    20. {
    21. InitializeComponent();
    22. }
    23. private void Grid_KeyDown(object sender, KeyEventArgs e)
    24. {
    25. // 首先判断按键的方位 比如上下左右
    26. // 向上移动 移到格子每次减去3 向下移动 移到格子每次加3
    27. // 向左移动 每次减去2 向右移动 每次加1
    28. //1 获取白色的border 元素 用对象点 子元素去获取。固定方式? gridContent.Children
    29. UIElementCollection childrenobj= gridContent.Children;
    30. // 变量定义 ,设置为Null first 使用
    31. Border curBorder =null;
    32. // 循环遍历,基础不知道,自个多看看去啊!
    33. // 遍历对象里面的数据-源
    34. for (int i = 0; i < childrenobj.Count; i++)
    35. {
    36. // 0.1 进行逻辑处理,都变成透明颜色
    37. // 嵌套的if判断用法
    38. if(childrenobj[i] is Border )
    39. { // 嵌套加嵌套的判断,很复杂了呢。。。这用法
    40. if(((childrenobj[i] as Border).Background
    41. as SolidColorBrush).Color.Equals(Colors.White))
    42. {
    43. // 找到他了,获取name
    44. curBorder = childrenobj[i] as Border;
    45. }
    46. // 背景颜色变成透明色
    47. (childrenobj[i] as Border).Background = new SolidColorBrush(Colors.Transparent);
    48. }
    49. }
    50. //找到数据格子后, 获取格子名字
    51. string name =curBorder.Name;
    52. int index = Convert.ToInt32(name.Replace("b", ""));
    53. // 行为判断的内在逻辑所在。
    54. if (e.Key.Equals(Key.Up)) // 上
    55. { // 减法运算,+ bool 判断?
    56. index = index - 3 >= 1 ? index - 3 : index; // 三元表达式使用?
    57. }
    58. else if (e.Key.Equals(Key.Down)) // 下 走逻辑
    59. { // 这里是写死的数据,数据9
    60. index = index + 3 <= 9 ? index + 3 : index;
    61. }
    62. else if (e.Key.Equals(Key.Left)) // 左键
    63. {
    64. index = index - 1 >= 1 ? index - 1 : index;
    65. }
    66. else if (e.Key.Equals(Key.Right)) // 右键 走 依次
    67. { // 不能大于他的最大下标
    68. index = index +1 <= 9 ? index + 1 : index;
    69. }
    70. object controlobj = gridContent.FindName("b" + index);
    71. if(controlobj != null)
    72. { // 1.3 把移动后的控件,变成了白色。
    73. (controlobj as Border).Background = new SolidColorBrush(Colors.White);
    74. }
    75. }
    76. }
    77. }

      3 主项目,修改启动项目文件夹就可以了

                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:WpfApp1"
                 StartupUri="移动方块/WindowModelBox.xaml">
       
             
       

     4 演示效果

        短视频

       

    按着键盘方向键,移动格子盒子效果

  • 相关阅读:
    【Vue】eventbus 首次$on事件未绑定问题
    Redis 数据迁移篇之move、dump、migrate、redis-rdb-tools和redis-dump工具使用手册
    猿创征文|【单片机】keil和Proteus使用教程
    单例模式的安全写法
    TMD,JVM类加载原来是这样的!!!!
    containerd拉取私库镜像失败(kubelet)
    python——运行方式
    rocketmq消息写入流程
    Linux日志打包脚本
    Java进阶资源汇总
  • 原文地址:https://blog.csdn.net/chenggong9527/article/details/126088978