大家好,一个简单的问题,我试图实现一个自动保存功能到文本框中的密码记忆软件,我正在为自己制作。我偶然发现了对Binding.UpadateSourceTrigger对PropertyChanged说TextBox.Text的回答。我尝试过这样做,但是我最终得到了大量的错误(对于C#和VS来说都是新的),如果这是唯一的方法,那么我如何使用这个代码(我使用mahapps地铁来获得良好的外观):
`
<Grid>
<TabControl HorizontalAlignment="Left" Height="469" VerticalAlignment="Top" Width="692">
<TabItem Header="Logins">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top" Width="352"/>
</Grid>
</TabItem>
<TabItem Header="Cards">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top" Width="352"/>
</Grid>
</TabItem>
<TabItem Header="Renewals">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top" Width="352"/>
</Grid>
</TabItem>
</TabControl>
</Grid>`
发布于 2014-11-09 15:08:08
可以使用应用程序的默认设置保存TextBox数据。
在某些控件的单击事件或TextBox的TextChanged事件上,您可以保存TextBox数据:
YourAppID.Properties.Settings.Default.DataToSave = TextBox.Text;在WindowLoaded事件或某些控件事件上,您可以获得保存到TextBox的数据:
TextBox.Text = YourAppID.Properties.Settings.Default.DataToSave;在这个重要时刻,您必须将"DataToSave“字段添加到您的项目属性中。
祝好运
发布于 2014-11-09 06:20:35
有很多事情你需要考虑。
首先,您需要一种加密密码的好方法。您不希望将密码保存为纯文本,因为访问该文件的任何人都可以读取您的所有密码。(如果这是一个个人应用程序,那么可能只有您可以访问该文件,但由于您正在制作一个应用程序而不是使用纯文本文件,我认为您的情况要复杂得多。)
C#附带了许多您可以使用的加密库,比如RijndaelManaged。您应该阅读大量关于加密和IVs的知识,以便了解如何以安全的方式实现它。基本上,您需要生成用于加密/解密文件的密钥。如何存储这个密钥可能很复杂,这取决于您想要的安全程度。您可以将密钥硬编码到您的应用程序中,但是要注意,有人可能会将您的应用程序解压缩并获得密钥。而且,每次加密密码时,都应该生成一个IV,并在加密密码字节的开头加上IV。然后,您可以在以后使用IV解密密码。
有关加密和解密的示例,请参阅RijndaelManaged页面。请注意,与示例不同的是,您不希望每次加密时都调用myRijndael.GenerateKey();,而是应该将myRijndael.Key设置为与预先生成的密钥相同的值。
接下来,您需要决定在哪里存储保存密码的文件。您可以将其存储在与应用程序相同的目录中,但如果应用程序位于Program中,则可能会遇到访问权限问题,因为您的程序需要以管理员身份运行才能写入program。您可能希望保存到像ApplicationData这样的地方:
string filePath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"MyApp",
"pw.txt");
// filePath = C:\Users\<username>\AppData\Roaming\MyApp\pw.txt (for Windows Vista and later)
// You can also use Environment.SpecialFolder.CommonApplicationData if you want the file to be accessible to all user accounts on the machine.另外,我认为Binding.UpadateSourceTrigger与此无关。默认情况下,当文本框失去焦点时,绑定将更新。如果将其更改为PropertyChanged,则绑定将更新文本框中的每个字符。在你的例子中,我认为默认的行为是好的。如果您正在使用MVVM,只需确保您的自动保存代码在ViewModel中的Passowrd setter中,并且您是黄金级的。
发布于 2014-11-09 14:31:10
如果您希望TextBox中的值在键入时自动更新DependencyProperty支持源,那么您就可以正确地使用UpdateSourceTrigger=PropertyChanged。UpdateSourceTrigger的Text属性的默认TextBox是LostFocus。
我给你举了一个简单的例子,让你用代码隐藏来玩。
XAML
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="auto" Width="auto">
<Grid>
<TabControl HorizontalAlignment="Left" Height="469" VerticalAlignment="Top" Width="692">
<TabItem Header="Logins">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top"
Width="352" Text="{Binding LoginsTextBoxText, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</TabItem>
<TabItem Header="Cards">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top"
Width="352" />
</Grid>
</TabItem>
<TabItem Header="Renewals">
<Grid Background="#FFFFFF">
<TextBox HorizontalAlignment="Left" Height="422" TextWrapping="Wrap" VerticalAlignment="Top"
Width="352" />
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>代码-幕后
namespace StackOverflow
{
using System.ComponentModel;
public partial class MainWindow : INotifyPropertyChanged
{
private string loginsTextBoxText;
public MainWindow()
{
this.InitializeComponent();
}
public string LoginsTextBoxText
{
get { return this.loginsTextBoxText; }
set
{
this.loginsTextBoxText = value; // Put a breakpoint here and type in the logins textbox
this.OnPropertyChanged(this.LoginsTextBoxText);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}在一个相关的注意事项上,但实际上不是问题的一部分,aj_r在密码管理方面有一个有效的观点。如果这只是一个应用程序,你可以玩和处理事情,那么没有什么大不了的,但是如果你打算在某个时候实际使用这个应用程序,那么你会想要研究密码管理,用PasswordBox控件代替PasswordBox控件。
https://stackoverflow.com/questions/26825103
复制相似问题