anakic / Jot

Jot is a library for persisting and applying .NET application state.
http://www.codeproject.com/Articles/475498/Easier-NET-settings
MIT License
633 stars 56 forks source link

WPF MVVM - how to save controls? #31

Closed Coke21 closed 5 years ago

Coke21 commented 5 years ago

Hi, I'm trying to use caliburn.micro and MVVM to save some controls in my app. This is the Constructor of my MainView (MainViewModel):

        public Tracker Tracker { get; set; } = new Tracker();
        public MainViewModel()
        {
            Tracker.Configure<MainViewModel>()
                .Id(p => p.MyMainWindow)

                .Property(p => p.RunHiddenCheckBox)

            Tracker.Track(this);
        }

That's how RunHiddenCheckBox property looks like:

        public bool RunHiddenCheckBox
        {
            get { return _runHiddenCheckBox; }
            set
            {
                _runHiddenCheckBox = value;
                if (RunHiddenCheckBox)
                {
                    RunAtStartUpCheckBox = true;
                    RunAtStartUpCheckBoxIsEnabled = false;
                }
                else
                {
                    RunAtStartUpCheckBoxIsEnabled = true;
                    WindowShowInTaskbar = true;
                }
            }
        }

MyMainWindow is just a string property. No exceptions are being thrown, however, the library doesn't create any folders and .json files. Any idea why? I guess I have to put .PersistOn somewhere but not sure where and what to put in. I tried: .PersistOn(nameof(PropertyChanged), RunHiddenCheckBox); But I'm getting NullReferenceException.


Update: Ok, I found the solution: .PersistOn(nameof(PropertyChanged), this); ^ I had to put the above to save all controls. I hope this will help somebody.

anakic commented 5 years ago

Hey, I missed your question. Yeah, you just needed to tell Jot when to persist the data to the store. You can skip the second argument if the target object (that's being tracked) is the source of the event:

.PersistOn(nameof(PropertyChanged));