2006年11月30日 星期四

為 Windows Live Writer 寫個 Plugin 吧

http://richielin-programer.blogspot.com/2006/11/windows-live-writer-plugin.html

Windows Live Writer 還真是個好東西
不且離線寫 Blog 方便好用,連拿來修改以前 Blog 寫的文章也是好用
雖然有點 Bug,但是看在 Beta 的面子上,天秤我還是給它很高的評價thumbs_up

不只如此,連寫個 Plugin 也十分容易
只要有 C#.NET 的基礎,再邊看範例邊上手
大概 10 分鐘就可以寫個簡單的 Plugin 來騙吃騙喝了
下面是我簡單寫下開發的步驟,沒什麼入門的東西
不過倒可以當作寫 Plugin 的入門,繼續看下去...

首先第一步就是用 VS2005 建立 Library 專案
加入參考,Windows Live Writer 目錄下 WindowsLive.Writer.Api.dll
產生一個類別來繼承 SmartContentSource
並加上 WriterPlugin 屬性及 InsertabllContentSource 屬性
WriterPlugin 參數為 id, Plugin 名稱, 圖示檔名
InsertabllContentSource 參數為 在 WLW 中的顯示名稱

using System;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace HelloWorldPlugin
{
[WriterPlugin( "F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72",
"Smart HelloWorld",
ImagePath = "Images.InsertHelloWorld.png")]

[InsertableContentSource( "Smart Hello World" )]
public class SmartHelloWorldPlugin : SmartContentSource
{
}
}

在專案中加入個圖示,並指定為內嵌資源


接下來覆寫幾個函式



  • public override void Initialize(IProperties pluginOptions)
    初始物件,記錄下 pluginOptions 物件
  • public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
    建立及顯示對話框
    這裡必須產生對話框供使用者設定或輸入相關參數
    並將參數寫到 newContent.Properties 物件裡
    如果想當下一次預設值,也必須將參數寫到 pluginOptions 物件裡
  • public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext )
    依 content 產生並回傳 HTML 語法
    在 publishingContext 物件會帶一些 Blog 的資訊供參考
  • public override SmartContentEditor CreateEditor(ISmartContentEditorSite contentEditorSite)
    如果使用者在編輯時點到剛才產生的 HTML 區
    必須在 SideBar 區出現視窗供參數設定
    此時必須回傳一個繼承 SmartContentEditor 物件的控制項

最後在設定專案屬性的建置後事件命令列加入以下命令
編譯後的 Library 就會直接被 Copy 到 Windows Live Writer 中
再來執行 Windows Live Writer 就會在 SideBar 看到 Plugin 了

XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live Writer\Plugins\"

簡單覆寫 4 個函式,產生二個視窗
就可以有彈性的增加 Windows Live Writer 功能
這一次 Microsoft 做得還真不錯

2 意見:

Unknown 提到...

Hi Richie

Thank you for sharing. I will give the Windows Live Write plug-in a try. I hope you don't mind if I contact you with some questions later.

I am just started to learn C#/.NET. Any good suggestions on how to improve my coding skill?

Michael

icools2 提到...

剛在writer目錄發現一些dll,於是就import看看,原來他的api在這邊

後來查到這個網站,剛剛實做了一下果然挺簡單的

現在writer似乎改為如此
"C:\Program Files\Windows
Live\Writer\Plugins"