Writing Plugins For Windows Live Writer – Using Settings In Your Plugin

So far I’ve shown you how to create basic plugins for Windows Live Writer, in this next guide I’m going to show you how you can use settings in your plugin to make things easier for your end user, you can create default options (that they can customise if you allow them). I’m going to show you how to do exactly that, have the user be able to change the default settings that are applied everytime the plugin is called. I will be carrying this on from my last guide so if you haven’t got that already, download the source now.

First thing’s first, what options does your plugin need? This is still going to be a simple plugin, so anything you put in can’t be changed once inserted, so the options and settings need to be something that the user would want. The simple plugin we have been using so far in this series has been to insert some simple text into the blog entry, so the option I’m going to use is to make it default to use a bold style for the text.

For the settings, we need to create a new Class that will contain all the settings’ properties. In this example, I have called it PluginSettings.cs. Before we call the constructor, we need to just declare an instance of the IProperties Interface: 

        private IProperties m_properties;
        private const string DEFAULTBOLD = "DEFAULTBOLD";

At this point, I also like to declare constants for the setting properties, these will be used in a bit, I will note that you don’t need to put these in, this is more a personal preference for me.

The settings class needs to be passed an IProperties instance so that it has a point of reference, then you need to set the IProperties instance you just declared to be the one that gets passed to the class. So your constructor should look like this: 

        public PluginSettings(IProperties properties)
        {
            m_properties = properties;
        }

Next we write the properties for the settings class, you will need one of these for each of the settings you use in your plugin and they can be one of five different types: int, string, boolean, decimal or float. For this plugin, I’m going to use a boolean type as I want it to either be yes (true) or no (false) for whether the text should be bold or not. This plugin only requires one property, and mine looks like this:  

        public bool DefaultBoldOption
        {
            get { return m_properties.GetBoolean(DEFAULTBOLD, true); }
            set { m_properties.SetBoolean(DEFAULTBOLD, value); }
        }

You’ll see that this is where we used the constant value I mentioned earlier, this is the property name that tells the plugin which setting to get, and you’ll see that there are two sections, a get section, and a set section. The get section must return a value of whatever type you’re using, in this case, a boolean value, so we use the IProperties’ GetBoolean method and put in the property name that we want. The set section is where we assign what the property’s value is going to be. This will all become clearer a bit further on. And that’s the settings class done with.

Now we move back to the plugin’s main class. To start off with, declare a PluginSettings object (I’ve called it m_defaultoptions), this will be set next. To do this, we need to override the Initialize method so that we can get the IProperties object for that plugin. Declaring the settings originally outside of this method means that the default options are available to the rest of the plugin. It should look like this: 

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_defaultsettings = new PluginSettings(pluginOptions);
        }

Notice how we pass the PluginSettings class the IProperties object so that it knows where it’s reading/writing from.

You might remember in the previous parts to this guide, in the WriterPlugin section we had HasEditbleOptions set to false, well now we’re going to set it to true, this will enable the Options button in the plugins part of the preferences. Before we tell the plugin about the options window, let’s create the form first. So add a new Windows form to the project (I called mine frmOptions.cs) and make your form look something like this:

Now, hit F7 and go into the code for the form and you will need to add the using WindowsLive.Writer.Api; namespace again. At the top, declare a new instance of the PluginSettings class, and in the constructor we need to then set that to be the PluginSettings that is being passed to the form, then apply the default settings. So your code should look like this:

        public frmOptions(PluginSettings settings)
        {
            InitializeComponent();
            m_settings = settings;
            // This sets the checkbox to be whatever the default option was
            chBoldOption.Checked = m_settings.DefaultBoldOption;
        }

Now these settings are only basic, so when we press the save button, there’s only one setting that needs to be saved, which we do with the following line:

        private void btnSave_Click(object sender, EventArgs e)
        {
            // This actually sets the option to be whatever the checkbox is currently at
            m_settings.DefaultBoldOption = chBoldOption.Checked;
            this.Close();
        }

Right, back to the main plugin class. Now that we have our options form, we need to tell the plugin what to do when the options button is pressed; so we need to override the EditOptions method. In this method, we are literally just going to create a new instance of the PluginSettings and use the stored options for that plugin, then pass that to the form, as follows:

        public override void EditOptions(IWin32Window dialogOwner)
        {
            PluginSettings options = new PluginSettings(this.Options);
            frmOptions op = new frmOptions(options);
            op.ShowDialog();
        }

Now, the final thing to do is actually apply these settings to the text that is put into the blog entry. For this plugin, this is simple as there is only one setting. So in the CreateContent method, we had the if statement to say that if (result == DialogResult.OK) then we make newContent = form.getText. All we will do for this plugin is add an extra if statement after that which checks whether the setting is to be applied or not, and as we have made this setting a boolean value, it makes it even easier to check. So the if statement we want is basically this:

                    if (m_defaultsettings.DefaultBoldOption)
                        newContent = string.Format("<b>{0}</b>", newContent);

And that’s it, you should be done

As always, the source code that I have used can be downloaded here.

Any problems, please leave a comment.

SL

Advertisements
  1. #1 by Unknown on January 21, 2008 - 3:41 pm

    wow gold wow gold wow power leveling wow power leveling wow powerleveling wow powerleveling wow power leveling wow gold wow gold World of Warcraft Gold rolex replica wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling power leveling power leveling power leveling rolex rolex power leveling power leveling power leveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling powerleveling powerleveling powerleveling wow gold powerleveling powerleveling powerleveling World of Warcraft Gold World of Warcraft Gold World of Warcraft Gold World of Warcraft Gold World of Warcraft Gold rolex replica Warcraft Gold Warcraft Gold Gold wow Gold wow Gold wow Gold wow Warcraft Gold Warcraft Gold 货架 机票 灭蟑螂 租房 租房 北京租房 北京租房 搬家公司 北京搬家 北京搬家公司 上海机票 上海机票 上海打折机票 上海打折机票 上海特价机票 上海特价机票 搬家公司 搬家公司 北京搬家公司 北京搬家公司 中高年 転職 アルバイト 求人情報 ブライダル 転職 競馬 FX ダイエット お見合い 競馬 予想 新築マンション 新築マンション コンタクトレンズ 婚約指輪 合宿免許 人材派遣 東京都 墓地 派遣会社 人材派遣 パチンコ 攻略 おなら dxghjgjhkk

  2. #2 by Unknown on April 8, 2008 - 3:45 am

    welcome to visit RF online blog Mmo blog Mmo guide blog Internet game guideTales of pirates forum ,it will give your some help.EVE online isk cheap EVE isk Guild wars gold Guild wars money GW gold Rs Money Wow gold Wow World of warcraft Gold Wow eu gold Wow us gold Maplestory mesos WOW GOLD worlf of warcrraft  World of warcraft gold Runescape RS gold RF online RF online cp RF online dalant  RF online disena  RF online money RF online gold Lotro gold Buy lotro gold  WOW gold  Cheap wow gold on sale. and you can sell your gold to us.

  3. #3 by on April 15, 2008 - 9:03 am

    wow gold!All US Server 29.99$/1000G on sell! wow gold,WOW Power Leveling-We supply the cheapest WOW PowerLeveling Service Online,with cheapest price and fastest of World Of Warcraft Gold and World Of Warcraft Power Leveling when purchaseing Honor Points/Professions Skills/Rep/World Of Warcraft PowerLeveling.
    Alcohol Tester purchasing center is a professional enterprise Breathalyser,Breathalyzer specializes in breath Gas Alarm consulting,Gas Detector through internet.Co Detector,Co Alarm always have been designed to alarm.
    数据恢复公司是北京市最大的仓储式弹簧销售企业,经销各种不锈钢弹簧。济南市龙威液压机械有限公司是专业生产液压升降机,是国内领先的升降机及升降平台的生产厂家。

  4. #4 by Unknown on May 6, 2008 - 12:51 pm

    キャッシング 比較
    銀行系キャッシング
    融資シミュレータ
    消費者金融 審査
    キャッシング比較
    信販系キャッシング
    ローン
    即日融資
    消費者金融
    レディースローン
    融資
    学生ローン
    即日融資
    おまとめローン
    キャッシング ローン
    低金利キャッシング
    キャッシング 申込
    一本化
    即日 キャッシング
    大口 融資
    低金利 キャッシング
    高額 融資
    低金利 ローン
    大口 ローン
    おまとめローン
    消費者 金融 審査 甘い
    カード キャッシング
    主婦 キャッシング
    ネット キャッシング

  5. #5 by Unknown on July 14, 2008 - 12:16 am

    主婦 キャッシング 銀行系 キャッシング キャッシング 即日融資 女性専用 キャッシング 一本化 ローン 一本化 借金 一本化 まとめ 一本化 銀行 おまとめローン カードローン 審査 ローン 借り換え 銀行系 カードローン レディースローン カードローン 低金利 即日融資 大口融資 高額融資 消費者金融 即日融資

  6. #6 by Unknown on September 12, 2008 - 10:49 am

    高卒求人 高卒求人 日総 高卒求人 日総 日総 風俗求人 池袋 風俗 高卒求人 高卒求人 日総 日総

  7. #7 by Unknown on September 30, 2008 - 4:12 pm

    Hi,Do you need advertising displays, digital signages, mp4 advertisement players, SD card players and advertisement LCD displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[cbdbfbabiciabd]

  8. #8 by Unknown on October 26, 2008 - 7:12 am

    Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[dbbdieccdhecbgh]

  9. #9 by Unknown on October 31, 2008 - 9:11 am

    Genuine TOSHIBA PA3465U-1BRS Laptop Battery PABAS069 laptop battery
    Battery for Toshiba PA3399U-1BRS PA3399U-2BAS 1BAS laptop battery
    6.6ah Battery for Toshiba B491 PA3166U laptop battery
    Battery For PA3383 TOSHIBA Satellite PA3383U-1BRS 12cells laptop battery

    Battery Fit Toshiba PA3382U-1BRS PA3384U-1BRS laptop battery
    6.6ah Battery Fit Toshiba PA3382U-1BRS PA3384U-1BRS laptop battery
    Toshiba PA3356U-3BAS PA3356U-3BRS PA3456U-1BRS Battery laptop battery
    Battery For Toshiba Portege M200 M205 PA3128U PA3191U black laptop battery
    Battery For Toshiba Portege M200 M205 PA3128U PA3191U white laptop battery

  10. #10 by Unknown on November 7, 2008 - 4:02 am

    hp presario r4000 battery
    hp presario pp2100 battery
    hp presario pp2200 battery
    hp pavilion nx9600 battery
    hp pavilion zv5000 series battery

    hp pavilion zv5200 battery
    hp pavilion zv6000 battery
    hp pavilion zd8000 battery
    hp pavilion nx9100 battery
    hp pavilion zt1000 battery
    hp pavilion zt1200 battery
    hp pavilion zt1100 battery
    hp pavilion zt1120 battery
    hp pavilion zt1130 battery

    hp f2299a battery
    hp f3172b battery
    hp f3172a battery
    hp f2019 battery
    hp f2019a battery
    hp f2019b battery
    hp omnibook 6000 battery
    hp omnibook 6100 battery
    hp omnibook xt6200 battery

    hp pavilion n6000 battery
    hp pavilion n6100 battery
    hp nx6100 battery
    hp nx6300 battery
    hp nc6100 battery
    hp nc6200 battery
    hp nc6400 battery
    hp nx7000 battery
    hp presario x1000 battery

    hp presario x1100 battery
    hp presario x1200 battery
    hp presario x1300 battery
    hp presario x1400 battery
    hp zt3000 battery
    hp zt3100 battery
    hp zt3200 battery
    hp zt3300 battery
    hp zt3400 battery

    hp l2000 battery
    hp dv1000 battery
    hp ze2000 battery
    hp dv4000 battery
    hp dv5000 battery
    hp presario c500t battery
    hp v2000 battery
    hp m2000 battery
    hp v4000 battery

    hp v5000 battery
    hp 320912-001 battery
    hp nc8230 battery
    hp nc8200 battery
    hp hstnn-db11 battery
    hp nc8200 battery
    hp 9400 battery
    hp ze2000 battery
    hp ze2200 battery

    hp zt4000 battery
    hp dv9000 battery
    hp dv9200 battery
    hp dv9100 battery
    hp dv9500 battery
    hp dv9600 battery
    hp dv9700 battery
    hp dv6000 battery
    hp hstnn-db42 battery

    hp dv2000 battery
    hp dv2200 battery
    hp dv6100 battery
    hp dv2300 battery
    hp dv2400 battery
    hp dv2500 battery
    hp dv2600 battery
    hp nc6140 battery
    hp nc4200 battery

    hp nc4400 battery
    hp tx2000 battery
    hp tx1000 battery
    hp hstnn-ob37 battery
    hp hstnn-ob38 battery
    hp hstnn-ub37 battery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: