My Experiences in Silverlight 4 : Writing re-usable Controls
These days I am working on Silverlight and creating Custom controls which would be re-usable in another projects. And when you set out to write re-usable Silverlight controls, Silverlight toolkit is the best code to look up to.
When you download the Silverlight toolkit. The toolkit installer also copies source code of the toolkit at “%Program Files%\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source” folder. When you start exploring the source code, you will notice a peculiar way the source code for toolkit is arranged. Every control in toolkit is written as a content control and has a resource dictionary that has a default style.
So, inspired by this style of writing custom controls, I set out. Now the reason why you want write a custom control is you require certain custom fields and a custom style and behaviors. So when you define these controls and the style, you also want your custom properties to appear in your style(Or else why would you define those additional controls. Typically your style for your control would look something like this.
and the your custom control would look something like this.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:layoutPrimitivesToolkit="clr-namespace:System.Windows.Controls.Primitives"
xmlns:layoutToolkit="clr-namespace:System.Windows.Controls">
<Style TargetType="my:CustomControl1">
<Setter Property="Foreground" Value="#FF000000" />
<Setter Property="BorderBrush" Value="#FFEAEAEA"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="my:CustomControl1">
<Grid x:Name="Root">
<vsm:VisualStateManager.VisualStateGroups/>
<Border
x:Name="Border"
BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<TextBlock Text={TemplateBinding MyCustomProperty1}
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
And your .cs file would look something like this. Notice that there is a peculiar way I chose to name the Dependency Property. You might want to follow this pattern (i.e. your DependencyProperty should be named your Variabale Name suffixed with “Property”.
You must adhere to this Naming convention if you intend to use Blend in your Project. (If your Dependency Property is named otherwise, You might not be able to Use Blend to set these template Bindings.)
public class MyCustomControl1 : ContentControl
{
public string MyCustomProperty1
{
get { return (ExpandDirection)GetValue(MyCustomProperty1Property); }
protected internal set
{ SetValue(MyCustomProperty1Property, value); }
}
public static readonly DependencyProperty MyCustomProperty1Property=
DependencyProperty.Register(
"MyCustomProperty1",
typeof(string),
typeof(MyCustomControl1),
new PropertyMetadata(ExpandDirection.Down, OnExpandDirectionPropertyChanged));
...
Now my control should show the value I set in MyCustomProperty1 on the UI right?. Something like this:
<Grid x:Name="LayourRoot"> <my:CustomControl1 MyCustomProperty1="Hello Content Control"/> </Grid>
But this does-not seem to happen. After searching for sometime on Silverlight .Net forums, I could not exactly find out what I was doing wrong here. After some Binging on Bing and Binging on Google
. I found that for custom properties, TemplateBinding doesnot seem to work the way I expect it to work.
So instead as per the suggestions on the Silverlight .Net forums, I modified the template of the control by replacing TemplateBinding. Now My TextBlock Markup looks something like this.
<TextBlock Text={Binding RelativeSource={RelativeSource TemplatedParent},Path=MyCustomProperty1} />
Now My Template binding works as I expect it to work. Essential what has changed in the previous representation and the new representation is still a matter of research for me. Will Keep posting more about this as and when I discover more about it.
If I have made any mistakes, feel free to write Comments about what I am mistaken about. I'll Happily Correct it
Using Open XML SDK v2.0 on Windows Azure
Hello folks,
Its been really long that I have written a post. And I have no excuses, Just couldn’t find the right time and the right topic to blog on
.
Generating Word/Excel reports is a fairly common requirement. Now that we intend to migrate our application to cloud, we realize that on Windows Azure, You do-not have Office DLLs or infact, any other (unnecessary from Azure’s perspective) DLLs available to you. You can always package Office DLLs with your app deployment package and use them in you application on Windows Azure (You can find articles on the blogosphere about how this can be done).
I opted for using Open XML SDK v2.0. Using Open XML SDK for .NET available here . I Can generated Word / Excel reports on the fly.
Here’s a sample code to write to create a Word Document
//Using Statements Required
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
//Code to Create a Word Document at the provided File Path
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
mainPart.Document = new Document(
new Body(
new Paragraph(
new Run(
new Text("Report Generated by Open XML SDK ")))));
wordDocument.MainDocumentPart.Document.Save();
wordDocument.Close();
}
ok. Now that we have everything in place, We’d want to deploy this “Report Generation Solution” to the cloud. But First , run it locally on the compute Emulator.
Everything seems to be working, Next step is to deploy your application to your azure account.
This blog post talks about a known issue on using Open XML SDK in .NET 4 Roles on Windows Azure.
When you right click your solution to do a publish
Windows Azure would ask you for the Hosted service where you want to deploy the solution, If you have not already set that up, the dialog box also has a provision to do that for you.
Please Remember to UnCheck, (you read that right) “UNCHECK” Enable Intellitrace for .NET 4 roles.
You may want to argue that Intellitrace helps us in historical debugging in an event of a fatal crash, but for now you will have to live with the Windows Azure Diagnostics Logging for now.
The reason, why this needs to be disable is, “Enabling Intellitrace for .NET 4 roles when using the OPEN XML SDK seems to freeze your web/worker role.”
I Learnt this the hard way (after being billed for a week for a (Frozen, just because I Enabled Intellitrace and am using Open XML SDK in my azure app) Extra Large VM ). In other words, that’s a lot of money. ![]()
![]()
Keep reading this space for such posts to come. I am working on Windows Azure now, So I am sure there are many such topics on which I can post.
. Thank You.