首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows Live Writer 外掛開發入門:Application API 實作指南

     編輯:關於.NET

Windows Live Writer SDK 提供三種擴充 API:Application API(啟動與編輯既有內容)、Content Source Plugin API(擴充新資料類型)、Provider Customization API(自訂功能)。本文以 Application API 為例,開發一個「將使用者輸入文字轉大寫並加入浮水印標記」的插入式外掛。

專案建立與參考

  1. Visual Studio 2008 建立 Class Library 專案。
  2. 加入參考: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# 跳脫字元。

部署與測試

  1. 編譯專案產生 DLL。
  2. 複製 DLL 至 Live Writer 安裝目錄下的 Plugins 資料夾。
  3. 重新啟動 Live Writer,側邊欄即會出現「iAutumoon」外掛。

擴充方向

掌握此模式後,可開發圖片浮水印、程式碼高亮、樣板文字、外部資料匯入等各式外掛,豐富撰寫體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved