Windows Live Writer SDK 提供三種擴充 API:Application API(啟動與編輯既有內容)、Content Source Plugin API(擴充新資料類型)、Provider Customization API(自訂功能)。本文以 Application API 為例,開發一個「將使用者輸入文字轉大寫並加入浮水印標記」的插入式外掛。
WindowsLive.Writer.Api.dll(位於 Live Writer 安裝目錄)與 System.Windows.Forms。[WriterPlugin("7c371eef-e350-4aae-af28-91613a9137e3", "Autumoon",
PublisherUrl = "https://www.Autumoon.com",
Description = "This is an Autumoon Lab plugin.",
Name = "Autumoon")]
[InsertableContentSource("iAutumoon", SidebarText = "iAutumoon")]
public class AutumoonPlugin : ContentSource
{
public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
{
new ProcessForm().ShowDialog(); // 彈出輸入視窗
content = ContentProcessor.ProcessedContent; // 取得處理後內容
return DialogResult.OK; // 告知 Writer 插入 content
}
}
WriterPlugin 屬性定義外掛中繼資料,第一參數為 GUID。InsertableContentSource 標示此為可插入內容來源,側邊欄顯示文字。content 為 ref 參數,回傳給 Writer 插入的 HTML 字串。// ProcessForm.cs - 簡單 WinForm 包含 TextBox 與 OK 按鈕
private void btnOK_Click(object sender, EventArgs e)
{
ContentProcessor.Process(txtInput.Text);
this.DialogResult = DialogResult.OK;
this.Close();
}
// ContentProcessor.cs - 靜態工具類別
public static string ProcessedContent { get; private set; }
public static void Process(string originalContent)
{
ProcessedContent = string.IsNullOrEmpty(originalContent)
? string.Empty
: string.Format("<p>{0}<br/><hr>-- Insert by <b>Autumoon Lab</b>.</p>",
originalContent.ToUpper());
}
注意:插入 Writer 的內容必須使用 HTML 標籤(如 <br/>),不可用 C# 跳脫字元。
Plugins 資料夾。掌握此模式後,可開發圖片浮水印、程式碼高亮、樣板文字、外部資料匯入等各式外掛,豐富撰寫體驗。