Amazon.com Widgets

Creating a Windows Live Writer Plug-in using C# Part 2

First we will start off with the Plug-in class that inherits from ContentSource.  Please note from Part 1 that “InsertForm” is a class we will create.

 

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;

   5: using WindowsLive.Writer.Api;

   6: using System.Windows.Forms;

   7:  

   8: namespace AmazonQuickLinkPlugin

   9: {

  10:     [WriterPlugin(

  11:         "93DD80D7-9327-4375-A9D8-777C0549C1C8", 

  12:         "Amazon Quick Link", 

  13:         PublisherUrl="http://www.whatifoundout.com",

  14:         Description="Creates a Amazon Quick Link url",

  15:         ImagePath="Images.Shazam.png")]

  16:     

  17:     [InsertableContentSource("Amazon Quick Link")]

  18:     public class Plugin : ContentSource

  19:     {

  20:         public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)

  21:         {

  22:             using (InsertForm form = new InsertForm())

  23:             {

  24:                 DialogResult result = form.ShowDialog();

  25:                 if (result == DialogResult.OK)

  26:                     content = form.Link;

  27:                 

  28:                 return result;

  29:             }

  30:  

  31:         }

  32:     }

  33: }

Now that we have the basics lets create the “InsertForm” form.  First a capture of what I made it look like and then the code.

image

   1: using System;

   2: using System.Collections.Generic;

   3: using System.ComponentModel;

   4: using System.Data;

   5: using System.Drawing;

   6: using System.Linq;

   7: using System.Text;

   8: using System.Windows.Forms;

   9:  

  10: namespace AmazonQuickLinkPlugin

  11: {

  12:     public partial class InsertForm : Form

  13:     {

  14:         public string ASIN { get; set; }

  15:         public string Category { get; set; }

  16:         public string SearchTerm { get; set; }

  17:         public string InnerText { get; set; }

  18:         public string Link { get; set; }

  19:  

  20:         public InsertForm()

  21:         {

  22:             InitializeComponent();

  23:         }

  24:  

  25:         private void btnOK_Click(object sender, EventArgs e)

  26:         {

  27:             AssignProperties();

  28:             this.Link = CreateAmazonQuickLink();

  29:             this.DialogResult = DialogResult.OK;

  30:             Close();

  31:         }

  32:  

  33:         private void btnCancel_Click(object sender, EventArgs e)

  34:         {

  35:             this.DialogResult = DialogResult.Cancel;

  36:             Close();

  37:         }

  38:  

  39:         private void AssignProperties()

  40:         {

  41:             //TODO: Add validation of ASIN format

  42:             this.ASIN = txtASIN.Text.Trim();

  43:             //TODO: Get categories from Amazon

  44:             this.Category = txtCategory.Text.Trim();

  45:             this.SearchTerm = txtSearchTerm.Text.Trim();

  46:             this.InnerText = txtInnerText.Text.Trim();

  47:         }

  48:  

  49:         private void btnPreview_Click(object sender, EventArgs e)

  50:         {

  51:             AssignProperties();

  52:             txtPreview.Text = CreateAmazonQuickLink();

  53:         }

  54:  

  55:         private string CreateAmazonQuickLink()

  56:         {

  57:             StringBuilder s = new StringBuilder();

  58:             s.Append("<a type=\"amzn\" ");

  59:  

  60:             if (ASIN != string.Empty)

  61:                 s.Append("asin=\"").Append(ASIN).Append("\" ");

  62:             else

  63:             {

  64:                 if (SearchTerm != string.Empty)

  65:                 {

  66:                     s.Append("search=\"").Append(SearchTerm).Append("\"");

  67:                     if (Category != string.Empty)

  68:                         s.Append("category=\"").Append(Category).Append("\"");

  69:                 }

  70:                 else

  71:                     return string.Empty;

  72:             }

  73:  

  74:             s.Append(">").Append(InnerText).Append("</a>");

  75:  

  76:             return s.ToString();

  77:         }

  78:  

  79:     }

  80: }

I’ve added a few TODO’s but for the most part it is done and works great.  One other thing I might note.  I never did get the XCOPY command they give you in the documentation to work after the first compile.  I had to keep deleting the DLL I created in the WindowsLive Writer plug-in directory.

Here is the Amazon Quick Linker plug-in in action.  Let me recommend one of my favorite books on estimation.  Steve McConnell just knows how to put it in a way I understand.  When I read his book Software Estimation: Demystifying the Black Art (Best Practices (Microsoft)) it really opened my eyes and made my programming more enjoyable.  I hope it will do the same for you.

Comments are closed.