Use WPF Dispatcher to invoke event handler only when needed
After floundering a bit with the WPF Dispatcher I've come up with a simple way to make sure an event handler executes on the UI thread without paying the overhead of always invoking a delegate.
This has the benefit (for me at least) of being very easy to remember. Hook up the event handler and then if there's a chance it could be called from a different thread wrap it using the pattern above. It's easier to read than an anonymous delegate and much faster than defining a specific delegate for the event in question.
I haven't tested the various methods to see which is the fastest yet… will get round to this at some point.
Related Posts
- WPF commands with nested focus scope
- Merging Resource Dictionaries for fun and profit
- UAC shield icon in WPF
- XamlParseException and 256x256 icons
- Outlook/Office iCal feed 400 bad request error with C# WebClient
(Published to the Fediverse as: Use WPF Dispatcher to invoke event handler only when needed #code #wpf #tips How to Invoke or BeginInvoke an event handler to the UI thread using the WPF Dispatcher. )
XamlParseException and 256x256 icons
When testing out a WPF app on XP I got an unhelpful XamlParseException error report.
I was a little puzzled because I was hooking up error reporting in App.xaml.cs:
My error handler was attempting to create a XAML window to report the error, and evidently this was bombing out as well triggering the good doctor Watson. I added a MessageBox call instead and discovered that the XamlParseException was wrapping a FileFormatException and the stack trace indicated that the problem was with setting the icon for the window. After removing the icon the app started up fine. Weird.
It turns out that WPF chokes on a compressed 256x256 icon on XP and Vista (Windows 7 seems to cope fine). Saving the icon without compression fixes the problem. I use IcoFX and you can set this at Options -> Preferences -> Options -> Compress 256x256 images for Windows Vista. Of course the consequence is that the icon is a couple of hundred kilobytes larger.
Related Posts
- UAC shield icon in WPF
- BadImageFormatException for a 64-bit ASP MVC web application
- Crushing PNGs in .NET
- Merging Resource Dictionaries for fun and profit
- The Secret Diary of a Xamarin Android Developer, Aged 48 1/3
(Published to the Fediverse as: XamlParseException and 256x256 icons #code #xaml #wpf #wtf You can't use a compressed 256x256 icon with WPF but you can include an uncompressed version. )
Space and multibyte character encoding for posting to Twitter using OAuth
I've spent the last day learning how to use OAuth and XAuth to post to Twitter. There are rumblings that Twitter will start to phase out basic authentication later this year, and more importantly you can only get the nice “via...” attribution if you use OAuth (for new apps, old ones are grandfathered in).
I coded up my own OAuth implementation, referring to Twitter Development: The OAuth Specification on Wrox and the OAuthBase.cs class from the oauth project on Google Code. Both are great references, but both fail with multibyte characters. The problem is that each byte needs to be separately escaped. OAuthBase.cs encodes characters as ints rather than breaking out the bytes and the Wrox article incorrectly suggests using Uri.EscapeDataString().
Here's a method to correctly encode parameters for OAuth:
NoEncode chars is a list of the permitted characters:
An impact of this encoding is that spaces must be encoded as %20 rather than plus. I was worried that each space would end up counting as three characters towards the 140 character limit. I tested this and it isn't true, so use HttpUtility.UrlEncode() to calculate the number of characters in the post OAuthUrlEncode() or similar to actually encode post parameter.
Related Posts
- Twitter: Put some status in status updates
- HOWTO: Fix Twitter
- OAuth
- Twitter's API has got too painful for me
- How to get SEO credit for Facebook Comments (the missing manual)
(Published to the Fediverse as: Space and multibyte character encoding for posting to Twitter using OAuth #code #twitter #oauth #xauth How to encode spaces and multibyte characters when posting to Twitter via OAuth / XAuth. )
BlogEngine.NET most popular pages widget using Google Analytics
I finished off my BlogEngine.NET migration yesterday missing a couple of useful sections from the previous incarnation of this blog. The first is a list of the most popular posts based on Google Analytics data. I've just finished porting this from a UserControl to a widget for BlogEngine.NET. To use this just download and extract this zip file to your widgets directory:
MostPopularFromGA.zip (5.22 kb)
You can see the widget in action under the Most Popular heading to the left if you're reading this post on the blog.
Most of the settings should be pretty obvious. The Google Analytics profile is the exception. This isn't the ID included in your tracking code. To get the profile log in to Google Analytics and click Edit next to the profile you want to use. At the top of the page you'll see a Profile ID. You need to use this number prefixed by ga: (i.e. ga:1234567). Once you have this and your account credentials entered you should be up and running.
The Post must match settings is a regular expression used to filter the Google Analytics report to only include blog posts. The default value corresponds to a default BlogEngine.NET install and only includes pages that start .../post/ (the regular expression is ^/post/). If your posts are under .../blog/posts/ then just update accordingly (^/blog/posts/). If you want to include the most popular pages regardless of the path just leave this setting blank.
If you have any questions or feature requests leave a comment below.
Related Posts
- Migrating from Blogger to BlogEngine.NET
- Migrating a C# Integration from GA3 to GA4
- Geotagging posts in BlogEngine.NET
- The curious case of the missing slugs (in BlogEngine.net 2.8)
- Converting Blogger ATOM export to BlogML
(Published to the Fediverse as: BlogEngine.NET most popular pages widget using Google Analytics #code #blogengine.net #googleanalytics A BlogEngine.NET most popular posts widget using data from the Google Analytics API. )
Migrating from Blogger to BlogEngine.NET
(Update June 22, 2010: I've released a tool, Blogger2BlogML, that converts Blogger's ATOM export file to BlogML. I ended up doing this because of problems with comments when I migrated this blog — I had to fix these up manually which was painful. I'm now working on some larger blogs where this would be impossible…)
In January I got an email from Blogger announcing that they're killing FTP support. Apparently only 0.5% of Blogger blogs are published using FTP and it's a huge pain to support, mainly because many hosting providers restrict FTP access to certain IP addresses and if the Google servers running Blogger that moment aren't listed it's technical support time.
Fair enough, but a bit painful for me as I have five blogs running on top of Blogger. I need FTP publishing as the templates I use end up running as ASP or ASP.NET pages. I Thought He Came With You is the first to move - if you're reading this post then it's up and running on BlogEngine.NET. This is an open source ASP.NET blogging platform. If it works out for this blog over the next month I'll start migrating the others.
Getting up and running with BlogEngine.NET is easy enough - download the latest release and follow the getting started guide. I added the default install to a new Visual Studio web site project and was able to run it fine in the development server, no need to configure IIS.
The challenge was moving posts and comments from Blogger into BlogEngine.NET. BlogEngine.NET happily imports and exports BlogML, Blogger spits out it's own Atom export format.
Luckily Aaron Lerch has knocked up a PowerShell script to export Blogger to BlogML. This takes your Blogger ID as a parameter and exports all the blogs associated with that ID. You can just pull the <blog> element you need out of the export if necessary. If you use this tool follow the syntax closely from the example on the post. If you're new to PowerShell run this as administrator to start with and enter "Set-ExecutionPolicy RemoteSigned" to give permission to run the script. Then exit and run a non-admin instance of PowerShell to run the script.
After getting the export BlogEngine.NET refused to import it. I kept getting an invalid username or password error from the BlogML importer. Digging into the code a bit I found that a crash was occurring in api/BlogImporter.asmx when a blank category was passed to AddCategories(). I patched this function to skip blank/null categories and was then able to import successfully. I added a bug report for the problem so please vote for it if you hit the same issue.
The blog was in pretty good shape after the import. Categories were lost so I had to create them again in BlogEngine.NET and then apply them to each post. I also found that the date for comments had been set to the date they were imported. This can be fixed by editing each post directly (the files in App_Data\posts). I fixed the timestamps and also added back the URL for each comment where I had one from Blogger. I also edited my own comments to match my new BlogEngine.NET account so they got decorated as "by the author".
At this point the blog was good to go, but using the default template.
I've now whipped up a new template to match the old blog - I was pleasantly surprised by how easy this was to do. At this point I Thought He Came With You is going live on BlogEngine.NET.
Almost everything is working at this point. I had a couple of user controls I need to move over - one shows the number of people who haven't visited the blog, the other figures out the most popular posts using the Google Analytics API. I'll see if I can implement at least the latter as a Widget so that other people can use it - watch this space (Update - now available here).
Overall, I'm impressed. I resisted the urge to just write my own blogging platform which is a testament to how easy it is to both get BlogEngine.NET running and to customize the look and feel. Now I just need to work up the enthusiasm to do the same for my remaining four Blogger based blogs...
Related Posts
- Converting Blogger ATOM export to BlogML
- Commentary
- The curious case of the missing slugs (in BlogEngine.net 2.8)
- Convert BlogML comments to WXR for Disqus
- BlogEngine.NET most popular pages widget using Google Analytics
(Published to the Fediverse as: Migrating from Blogger to BlogEngine.NET #code #blogger #blogengine.net Moving a Blogger FTP published blog to BlogEngine.NET, converting Blogger to BlogML, fixing import problems. )
Catfood.Shapefile.dll 1.10
I've just released v1.10 of my ESRI Shapefile Reader (Catfood.Shapefile.dll). This is a .NET 2.0 forward only parser for reading shapefile content.
Sharon Tickell was kind enough to report two bugs with suggested updates. These have both been fixed in 1.10.
While working on these fixes I also discovered that there are no 64-bit Jet drivers (since releasing the first version I've upgraded to a 64-bit box for development). This is an easy fix, just target any application using Catfood.Shapefile.dll at x86. I've updated the demo application accordingly.
Download Catfood.Shapefile.dll from GitHub.
Related Posts
Ambient Orb Controller .NET Library
The Ambient Orb is (was) an LED illuminated globe designed to display subtle information - stock market prices, weather, etc. Normally the Orb is controlled via the pager network but you can buy (or build) a developer module and connect the Orb via your serial port.
I've used my Orb via both pager and serial port for a number of applications. I've just released a library - Ambient Orb Controller - on GitHub that supports both methods of control. I wrote the library to make it easier for me to gin up new Orb applications. If you use an Orb let me know what you come up with. Two of my favorites are using the Orb for continuous integration and as a music visualizer.
Related Posts
- CodePlex Shutdown: Shapefile, Orb, StackHash and Blogger2BlogML Migration
- 3D Printing a discreet wall mount shelf for the Aura Carver Mat
- ZoneInfo Update (tzdata for .NET)
- Control LIFX WiFi light bulbs from Google Apps Script
- Animation of a year of Global Cloud Cover
(Published to the Fediverse as: Ambient Orb Controller .NET Library #code #random .NET library to control the Ambient Orb (available on GitHub). )
Finding NPR on Windows Mobile
Now here's a niche:
I'm occasionally jealous of an iPhone app, in this case a station finder for NPR. I hate hunting on a long drive or in a strange city. Luckily, NPR has an API, so I've whipped up an NPR Station Finder for Windows Mobile.
Related Posts
Catfood: Earth and Follower
I've been busy updating a couple of Catfood products.
Catfood Earth updates your desktop wallpaper to show day, night and cloud cover. It can also display earthquakes, time zones, places and US weather radar:
Catfood Follower is a Twitter tool that automatically follows people who follow you and/or removes friends who stop following you. I think it's most useful for business accounts:
With these two out the door my next project is overhauling the Catfood website. I'm kind of dreading it, but the current architecture is over six years old and it's definitely showing its age.