大家好,我只是一个简单的问题,我试图在文本框中实现一个自动保存功能,以便记住我为自己制作的软件。我找到Binding.UpadateSourceTrigger
至PropertyChanged
的答案,TextBox.Text
。我已经尝试过这样做,但我最终得到了大量的错误(C#和VS的新手),如果这是唯一的方法,我怎么能用这个代码(我使用mahapps地铁好看):< / p>
`
<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>
`
答案 0 :(得分:0)
您需要考虑许多事项。
首先,您需要一种加密密码的好方法。您不希望将密码保存为纯文本,因为任何有权访问该文件的人都可以读取您的所有密码。 (如果这是个人应用程序,那么可能只有您可以访问该文件,但由于您正在制作应用程序而不是使用纯文本文件,我认为您的情况更复杂。)
C#附带了许多可以使用的加密库,例如RijndaelManaged。您应该阅读有关加密和IV的大量内容,以便了解如何以安全的方式实现它。基本上,您需要生成一个用于加密/解密文件的密钥。如何存储此密钥可能很复杂,具体取决于您希望的安全性。您可以将密钥硬编码到您的应用程序中,但请注意,有人可能会对您的应用程序进行反编译并获取密钥。此外,每次加密密码时,都应生成IV并在加密密码字节的开头添加IV。然后你可以使用IV来解密密码。
有关加密和解密的示例,请参阅RijndaelManaged页面。请注意,与示例不同,您不希望每次加密时都调用myRijndael.GenerateKey();
- 而应将myRijndael.Key
设置为等于预先生成的密钥。
接下来,您需要确定存储保存密码的文件的位置。您可以将它存储在与应用程序相同的目录中,但如果您的应用程序位于Program Files中,则可能会遇到访问权限问题,因为您的程序需要以管理员身份运行才能写入Program Files。您可能希望保存到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
与此无关。默认为TextBox.Text
,绑定将在文本框失去焦点时更新。如果将其更改为PropertyChanged
,则绑定将针对文本框中的每个字符进行更新。在你的情况下,我认为默认行为是好的。如果您使用的是MVVM,请确保您的自动保存代码位于ViewModel中的Passowrd设置器中,并且您已经是黄金。
答案 1 :(得分:0)
如果您希望TextBox
中的值在您键入时自动更新DependencyProperty
支持来源,那么您就可以使用UpdateSourceTrigger=PropertyChanged
走上正轨。 UpdateSourceTrigger
的{{1}}属性的默认Text
为TextBox
。
我使用Code-Behind为您提供了一个快速示例。
<强> XAML 强>
LostFocus
<强>代码隐藏强>
<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>
在相关说明中,但实际上不是问题的一部分,aj_r有关于密码管理的有效点。如果这只是一个让你玩的应用程序,并掌握一些东西,那么没什么大不了的,如果你打算在某个时候实际使用这个应用程序那么你会想要研究密码管理并替换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));
}
}
}
}
控件使用TextBox
控件。
答案 2 :(得分:0)
您可以使用应用程序的默认设置保存TextBox数据。
在TextBox的某些控件或TextChanged事件的Click事件中,您可以保存TextBox数据:
YourAppID.Properties.Settings.Default.DataToSave = TextBox.Text;
在WindowLoaded事件或某些控制事件上,您可以获取已保存到TextBox的数据:
TextBox.Text = YourAppID.Properties.Settings.Default.DataToSave;
在此重要时刻,您初步必须将“DataToSave”字段添加到项目属性中。
祝你好运