如何保存在文本框的用户估算值? (WPF XAML)所以在我的XAML的窗口我有一个文本框。用户sturts我的应用程序输入一些数值到它和presses按钮或提示输入。他关闭的应用程序。再次打开它。如何使自己的输入保存在文本框在WPF?
How to save user imputed value in TextBox? (WPF XAML) So in my xaml window I have a TextBox. User sturts my application inputs some values in to it and presses a button or hints Enter. He closes an app. Opens it up again. How to make his inputs be saved in that TextBox in WPF?
推荐答案
您可以使用内置的.NET设置。
You can use the built in .net settings.
在Visual Studio中,右键点击你的项目,然后选择添加新项。在对话框中,选择设置文件,并给它一个名称,如MySettings。 Visual Studio将创建一些文件,包括 MySettings
类的一些静态方法来提供您访问的设置。
In visual studio, right click on your project and choose Add new item. From the dialog, select "Settings file", and give it a name like "MySettings". Visual studio will create a few files including a MySettings
class with some static methods to provide you access to your settings.
如果你打开这个文件,你会得到一个不错的网格的用户界面,使您可以输入一些设置,设置它们的类型(在这种情况下,字符串
),并设置一个默认值。它也允许你指定,如果他们的应用程序或用户的设置。
If you open this file up, you will be given a nice grid ui that allows you to enter some settings, set their type (in this case String
) and set a default value. It also allows you to specify if they are application or user settings.
的Documents and Settings 用户名本地设置
文件夹。可以为每个用户不同。
Application settings: Cannot be modified after the app has started. Can only be configued by editing an xml .config file. Will be the same for every user who runs the app.
User settings: Can be modified and saved while the application is running. Will be stored in the users documents and settingsusernamelocal settings
folder. Can be different for every user.
有关你所描述的,选择用户的范围。
For what you are describing, choose "User" for the scope.
现在,进入价值code:
Now, to access the value in code:
// Load the value into the text box.
txtBox1.text = MySettings.Default.SomeSetting;
和保存的改变:
// Update the value.
MySettings.Default.SomeSetting = txtBox1.text;
// Save the config file.
MySettings.Default.Save();
有一个关于所有这一切都在这里 MSDN 更多的信息,还有的更多信息在ApplicationSettingsBase一流这里。
(显然,如果您使用的是MVVM,或任何其他UI模式,你能适应这种code加载设置值代入模型/的ViewModels适当的时机,而不是直接在文本框中)
相关推荐
最新文章