Blog posts about codeproject

  • A Smarter Way to Include JavaScripts and CSS to Reduce Page Load Times

    April 18, 2011 | Tags: Javascript, ASP.NET, Open source, CodeProject, CSS, C#
    Updated my open source project SmartInclude which handles runtime compression and minifying javascript and stylesheet.

    The project have been constructed to be:
    • Powerful - use well proven compressors from YUI together with automatic GZip to further decrease the file transfer.
    • Easy to use - just replace your script-tags with a custom webcontrol to implement in your project.
    • Easy to develop with - set a single property in your web.config to disable the compression to help debugging scripts.

    The main idea is to have all scripts and css-files in a readible form on the server and compile them to one compressed and minified file when transfered to the visitor in order to decrease bandwidth and most important of all: load time of the page.

    The compressed file is cached client side with a hash stamp that makes the web browser request a new file if it has changed since last request from the server.

    Take a look at the article describing the project:
    SmartInclude article and code at CodeProject

    You can always find the latest code over at smartinclude.codeplex.com.

  • Get height of TextBlock in WPF

    May 21, 2010 | Tags: WPF, XAML, CodeProject, C#, Snippet
    Sometimes it's quite nice to get the height of a textblock at the same time it's created. In WPF however there's a couple of events that handle the layout functions when an element is rendered, and before these have been fired, you won't get proper values from the DesiredSize property. The good news is that these can be triggered by the Arrange function. You call the Arrange function with a Rect (rectangle) representing the column width and max height (example below use 1000px, but you might need more for longer texts) of your text.

    TextBlock t = new TextBlock();
    int columnWidth = 310;
    t.FontFamily = new FontFamily("Arial");
    t.FontSize = 20;
    t.Text = "Here goes the text..";
    t.Width = columnWidth;
    t.TextWrapping = TextWrapping.Wrap;
    t.Arrange(new Rect(0,0,columnWidth,1000));
    int textHeight = (int)t.DesiredSize.Height;