Using Forms in SmartContentSource Plugins
Using forms is similar to the process already discussed when building a basic ContentSource plugin. The main difference is that instead of creating the final HTML and passing that back, you just save the settings into the PluginSettings class, which you can then use later in the plugin's main class methods.
Listing 4 shows the full code for the main form used in this example plugin.
The Sidebar (ContextEditor)
 | |
Figure 8. Live Writer Sidebar: The sidebar in Live Writer appears on the right-hand side when a user selects a SmartContentSource. |
When you select a SmartContentSource within the Live Writer editor, the editor activates a sidebar on the right-hand side of the Live Writer window, as shown in
Figure 8.
For this, you need to create a new User Control in your plugin project. I called mine "ContextEditor." Now, rather than inherit the UserControl interface, it needs to inherit from the SmartContentEditor interface (make sure you're using WindowsLive.Writer.API):
public partial class ContextEditor :
SmartContentEditor
The constructor for the editor has to be similar to the following or your plugin could end up with some strange behavior:
PluginSettings m_settings;
ISmartContent m_content;
public ContextEditor()
{
InitializeComponent();
this.SelectedContentChanged +=
new EventHandler(
SelectedContentNowChanged);
}
Notice how the code listens to the EventHandler. This is important because it's what detects that you have selected a different SmartContentSource object instance (don't forget, your user could have used your plugin to insert two separate objects in a blog entry).
The code for the
EventHandler method is also important to get right:
void SelectedContentNowChanged
(object sender, EventArgs e)
{
m_content = SelectedContent;
m_settings =
new PluginSettings(m_content.Properties);
textBox1.Text = m_settings.PlaceHolder;
textBox2.Text = m_settings.FinalText;
}
Important: You must not use
SelectedContent.Properties if you wish to get the IProperties property of the ISmartContent—that won't work. Instead, assign SelectedContent to a local variable, and then use that local variable when passing the IProperties object to the PluginSettings class.
As a best practice, you should also use this
EventHandler method to set the current settings to those in the sidebar or call the method that applies these settings.
If a user changes the content using the sidebar, nothing will change in the editor until you call the
OnContentEdited() method. You can call this from a single button after all changes or every time anything gets changed; it is up to you, as the developer, to decide when to update the editor.
Listing 5 shows the code needed for the sample plugin's ContextEditor.
Much Much More
If you look through the Live Writer APIs, you will find so much more that you can do with your plugins, including an excellent screen-scraping method that will take a screenshot of a given URL and return an image. I would highly recommend taking a good look at the rest of the
API documentation on MSDN.