WPF设计简易版键盘

这篇文章展示了一个基于WPF的输入窗口实现,用户在输入框中输入内容,点击确定按钮后发送数据。前端XAML代码定义了窗口布局,包括按钮和文本显示,后端代码实现了DelegateCommand来处理关闭、删除、输入和确定等操作。数据并非实时发送,这降低了复杂性但可能影响用户体验。
摘要由CSDN通过智能技术生成

效果预览

当我需要输入文本数据时候,就会弹出输入框,然后我们可以输入内容,这时候内容不是实时传送过去的,而是点击确定按钮时候才发送过去,优点就是简单,不会有bug,缺点就是丑

 

 前端xaml代码

<Window x:Class="LaBuJi.Views.InputWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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:local="clr-namespace:LaBuJi.Views"
        mc:Ignorable="d"
        WindowStyle="None"
        WindowStartupLocation="CenterScreen"
        xmlns:prism="http://prismlibrary.com/" 
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="InputWindow" Height="400" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Border BorderThickness="2" BorderBrush="Green" Grid.Row="0" Grid.Column="0">
            <TextBlock   FontSize="30" Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>
        <Button Grid.Row="0" Grid.Column="1"   FontSize="30"  Content="退出" Command="{Binding CloseCommand}" BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="0" Grid.Column="2" Content="删除" Command="{Binding DeleteCommand}"  FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="1" Grid.Column="0" Content="1" Command="{Binding NumberCommand}" CommandParameter="1" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="1" Grid.Column="1" Content="2" Command="{Binding NumberCommand}" CommandParameter="2" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="1" Grid.Column="2" Content="3" Command="{Binding NumberCommand}" CommandParameter="3" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="2" Grid.Column="0" Content="4" Command="{Binding NumberCommand}" CommandParameter="4" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="2" Grid.Column="1" Content="5" Command="{Binding NumberCommand}" CommandParameter="5" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="2" Grid.Column="2" Content="6" Command="{Binding NumberCommand}" CommandParameter="6" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="3" Grid.Column="0" Content="7" Command="{Binding NumberCommand}" CommandParameter="7" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="3" Grid.Column="1" Content="8" Command="{Binding NumberCommand}" CommandParameter="8" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="3" Grid.Column="2" Content="9" Command="{Binding NumberCommand}" CommandParameter="9" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="4" Grid.Column="0" Content="."  Command="{Binding NumberCommand}" CommandParameter="." FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="4" Grid.Column="1" Content="0" Command="{Binding NumberCommand}" CommandParameter="0" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
        <Button Grid.Row="4" Grid.Column="2" Content="确定" Command="{Binding EnterCommand}" FontSize="30"  BorderBrush="Green"  BorderThickness="2"/>
    </Grid>

</Window>

后端代码

using LaBuJi.Models;
using LaBuJi.Utils;
using LaBuJi.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace LaBuJi.ViewModels
{
    public class InputWindowViewModel : BindableBase
    {
        public DelegateCommand CloseCommand { get; private set; }
        public DelegateCommand DeleteCommand { get; private set; }
        public DelegateCommand EnterCommand { get; private set; }
        //处理小数和数字
        public DelegateCommand<string> NumberCommand { get; set; }
        private readonly IEventAggregator ea;

        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                RaisePropertyChanged();
            }
        }
        public InputWindowViewModel(IEventAggregator eventAggregator)
        {
            ea = eventAggregator;
            NumberCommand = new DelegateCommand<string>(AddNumber);
            CloseCommand = new DelegateCommand(closeFun);
            DeleteCommand = new DelegateCommand(deleteFun);
            EnterCommand = new DelegateCommand(enterFun);
        }
        private void AddNumber(string number)
        {
            Text += number;
        }
        private void closeFun()
        {

            foreach (Window window in Application.Current.Windows)
            {
                if (window is InputWindow)//关闭当前界面,打开上一个界面
                {
                    window.Close();
                    break;
                }
            }

        }
        private void deleteFun()
        {
            if (!string.IsNullOrEmpty(Text))
            {
                Text = Text.Substring(0, Text.Length - 1);
            }
        }
        private void enterFun()
        {
            //回车事件,判断操作的界面在哪
            foreach (Window window in Application.Current.Windows)
            {
               
                //假如你是MainWindow的文本内容,
                if (window is MainWindow)
                {
                    //写跨模块事件,主要是把结果发送给MainWindow界面那边
                }
                    
                
            }
            //关闭
            foreach (Window window in Application.Current.Windows)
            {
                if (window is InputWindow)//
                {
                    window.Close();
                    break;
                }

            }

        }

    }
}

MainWindow

这里前端随便丢个<textbox>控件,例如


                    <TextBox 
                            Foreground="#545454"
                            Text="{Binding text}"
                            FontSize="20"
                            BorderThickness="0"
                            >
                      <i:Interaction.Triggers>
                         <i:EventTrigger EventName="PreviewMouseDown">
                           <i:InvokeCommandAction Command="{Binding textCommand}" />
                            </i:EventTrigger>
                      </i:Interaction.Triggers>
                    </TextBox>

这里就是当我点击文本内容时候触发的事件

MainWindowViewModel

部分核心代码

public string text{ get { return text; } set { text= value; RaisePropertyChanged(); } }
        private string _text;
public DelegateCommand textCommand { get;private set; }


 private readonly IEventAggregator ea;
public MainWindowViewModel(IEventAggregator eventAggregator) {
       ea=eventAggregator;
       textCommand = new DelegateCommand(inputFun);
}
 private void inputFun() {
            //先关闭,如果有再打开,

            foreach (Window window in Application.Current.Windows)
            {
                if (window is InputWindow)//关闭键盘界面
                {
                    window.Close();
                    break;
                }
            }
            InputWindow inputWindow = new InputWindow();
            inputWindow.Topmost = true;
            inputWindow.Show();
            ea.GetEvent<Event>().Subscribe(Function);
            

        }
        private void Function(string _text)
        {
           
           
                text= _text;
            
        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值