所以我要做的是在WPF中创建一个基本的char替换密码,但由于某种原因,当我按Enter键时,结果框不会更新。任何人都可以看到我在下面的C#代码中做错了什么?
public partial class MainWindow : Window
{
public static string PassingEncrypt;
public static string PassingDecrypt;
string plainText;
public MainWindow()
{
InitializeComponent();
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
plainText = EncryptBox.Text;
string cipherText = Encrypt(plainText, key);
string decryptedText = Decrypt(cipherText, key);
EncryptResult.Text = cipherText;
DecryptResult.Text = decryptedText;
}
private void EncryptBox_TextChanged(object sender, TextChangedEventArgs e)
{
this.EncryptBox = sender as TextBox;
plainText = EncryptBox.Text;
}
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for (int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = key.IndexOf(cipherText[i]) - 97;
chars[i] = (char)j;
}
}
return new string(chars);
}
}
如果你找不到问题,这是我的xaml代码
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="EncryptBox" HorizontalAlignment="Left" Height="50" Margin="0,81,0,0" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="250" TextChanged="EncryptBox_TextChanged"/>
<TextBox x:Name="DecryptBox" HorizontalAlignment="Left" Height="50" Margin="269,81,-0.6,0" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="250"/>
<TextBox x:Name="EncryptResult" HorizontalAlignment="Left" Height="43" Margin="0,238,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
<TextBox x:Name="DecryptResult" HorizontalAlignment="Left" Height="43" Margin="269,238,-0.6,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
</Grid>
</Window>
答案 0 :(得分:1)
您可以在后面的代码中实现此处理TextChangedEvent
,但您确实不应该这样做。这就是Converters
的用途,保持您的View干净,没有任何代码。
将Text
和Encrypt-
的{{1}}绑定到其前任的DecriptBox
,并让Text
完成工作。
<强> MainWindow.xaml 强>
Converters
<强> MainWindow.xaml.cs 强>
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:EncryptConverter x:Key="EncryptConverter"/>
<local:DecryptConverter x:Key="DecryptConverter"/>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="20,0"/>
</Style>
</Window.Resources>
<StackPanel>
<Label>Plain Text</Label>
<TextBox x:Name="PlainBox" MaxLength="26"/>
<Label>Encripted Text</Label>
<TextBox x:Name="EncriptedBox" IsEnabled="False"
Text="{Binding Path=Text, ElementName=PlainBox, Converter={StaticResource EncryptConverter}}"/>
<Label>Decripted Text</Label>
<TextBox x:Name="DecriptedBox" IsEnabled="False"
Text="{Binding Path=Text, ElementName=EncriptedBox, Converter={StaticResource DecryptConverter}}"/>
</StackPanel>
</Window>