如何在TextBox中保存用户输入的值?(WPF,XAML) [英] How to save user inputed value in TextBox? (WPF, XAML)

查看:43
本文介绍了如何在TextBox中保存用户输入的值?(WPF,XAML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文本框中保存用户输入的值?(WPF XAML)因此,在我的xaml窗口中,我有一个TextBox.用户启动我的应用程序,在其中输入一些值,然后按一个按钮或按Enter.他关闭应用程序,然后再次打开它.如何将他的输入保存在WPF的那个TextBox中?

How to save user inputed value in a TextBox? (WPF XAML) So in my xaml window I have a TextBox. A User starts my application, inputs some values into it and presses a button or hits Enter. He closes the app, opens it up again. How to make his inputs to 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.

如果打开此文件,将为您提供一个漂亮的网格ui,允许您输入一些设置,设置其类型(在本例中为 String )并设置默认值.它还允许您指定它们是应用程序设置还是用户设置.

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.

  • 应用程序设置:应用程序启动后无法修改.只能通过编辑xml .config文件进行配置.每个运行该应用程序的用户都将是相同的.
  • 用户设置:可以在应用程序运行时进行修改和保存.将存储在用户文档和设置\用户名\本地设置文件夹中.每个用户都可以不同.
  • 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 settings\username\local settings folder. Can be different for every user.

对于您要描述的内容,请为作用域选择用户".

For what you are describing, choose "User" for the scope.

现在,要访问代码中的值:

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类上有更多信息.

There's more information about all of this on MSDN here, and there is more information on the ApplicationSettingsBase class here.

(显然,如果您使用的是mvvm或任何其他UI模式,则可以在适当的时候修改此代码以将设置值加载到模型/视图模型中,而不是直接将其加载到文本框中)

这篇关于如何在TextBox中保存用户输入的值?(WPF,XAML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆