Solution Options Persistence (Part 2)


It occured to me that I didn't discuss what the framework will do when the options you specified in the AddOptionsKey method are needed to be loaded or saved.


The framework will eventually call one of two virtual methods that you need to override.


        protected override void OnLoadOptions(string key, Stream stream) {
            // if key is a string this package cares about then
            // stream can be used to read the data for runtime use
            if (key == MyOptionKey)
                try {
                    BinaryReader reader = new BinaryReader(stream);
                    // read the options here...
                } catch (Exception) {
                    // no options, ok, no problem.
                }
            } else {
                // If this isn't a key this package cares about,
                // then call the base class implementation
                base.OnLoadOptions(key, stream);
            }
        }


        protected override void OnSaveOptions(string key, Stream stream) {
            // if key is a string this package cares about then
            // stream can be used to write the data
            if (key == MyOptionKey) {
                try {
                    BinaryWriter writer = new BinaryWriter(stream);
                    // write the options here...
                    writer.Flush();
                    stream.Flush();
                } catch (Exception) {
                    // no options, ok, no problem.
                }
            } else {
                // if this isn't a key this package cares about,
                // then call the base class implementation
                base.OnSaveOptions(key, stream);
            }
        }              


Allen

Skip to main content