简体   繁体   English

从 chache 获取用户设置在 WPF MVVM C# 中不起作用

[英]Get user setting from chache don`t work in WPF MVVM C#

I like to save the user input to the user settings and after every restart of the app I like that they get the "new" string from the settings.我喜欢将用户输入保存到用户设置中,并且在每次重新启动应用程序后,我喜欢他们从设置中获取“新”字符串。

But everytime the user settings proparty are empty.但是每次用户设置proparty都是空的。 Don`t know why.不知道为什么。 Can someone help me?有人能帮我吗?

Here is compressed code:这是压缩代码:

View Datacontext is in the code behinde: View Datacontext 在后面的代码中:

<UserControl.Resources>
    <m:Model x:Key="model"/>
</UserControl.Resources>

<TextBox x:Name="txtbx_user" Text="{Binding UserName}"/>

Model: Model:

private string _userName;

        public string UserName
        {
            get
            {
                if (!String.IsNullOrEmpty(Properties.Settings.Default.UserName))
                {
                    return Properties.Settings.Default.UserName;
                }
                else
                {
                    return _userName;
                }
            }
            set
            {
                _userName = value;
                OnPropertyChanged("UserName");
                Properties.Settings.Default.UserName = _userName;
            }
        }

And yes I have the already set the string in the properties of the project.是的,我已经在项目的属性中设置了字符串。

Why I have every time if I restart the app a empty string in the Properties.Settings.Default.UserName为什么我每次重新启动应用程序时在 Properties.Settings.Default.UserName 中都有一个空字符串

Thank you guys!感谢你们!

As pointed by Anton , you're missing the Save statement once changes have taken place.正如Anton所指出的,一旦发生更改,您就会丢失Save语句。

Properties.Settings.Default.UserName = _userName;
Properties.Settings.Default.Save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM