我正在尝试创建一个输入框(不包含按钮),以等待用户的输入(通常是数字),并且当长度等于24时,它将调用另一个函数来执行某项操作。
我已经创建了一个,但是使用确认按钮: 在mainpage.xaml.cs
中public void WaitingInput()
{
string text = InputTextDialogAsync();
DoSomething(text);
}
private void InputTextDialogAsync()
{
TextBox inputTextBox = new TextBox();
inputTextBox.AcceptsReturn = false;
inputTextBox.Height = 32;
inputTextBox.Width = 300;
ContentDialog dialog = new ContentDialog();
dialog.Content = inputTextBox;
dialog.Title = "Input Reader";
dialog.IsPrimaryButtonEnabled = true;
dialog.IsSecondaryButtonEnabled = false;
dialog.PrimaryButtonText = "Ok";
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
return inputTextBox.Text;
else
return "";
}
它创建我想要的内容,但是使用“确定”按钮进行确认。
public void DoSomething (string text) {
if (text.length < 24) {
WaitingInput();
return;
}
// Do Something with text input ...
}
我尝试使用inputTextBox.TextChanging,但是即使输入文本满足了要求(长度== 24),当它完成页面的加载时,inputText也会再次出现...
我缺少了一些东西,也许它是一个更好的解决方案。
----更新: 现在,我可以隐藏contentdialog框,但是当您尝试插入此输入文本“ 123456789012345678901234”时,出现错误消息,即使我放置了Hide函数,也无法打开多个内容对话框
mainpage.xaml.cs
namespace App1
{
public sealed partial class MainPage : Page
{
ContentDialog dialogInput = new ContentDialog();
TextBox inputBox = new TextBox();
public MainPage()
{
this.InitializeComponent();
}
public void Page_Loaded(object sender, RoutedEventArgs e)
{
WaitingInput();
}
public async void WaitingInput()
{
inputBox.AcceptsReturn = false;
inputBox.Height = 32;
inputBox.Width = 300;
inputBox.TextChanging += TextChangingHandler;
dialogInput.Content = inputBox;
dialogInput.Title = "Input Reader";
dialogInput.IsPrimaryButtonEnabled = false;
dialogInput.IsSecondaryButtonEnabled = false;
dialogInput.PrimaryButtonText = "";
await dialogInput.ShowAsync();
}
private void TextChangingHandler(TextBox sender, TextBoxTextChangingEventArgs e)
{
if (sender.Text.Length < 24)
{
return;
}
dialogInput.Hide();
DoSomething(sender.Text);
}
private async void DoSomething(string inputTextUSER)
{
if (inputTextUSER == "123456789012345678901234")
{
WaitingInput();
return;
}
inputText.Text = inputTextUSER;
await Task.Delay(3000);
}
}
}
mainPage.xaml
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<TextBox x:Name="inputText" Text="" HorizontalAlignment="Center" VerticalAlignment="Top" IsReadOnly="True" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" FontSize="24" FontWeight="Bold" RequestedTheme="Default" IsHitTestVisible="False" IsTabStop="False" IsTextPredictionEnabled="False" BorderThickness="0,0,0,2" BorderBrush="Black" Width="592" TextAlignment="Center" Foreground="Black" Background="Transparent" Height="45" Margin="0,50,0,0" />
</Grid>
答案 0 :(得分:1)
我认为OnTextChanging()是正确理解您的要求的方法。
这里有一些代码...但是了解可能还有其他情况应该考虑...粘贴大于24的数据该怎么办?输入验证?如果没有按钮并且用户想要退出,该如何关闭窗口(esc可行,但这是好的设计)吗?等等。我也没有涉及MVVM或类似的其他概念。此代码中也没有异常处理。
您应该可以根据自己的喜好进行修改。
输入文本对话框控件:
<ContentDialog
x:Class="App1.InputTextDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE">
<Grid>
<TextBox AcceptsReturn="False" Height="32" Width="300" TextChanging="OnTextChanging" />
</Grid>
</ContentDialog>
后面的代码:
public sealed partial class InputTextDialog : ContentDialog
{
public event EventHandler<EnteredTextArgs> OnValueEntered;
public InputTextDialog()
{
this.InitializeComponent();
}
private void OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (sender.Text.Length == 24)
{
OnValueEntered?.Invoke(this, new EnteredTextArgs() { EnteredText = sender.Text });
sender.Text = string.Empty;
this.Hide();
}
}
}
args类:
public class EnteredTextArgs : EventArgs
{
public string EnteredText { get; set; }
}
主页:
<StackPanel>
<Button Content="Show Dialog" Click="OnShowClick" />
<TextBlock x:Name="txtblockResult" />
</StackPanel>
后面的主页代码:
public sealed partial class MainPage : Page
{
InputTextDialog dialog = new InputTextDialog();
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
dialog.OnValueEntered += Dialog_OnValueEntered;
}
private void Dialog_OnValueEntered(object sender, EnteredTextArgs e)
{
txtblockResult.Text = e.EnteredText;
}
private async void OnShowClick(object sender, RoutedEventArgs e)
{
await dialog.ShowAsync();
}
}
****更新**** 好。根据您的最新更新,我认为这是您想要的...但是在不了解全部要求的情况下,这似乎需要一些工作。祝你好运!
public sealed partial class MainPage : Page
{
ContentDialog dialogInput = new ContentDialog();
TextBox inputBox = new TextBox();
public MainPage()
{
this.InitializeComponent();
//you only want to call this once... not each time you show the dialog
Setup();
Loaded += Page_Loaded;
}
public void Page_Loaded(object sender, RoutedEventArgs e)
{
WaitingInput();
}
/// <summary>
/// initializes the dialog and its child - the textbox
/// </summary>
private void Setup()
{
inputBox.AcceptsReturn = false;
inputBox.Height = 32;
inputBox.Width = 300;
inputBox.TextChanging += TextChangingHandler;
dialogInput.Content = inputBox;
dialogInput.Title = "Input Reader";
dialogInput.IsPrimaryButtonEnabled = false;
dialogInput.IsSecondaryButtonEnabled = false;
dialogInput.PrimaryButtonText = "";
}
private void ResetDialog()
{
inputBox.Text = string.Empty;
WaitingInput();
}
public async void WaitingInput()
{
await dialogInput.ShowAsync();
}
private async void TextChangingHandler(TextBox sender, TextBoxTextChangingEventArgs e)
{
if (sender.Text.Length < 24)
{
return;
}
dialogInput.Hide();
await DoSomething(sender.Text);
}
private async Task DoSomething(string inputTextUSER)
{
if (inputTextUSER == "123456789012345678901234")
{
//note: the dialog will not show again. May as well close the app
return;
}
//show inputted text in textblock
inputText.Text = inputTextUSER;
await Task.Delay(3000);
//after 3 seconds, show the dialog again - unclear requirement
ResetDialog();
}
}
mainpage.xaml:
<Grid>
<TextBox x:Name="inputText" Text="" HorizontalAlignment="Center" VerticalAlignment="Top" IsReadOnly="True" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" FontSize="24" FontWeight="Bold" RequestedTheme="Default" IsHitTestVisible="False" IsTabStop="False" IsTextPredictionEnabled="False" BorderThickness="0,0,0,2" BorderBrush="Black" Width="592" TextAlignment="Center" Foreground="White" Background="Transparent" Height="45" Margin="0,50,0,0" />
</Grid>