创建我已创建的窗口的新实例.多次调用来做不同的事情
本文关键字:创建 调用 不同的事 实例 窗口 新实例 | 更新日期: 2023-09-27 18:05:55
我已经创建了一个窗口,这是一个数字键盘:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
<Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False" Click="btnZzero_Click"></Button>
<Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
<Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
<Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
<Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
<Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"/>
<Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
<Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
<Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
<Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White"></Button>
<Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
<Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
现在,我想在我的应用程序的不同时间调用这个键盘来做不同的事情。
IE我想用它作为登录屏幕:
<Window x:Class="WpfApplication2.MainLogin2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainLogin2" WindowState="Maximized" Width="1024" Height="768" WindowStyle="None" WindowStartupLocation="CenterScreen">
<Grid Background="#FF260538">
<Grid.RowDefinitions>
<RowDefinition Height="768" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1024" />
</Grid.ColumnDefinitions>
<TextBox Name="userIDTextBox" HorizontalAlignment="Left" Height="47" Margin="402,249,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="213" FontSize="30" PreviewMouseDown="userIDTextBox_PreviewMouseDown" />
<TextBox Name="passcodeTextBox" Height="47" Margin="402,317,0,0" TextWrapping="Wrap" Text="Passcode" VerticalAlignment="Top" Width="213" FontSize="30" HorizontalAlignment="Left" PreviewMouseDown="passcodeTextBox_PreviewMouseDown" />
<Button Content="OK" HorizontalAlignment="Left" Margin="402,386,0,0" VerticalAlignment="Top" Width="213" Height="59" FontSize="30" Click="okButton_Click"/>
<Button Content="CANCEL" HorizontalAlignment="Left" Margin="402,450,0,0" VerticalAlignment="Top" Width="213" Height="59" FontSize="30" Click="cancelButton_Click_1"/>
</Grid>
我的问题是我如何通过结果从键盘回到不同的文本字段后,我已经点击确定?
下面是键盘窗口后面的代码。到目前为止,我只使用数字1进行测试:
namespace WpfApplication2
{
public partial class QuantityKeypad : Window
{
bool btnClicked = false;
string quantityResult = "";
public QuantityKeypad()
{
InitializeComponent();
}
private void cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnZzero_Click(object sender, RoutedEventArgs e)
{
}
private void btnOne_Click(object sender, RoutedEventArgs e)
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
quantityResult = quantityResult += "1";
btnTotal.Text = quantityResult;
}
private void btnTwo_Click(object sender, RoutedEventArgs e)
{
}
}
}
下面是登录屏幕的代码:
namespace WpfApplication2
{
public partial class MainLogin2 : Window
{
public MainLogin2()
{
InitializeComponent();
}
//OK button
private void okButton_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
this.Close();
}
//Cancel button - takes user back to login screen
private void cancelButton_Click_1(object sender, RoutedEventArgs e)
{
this.Close();
}
//Removes text from login textboxes when clicked in
//opens instance of Quantitykeypad when clicking inside textbox
private void userIDTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
userIDTextBox.Text = "";
QuantityKeypad loginKeypad = new QuantityKeypad();
loginKeypad.Owner = this;
loginKeypad.ShowDialog();
}
//Removes text from passcode textboxe when clicked in
//opens instance of Quantitykeypad when clicking inside passcode textbox
private void passcodeTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
passcodeTextBox.Text = "";
QuantityKeypad loginKeypad = new QuantityKeypad();
loginKeypad.Owner = this;
loginKeypad.ShowDialog();
}
}
}
非常抱歉,如果这是简单的,但我已经挣扎了几天了。
谢谢
您可以将您的私有字段quantityResult
设置为可公开访问的属性:
public string QuantityResult { get; private set; }
这样,你可以从中读取:
passcodeTextBox.Text = "";
QuantityKeypad loginKeypad = new QuantityKeypad();
loginKeypad.Owner = this;
loginKeypad.ShowDialog();
passcodeTextBox.Text = loginKeypad.QuantityResult;