Browse DevX
Sign up for e-mail newsletters from DevX


Developing Plugins for Windows Live Writer : Page 2

Find out how to write plugins for Windows Live Writer, including how to get started, how to debug, and how to package the final plugin.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using Forms in Plugins
Windows Forms can be an integral part of your plugin, so it's important to know how to call them correctly from your plugin code. Forms can control exactly what users put into their blog entries. From inserting video URLs, selecting an emoticon from a list, or even searching for a book from Amazon; you can use Forms in a multitude of different ways.

You call the form from the CreateContent method:

public override DialogResult CreateContent (IWin32Window dialogOwner, ref string content) { using (frmMain form = new frmMain()) { DialogResult result = form.ShowDialog(); if (result == DialogResult.OK) content = form.NewContent; return result; } }

The preceding snippet of code displays a simple form with a property called NewContent, which returns the new content value to insert into the blog entry. Usually, the final code to be inserted by the plugin will be a property of your form.

Note that in the Accept button code in the form, you need this code:

this.DialogResult = DialogResult.OK;

Figure 6. Selecting Live Writer Plugins: The figure shows how to access your plugin's options in Live Writer.
That's so when the Form closes the if condition that checks for that result is actually met.

Using Settings
Settings, like Forms, can be an integral part of your plugin if you would like users to be able to set plugin defaults. Typically users would do this by going to the Options area of Live Writer, selecting the plugin, and then choosing the Options button as shown in Figure 6.

You must inform Live Writer that your plugin has an options form, which you do by making sure you set HasEditableOptions=true in the Plugin Attributes. Next, you have to create a Settings class, as this makes it easier to access the settings.

The Settings class also has to use the WindowsLive.Writer.Api, because you need to pass it an IProperties object through which your plugin will read/write the settings. The settings class constructor should look like the following:

IProperties m_properties; public PluginSettings(IProperties properties) { m_properties = properties; }

Note the global variable m_properties, which is used to call the IProperties in the class' Properties.

The Live Writer APIs understands five types of setting types: String, Int, Float, Bool, and Decimal. They all have their own Get and Set methods (GetString(), SetString(), etc.) and they're exposed as part of the IProperties class. So a simple property for a setting that gets saved using the Live Writer APIs will look like this:

public bool DefaultBoldOption { get { return m_properties.GetBoolean ("DEFAULTBOLD", true); } set { m_properties.SetBoolean ("DEFAULTBOLD", value); } }

Each Get method requires an attribute name (in this case DEFAULTBOLD) and a default value, so the plugin has something to fall back on the first time it's used. Each Set method requires the attribute name, like the Get method, and simply sets the attribute using the value passed. You will need to do this for all the settings you wish your plugin to have.

After you're happy with your Settings class, you need to initialize the settings from within the plugin code. Back in the original plugin class, add a global variable:

PluginSettings m_defaultsettings;

You then need to assign some PluginSettings to those default settings, so you need to override the Initialize() method:

public override void Initialize (IProperties pluginOptions) { base.Initialize(pluginOptions); m_defaultsettings = new PluginSettings(pluginOptions); }

Having the base.Initialize(pluginOptions) tells Writer to get the plugin settings from the registry for this particular plugin and put them into an IProperties class, which you then pass to the PluginSettings class that you created so you can read and write to them.

Now you have that done, you need to create your Options Form. For this example, I created a Form that looks like Figure 7. You will need to pass the PluginSettings object to the Form itself:

Figure 7. Sample Plugin Options Form: Here's how the options form looks for this sample plugin.

PluginSettings m_settings; public frmOptions(PluginSettings settings) { InitializeComponent(); m_settings = settings; // This sets the check box to be // whatever the default option was chBoldOption.Checked = m_settings.DefaultBoldOption; }

The Save button's Click event runs this code:

private void btnSave_Click (object sender, EventArgs e) { // Sets the option to whatever // the check box is currently at m_settings.DefaultBoldOption = chBoldOption.Checked; this.Close(); }

This example has only one setting, but the principle is the same however many settings your plugin has.

Back to the original plugin class, you now need to override the EditOptions method to call your Options Form:

public override void EditOptions (IWin32Window dialogOwner) { PluginSettings settings = new PluginSettings(this.Options); frmOptions op = new frmOptions(settings); op.ShowDialog(); }

All that's left to do is apply that setting to the plugin action. Change the CreateContent code as follows:

using (frmMain form = new frmMain()) { DialogResult result = form.ShowDialog(); if (result == DialogResult.OK) { content = form.NewContent; if (m_defaultsettings.DefaultBoldOption) { content = string.Format( "<b>{0}</b>", content); } } return result; }

Thanks for your registration, follow us on our social networks to keep up-to-date