Archives

Archives / 2009 / March
  • Silverlight 3,0 Split styles and templates into different files and merge resources

    Silverlight 3.0 now support a way to merge resources. So now we can split resources into different files. The following is a example of a simple resource file (ResourceA.xaml):


    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Style x:Name="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="red"/>
        </Style>
    
    </ResourceDictionary>


    When you create a resource file, make sure the Build Action is set to Resource for the file.

    If you want to globally want to merge different resource file you can for example do it on a application level by using the App.xaml file. To merge a Resource file, you need to use the ResourceDictionary object and it’s MergedDictionaries property, which takes ResourceDictionary objects as values. By using the ResourceDictionary’s Source property, you can specify the source for a resource file:


    <Application.Resources>
    
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceA.xaml"/>
                <ResourceDictionary Source="ResourceB.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    
    </Application.Resources>


    You can of course use a resource file on a User Control level:


    <UserControl x:Class="SilverlightApplication6.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:SilverlightApplication6.MyControl"
        Width="400" Height="300">
        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="ResourceA.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        
        <Grid x:Name="LayoutRoot" Background="White">
    
            <StackPanel>
                <Button Style="{StaticResource MyButtonStyle}"/>
                <Button Style="{StaticResource MyButtonStyleB}"/>
            </StackPanel>
    
        </Grid>
    </UserControl>


    If you only need to “include” on resource file, you don’t need to use the MergedDictionaries property. Here is an example where only one resource file is used:


    <UserControl.Resources>
        <ResourceDictionary Source="ResourceA.xaml"/>
    </UserControl.Resources>


    A resource file can in turn merge other resource files:


    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceB.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        
        <Style x:Name="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="red"/>
        </Style>
    
    </ResourceDictionary>


    Now I only want to see a way to skip adding the Style attribute to a control, and instead only use the TargetType attribute on a style, and all types should use that style:


    <Style TargetType="Button">
        <Setter Property="Background" Value="red"/>
    </Style>

    Wouldn’t that be something?

    Read more...

  • I’m going to talk about Silverlight 3.0 and .Net RIA Services

    For my Swedish readers. If you are interested to see some of the new features in Silverlight 3.0 and also the .Net RIA Services and out-of-‘browser feature, I’m going to talk about it on Developer Summit in Stockholm 15-16 April. I’m responsible for the Web track and there are be sessions about ASP.Net 4.0 AJAX, Dynamic Data, Building Web sites with Entity Framework etc. You can find more about the web sessions here: http://www.cornerstone.se/sv/ExpertZone/developersummit/2009/Startsida/Webb/

    Read more...

  • HTML is so 1990

    Yesterday I was on a meeting with some people from Microsoft and other companies working with social media and communication etc. We talked about the future of the web. I have developed web applications since 1995 or something like that and I’m so tired of is HTML, CSS and client-side script. During the last years people have done incredible web application with technology like AJAX to increase user experience. But HTML, Client-side script and the Browser we are using have limitations, it takes time to build application with rich and good user experience, some stuff are impossible to do because of a limitation both in the HTML, CSS, Client-side script and browser to create Rich Internet Applications. We need a new solutions, a new better way to build applications, and we have two “tools” we can use today to create richer web/internet applications, Flash and Silverlight.

    Read more...

  • on Twitter

    For some month ago I just created a Twitter account, and suddenly some poeple found me.. I didn't told anyone that I was on Twitter because I didn't know if I'm going to use it. But now when I got some fre followers I suddenly had to write something ;)

    So if you like to read my short and probably meaningless messages on twitter, you can find me @ http://twitter.com/fredrikn

    Read more...

  • Change IE8 to IE7 compatible mode with meta tags

    I got some questions regarding how to make sure IE8 will run in IE7 mode by default when visiting a site without changing the IE8 options.

    There is a meta tag which can be used to force IE8 to "display" a site in IE7 mode:

    Turn the mode to IE7:
     
    <meta http-equiv="X-UA-Compatible" content="IE=7">
     
    Make sure IE8 emulate IE7:
     
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>

    Read more...

  • MasterPage and Silverlight

    I have seen several post about how to implement MasterPage in Silverlight 2.0, most of the posts are about using “fixed” UserControls and dynamically change the content of a control in code-behind. Here is another solution where a Template will be used instead.

    Read more...