Design time resources and Attributes in Silverlight/WPF/WP7

When you open a Silverlight/WPF/WP7 application in design mode in Visualstudio or Expression Blend, we may not see the exact look as we expect and see at runtime because of designtime.

There are several reasons for this, major reasons are:

–          Controls are not embedded in a view(Majorly this problem will come if we don’t give width and height)

–          Constructor of a root element is not called.

–          ViewModel is not created

to avoid this you need to provide information to make design view correctly.  You can specify this kind of information by using the design-time attributes.

The designtime attributes are defined in a special namespace, that is usually mapped to the d: prefix. To tell the XAML parser not to interprete these attributes at runtime, the markup compatibility namespace is mapped to mc: and with the mc:Ignorable="d" instruction, the d: namespace is excluded.

EX:

 <UserControl
   xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d" />

d:DesignHeight and d:DesignWidth:

The d:designHeight and d:DesignWidth are used to provide fixed height and width at design time.

EX:

<UserControl
    xmlns="http://schemas.microsoft.com/..."
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    d:DesignWidth="640" d:DesignHeight="480" >
<UserControl />

d:LayoutOverrides:

If a property is set to a control at runtime and if we want to change this property at design time we can use the LayoutOverrides.

EX:

<Border Height="250" Width="160" d:LayoutOverrides="Width, Height" >
</Border>

For more information see MSDN

Advertisements

Design time resources in Expression Blend 4

Design time resources in Expression Blend 4

Design time resources are very useful while we working with design on Expression Blend, resources will apply at run time, this will not be a problem for developers, but it is most important while we working on design tasks .  If your design surface can’t find the resources at design-time your design will not display properly as we see it on run-time, so that we cannot work on design, also we get  XAML parsing errors. Expression blend will show a message box which will looks in the following image.

(This message box will appear when we miss any resources that are linked with Style=”{}” property, and blend tries to find out the appropriate resource in the existing resource dictionaries in the solution if it is not found, then throws the above message box to fix the problem)

in this message box, Expression blend 4 provides us the available resource dictionaries, if we select one of resource from dropdown blend creates a new resource file called DesignTimeResources.xaml. and this will store in properties node of Silverlight project in the solution.Check the below image

Blend will now find the missing resources during design-time and be able to render the design surface properly.This feature enables editing of resources in dynamic styled applications to be much easier. If you open the DesignTimeResources.xaml, you will see the following xaml code.

if you remove the <resourceDictionary.MergedDictionares> tag you will get the same message box again.