简体   繁体   中英

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. Don`t know why. Can someone help me?

Here is compressed code:

View Datacontext is in the code behinde:

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

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

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

Thank you guys!

As pointed by Anton , you're missing the Save statement once changes have taken place.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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