anakic/Jot

WPF MVVM - how to save controls?

Closed this issue · 1 comments

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.

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));