如何在 XML 中保存 WPF 用户设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13190342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:02:22  来源:igfitidea点击:

How to save WPF user setting in XML?

c#wpfxmllinq-to-xml

提问by 0070

I'm a WPF newbie. I'm building a simple WPF app which require the user to login using the C# language. In the login page, user allow to tick on the "Remember me" and the "Sign me in automatically" check box but if the app closes, the setting will be reset to default. So, what I want is after every time the user ticks the checkbox and close the app, the setting must be saved.

我是 WPF 新手。我正在构建一个简单的 WPF 应用程序,它要求用户使用 C# 语言登录。在登录页面,用户允许勾选“记住我”和“自动登录”复选框,但如果应用程序关闭,设置将重置为默认值。所以,我想要的是每次用户勾选复选框并关闭应用程序后,必须保存设置。

As what I found online, there are some ways to save the user setting. Can someone provide me a step-by-step tutorial or a link about saving the user setting into the XML?

正如我在网上找到的,有一些方法可以保存用户设置。有人可以向我提供有关将用户设置保存到 XML 的分步教程或链接吗?

P.S. I have some Linq experience. Linq to XML method is preferable by me.

PS我有一些Linq经验。我更喜欢 Linq to XML 方法。

回答by Kal_Torak

I'm not sure if this is a homework assignment requiring an XML file but this method is much easier than a custom XML solution and is built into the WPF application.

我不确定这是否是需要 XML 文件的家庭作业,但此方法比自定义 XML 解决方案容易得多,并且内置于 WPF 应用程序中。

Configuring Application/User Settings in WPF the easy way

在 WPF 中配置应用程序/用户设置的简单方法

P.S.Don't forget to call .Save()upon your setting changes!

PS不要忘记调用.Save()您的设置更改!