ASP.NET MVC Template editing

by Tristan Smith 26. August 2009 13:43

If, like me, you’re building websites using ASP.NET MVC in Visual Studio 2008 but haven’t taken the time to create your own T4 templates, you can edit the 5 basic ones as follows:

Go to your equivalent of the following:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\CodeTemplates\

You’ll find the templates for Create, Details, Edit, Empty and List as well as Controllers.

Open the .tt files in your editor of choice, and edit away.

Why bother?

If you’re following Phil Haack’s article on Putting your pages and views on a diet which shows you how to remove the common aspects of views (default language, page & user base type and master page) this is a great place to implement that.

By editing the item templates themselves, you can remove the language tags and any other niggling things. I, for example, always remove the Title=”” from pages as titles are an obligatory page element in many sites.

Hope this helps.

Digg It!Del.icio.us

ObservableDictionary in Silverlight

by Neil Bostrom 17. June 2009 14:20

After having a requirement for an observable dictionary, I went searching and stumbled across Dr. WPF ObservableDictionary. This is a wonderful implementation for WPF. To port it to Silverlight, only a few changes were required.

Silverlight does not have ISerializable or IDeserializationCallback interfaces. These have to be removed and the associated implemented methods.

I also noticed that the class has two internal classes (KeyedDictionaryEntryCollection and Enumerator) that uses the same generic types as the outer class. This causes compiler warnings as below:

Warning    1    Type parameter 'TKey' has the same name as the type parameter from outer type 'ObservableDictionary<TKey,TValue>'

Simply changing the name of the inner generic types to something else will remove these warnings.

Otherwise, the ObservableDictionary class is geared up for Silverlight and works a treat. Great work Dr. WPF!

Digg It!Del.icio.us

Tags:

Silverlight

Auto resizing ListView columns

by Tristan Smith 8. May 2009 16:34

Surprisingly, Windows Mobile provides no overt mechanism for auto sizing ListView columns to the size of both the content of the rows AND the column headers.

Users expect this:

ideal

In order to do this exactly you have to manually specify the width of the columns. Here you’ll likely call upon the Graphics.MeasureString() method to get the exact size and resize the different columns until they fit both the column name and content.

For this pseudo-code wise you’ll need to do something like:

   1: Dictionary<int, int> columnIndexAndMinWidth;
   2: foreach(ColumnHeader header in listView.Columns)
   3: {
   4:     /*If Graphics.MeasureString(header.Text).Width > ALL of 
   5:     the MeasureString(listView.Item[columnHeaderIndex].Text).Widths for that 
   6:     column, set the column width to the column header width, otherwise set it to the 
   7:     largest listViewItem.*/
   8: }

Alternatively the APIs do give you the magic –1 and –2 that you can set a ColumnHeader’s width to.

   1: //codesnippet: D6896E9C-C411-428F-870C-2450EB80E79D
   2: //Autosize to max item width
   3: header.Width = –1;
   4: //Autosize to column width
   5: header.Width = –2;
Digg It!Del.icio.us

Accessing the memory card in Windows Mobile

by Tristan Smith 8. May 2009 15:06

We’ve been working on an internet enabled PDA interface to an existing web application.

Part of the functionality is to allow signature capture and photographs taken on the device to be sent to the server. We’re using web services as the mechanism to do the transfer.

Due to the small amount of memory on mobile devices which they need for important things such as the operating system, we’re saving the photos and signature images to an SD card.

Programmatically it can be hard to determine which of the Directories is onboard memory and which is Storage card.

Here’s a handy snippet to give you that.

   1: //codesnippet:06EE3DE0-D469-44DD-A15F-D8AF629E4E03
   2: public string GetStorageCardFolder()
   3: {
   4:    string storageCardFolder = string.Empty;
   5:    foreach (string directory in Directory.GetDirectories("\\"))
   6:    {
   7:        DirectoryInfo dirInfo = new DirectoryInfo(directory);
   8:  
   9:        //Storage cards have temporary attributes do a bitwise check.
  10:        //http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=612136&SiteID=1
  11:        if ((dirInfo.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary)
  12:            storageCardFolder = directory;
  13:    }
  14:  
  15:    return storageCardFolder;
  16: }

(We support Jeff Attwood's idea for flagging code snippets with GUIDs.)

In my specific instance I do an added lazy load check to avoid hitting this method multiple times and it’s a static. I’ve removed that from the snippet.

Digg It!Del.icio.us

Silverlight FX

by Neil Bostrom 7. May 2009 15:20

Treehouse are busy creating a bunch of silverlight related applications. One of the frameworks we are finding very helpful during our development is Silverlight FX. One of the great features of this framework is flexible theme support. Silverlight 2 currently does not really support themes, the main reason behind this is that styles can only be loaded once. It also does not support any type of style inheritance. Most of these features are on the list for Silverlight 3.

Theme your Silverlight 2 application

To add theme support to your existing application, a few steps are required. Add a reference to the SilverlightFX dll. Get your app class to inherit from XApplication. To get the generated half of the class to match, update your xaml file to start wih the following tag:

   1: <fxa:XApplication 
   2: xmlns:fxa="clr-namespace:SilverlightFX.Applications;assembly=SilverlightFX"> 

Once you have done this, you can set the theme on the fly or in your xaml using ThemeName="$themeName". The XApplication class will then search your application for a /Theme directory and look for a directory matching the name of the theme. Once it finds that it will import any styles found in the Theme.xaml file in that directory.

For more information on theming using the SilverlightFX framework, please read Nikhil's tutorial.

Great work Nikhil Kothari!

Digg It!Del.icio.us

Powered by BlogEngine.NET 1.5.0.7