效果预览
当我需要输入文本数据时候,就会弹出输入框,然后我们可以输入内容,这时候内容不是实时传送过去的,而是点击确定按钮时候才发送过去,优点就是简单,不会有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;
}