How can I save/load a HighlightStyleRegistry?

SyntaxEditor for WPF Forum

Posted 14 years ago by Craig - Varigence, Inc.
Version: 11.1.0541
Avatar
I want to persist any changes the user makes to highlighting styles between runs of my application. Is there a 'right' way to load/save the HighlightingStyleRegistry?

Comments (9)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

We don't have anything built-in at the moment to do it. I've added that to the TODO list.

Your best bet in the meantime is to just iterate over the registry and save each value in the IHighlightingStyles. Then later, restore those again. You probably want to restore all that at app startup to make sure it's properly picked up by any lexers.


Actipro Software Support

Posted 14 years ago by Craig - Varigence, Inc.
Avatar
Can you clarify the part about lexers and startup? Along with saving/loading, I want the user to be able to revert their styling changes and return to the defaults. I assumed I could simply reload their last saved changes into a HighlightingStyleRegistry and load that into the SyntaxEditor. Was your concern about lexers regarding using the global Ambient registry?
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
It should pick up changes since an event is fired as the registry changes, so everything should still work at run-time when you change it. It just will be slightly faster to initially load before any editors are attached to the event.


Actipro Software Support

Posted 14 years ago by Craig - Varigence, Inc.
Avatar
So, I'm trying to write some load/save logic but loading never seems to work. I have my SyntaxEditor control in a UserControl. In the UserControl's constructor, I do the following:

InitializeComponent();

_xmlSchemaResolver = new XmlSchemaResolver();
LoadSchema(XmlSchema);

_xmlSyntaxLanguage = new XmlSyntaxLanguage();
_xmlSyntaxLanguage.RegisterXmlSchemaResolver(_xmlSchemaResolver);

syntaxEditor.Document.Language = _xmlSyntaxLanguage;

LoadSavedHighlightingStyles();  
For LoadSavedHighlightingStyles, I am attempting to create a new HighlightingStyleRegistry and store my saved highlighting styles within it, like so:

bool loadSucceeded = true;

_editorFontSettings = new EditorFontSettings();
_editorFontSettings.Reload();

if (_editorFontSettings.FontStyles != null && _editorFontSettings.FontStyles.Count > 0)
{
    _loadedStyleRegistry = new HighlightingStyleRegistry();
    _loadedStyleRegistry.Description = "Biml Editor";
    new DisplayItemClassificationTypeProvider(_loadedStyleRegistry).RegisterAll();

    try
    {
        foreach (var fontStyle in _editorFontSettings.FontStyles)
        {
            var highlightStyle = new HighlightingStyle();
            if (fontStyle.IsForegroundSet)
            {
                highlightStyle.Foreground = new BrushConverter().ConvertFromString(fontStyle.Foreground) as Brush;
            }

            if (fontStyle.IsBackgroundSet)
            {
                highlightStyle.Background = new BrushConverter().ConvertFromString(fontStyle.Background) as Brush;
            }

            _loadedStyleRegistry.Register(new ClassificationType(fontStyle.Key, fontStyle.Description), highlightStyle, true);
        }
    }
    catch (Exception)
    {
        loadSucceeded = false;
    }
}

if (!loadSucceeded)
{
    _loadedStyleRegistry = AmbientHighlightingStyleRegistry.Instance;
}

syntaxEditor.HighlightingStyleRegistry = _loadedStyleRegistry;
However, even when I load style information successfully and assign my registry to the HighlightingStyleRegistry property, my text color changes don't appear in the SyntaxEditor. Instead, all the text is black. Is there something else I need to do so the SyntaxEditor will apply my new registry?

Thanks,

-Craig
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

It's hard to say without something to debug. Can you make a new simple sample project that shows it and email it to us? Please don't include any .exe files in the ZIP and rename the .zip file extension so it doesn't get spam blocked.


Actipro Software Support

Posted 14 years ago by Craig - Varigence, Inc.
Avatar
Just sent over a sample that reproduces the issue.

For the sample, just make some highlighting style changes in the text editor (not console editor) and then close/reopen the application. We'll save and reload the styling changes but they won't be applied in the text editor when run the next time.

Thanks,

-Craig

[Modified at 03/07/2011 02:05 PM]
Posted 14 years ago by Bradley
Avatar
Was this issue ever resolved? I'm running into a similar problem where I'm trying to create a new registry and the code editor is not picking up the new registry.

            IHighlightingStyleRegistry registry = new HighlightingStyleRegistry();
            new DisplayItemClassificationTypeProvider(registry).RegisterAll();
            registry.Register(ClassificationTypes.Keyword, new HighlightingStyle(Brushes.HotPink));
            registry.Register(ClassificationTypes.Comment, new HighlightingStyle(Brushes.Yellow));
            codeEditor.HighlightingStyleRegistry = registry;
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Bradley,

From our private conversation with Craig (previous poster), the issue should have been resolved in the latest maintenance release build 542.


Actipro Software Support

Posted 14 years ago by Bradley
Avatar
Thanks, that did the trick.
The latest build of this product (v24.1.3) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.