Saturday, 8 March 2014

Windows Phone - TextWrap in TextBlock

How to enable text wrapping in Windows Phone XAML TextBlock.
Its simple by setting the "TextWrapping" property to "Wrap".

<TextBlock Text="{Binding}" TextWrapping="Wrap"/>

Windows Phone - Set the width of Grid to 100%

I have a ListBox and within its ItemTemplate, I have a Grid, and the Grid has rows with 2 columns each.
My intention is to have the ListBox stretch to 100%. Also the grid inside each ListBoxItems to stretch to 100% of the screen size. The grid's 1st column is to occupy 80% and 2nd column 20% of the screensize.

All the above settings I want to have without explicitly setting the width in pixels (px), but have it by percentages so that according to different screen widths and orientations (Portrait/ Landscape), the controls re size themselves accordingly.

<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="Sample" />
<ListBox Name="MyListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0">
                        <TextBlock Text="Hello" />
                    </Grid>
                    <Grid Grid.Column="1">
                        <TextBlock Text="Hello" />
                    </Grid>
            </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
</StackPanel>

To make the design fluid based on percentages, I would change the xaml as below:
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="Sample" />
<ListBox Name="MyListBox">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="8*"/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <TextBlock Text="Hello" />
            </Grid>
            <Grid Grid.Column="1">
                <TextBlock Text="Hello" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
</StackPanel>

We have added a styling to ListBoxItem as below:
<Style TargetType="ListBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListBox.ItemContainerStyle>
This ensures that the ListBoxItem contents stretches to 100%.

Next changes are on the Grid Control inside the ListBox.
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="8*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
<Grid Grid.Column="0">
We want the first column to consume 80% of the width of the Grid and second column to occupy 20%.
Unlike HTML, XAML doesn't directly support percentages.
We specified 8* and 2*. How it works is as follows:
With the * is works as a ratio. The total width ratio (100%) is calculated on runtime as 10 (8+2).
Out of which 80% (8 out of 10) is first column and 20% (2 out of 10) is second column.
Having said that you would already have guessed that instead of 8* and 2*, 80* and 20* also works. This is correct as it works based on ratio.


Monday, 3 March 2014

ASP.NET - The type exists in both temp123.dll and temp456.dll

ASP.Net error: The type 'abc' exists in both "temp123.dll" and "temp456.dll

This is a common issue occuring in ASP.NET Web Application Projects. This is an age old issue in ASP.NET. At random, the pages gives error CS0433, saying the type exists in multiple DLLs.
These dlls are generated and located in "Temporary ASP.NET Files" directory. The error occurs most in pages using user controls.

A sample of this error is as below:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0433: The type 'myusercontrol_ascx' exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\sb2\4d76034e\bec2c8d0\App_Web_myusercontrol.ascx.abcd7d2.zyxwv5k.dll' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\sb2\4d76034e\zyxwv0\App_Web_abcde.dll'


Solution
In the "compilation" element of web.config file, add the attribute "batch" and set its value to "false"
<configuration ...>
  <system.web>
    <compilation ... batch="false"/>


This problem occurs because of the way in which ASP.NET 2.0 uses the application references and the folder structure of the application to compile the application. If the batch property of the element in the web.config file for the application is set to true, ASP.NET 2.0 compiles each folder in the application into a separate assembly.