Wednesday, May 5, 2010

Silverlight 4, Javascript, and C# 4's dynamic keyword

The .NET 4 framework was released last month in April and along with it came C# 4.0. Among the many features added in Microsoft's new iteration of C#, the dynamic keyword was a major one. The keyword simplifies interop between dynamic languages and C#, which can come in handy when dealing with COM objects and others. Fortunately, this feature plays well with Silverlight 4 and, as a result, allows for easy communication between C# and JavaScript. Although it was already possible in previous versions of Silverlight, the dynamic keyword makes it much easier to communicate between the two languages.

First, if you haven't already done so, you will need the following using statements in your code in order to use the HtmlPage class.
using System.Windows.Browser;

Now, we can get started on calling JavaScript from C# using dynamic.  We need to access the underlying HTML page that contains our JavaScript code, so we will use the HtmlPage class to do so and assign it to a dynamic variable.
dynamic htmlWindow = HtmlPage.Window;

Now we can invoke JavaScript methods on the page using the htmlWindow variable and receive return values as well.  To do this, we simply call the JavaScript function as we would call a function that is in a class.
dynamic sum = htmlWindow.addWithJavaScript(10, 13);

The JavaScript function is as follows
function addWithJavaScript(var1, var2)
{
    var sum = var1 + var2;
    alert("JS sum: " + sum);
    return sum;
}

Together, the C# code is very concise and only takes two lines to communicate with JavaScript
dynamic htmlDocument = HtmlPage.Window;
dynamic sum = htmlDocument.addWithJavaScript(10, 13);
MessageBox.Show("C# return value: " + sum); // Just to show the results in C# 
For more information on the dynamic keyword in C# and the DLR
http://msdn.microsoft.com/en-us/library/dd264741.aspx (C# Dynamic)
http://msdn.microsoft.com/en-us/library/dd233052.aspx (DLR)

Wednesday, March 24, 2010

Creating a Checked ListBox in Silverlight with C#

I'm not sure if there is already a CheckedListBox control in Silverlight 4, but creating one manually is trivial enough that such a control may be unnecessary. Adding checkboxes to a ListBox is fairly easy in Silverlight 4 with a little bit of LINQ and some event handling.

In this example, we are going to be adding a list of names to a ListBox and figure out which names have a check next to them and which don't. We will also see how to set the IsChecked property of a CheckBox using code

string[] names = new[] {"Susan", "John", "Andrew", "Bob", "Raphael", "Sam"};

foreach (var name in names)
{
CheckBox checkBox = new CheckBox();
checkBox.Content = name;
checkBox.IsChecked = name.Contains('a'); // Place a check next to names containing an 'a'
//checkBox.Click += checkBox_Click;
ListBox1.Items.Add(checkBox);
}

It's seriously that simple. However, this probably isn't useful if you can't pull any data out of it. Finding which boxes are checked and which aren't is just as simple using LINQ.


string[] GetSelectedNames()
{
List selectedNames= new List();
var checkedCheckBoxes = from CheckBox checkBox in ListBox1.Items
where checkBox.IsChecked == true
select checkBox;
foreach (var name in checkedCheckBoxes)
{
selectedNames.Add(name.Content + Environment.NewLine);
}
return selectedNames.ToArray();
}


Here is a sample:
Get Microsoft Silverlight

Saturday, March 20, 2010

Dynamically Compiling F# in .NET

If you've ever needed to dynamically compile C# or VB.NET code in your program, you've likely seen and/or used the CodeDomProvider class. However, if you try to use this class to compile F# code, you will get a ConfigurationErrorsException with the following message: "There is no CodeDom provider defined for the language."

To compile F# code dynamically, you will need to use the FSharpCodeProvider class. Using it is hardly different from using the CodeDomProvider class

First, you need to add the following reference:
FSharp.Compiler.CodeDom.dll


Then add the following using statements:
using System.CodeDom.Compiler
using Microsoft.FSharp.Compiler.CodeDom;

The code is below.
public static void CompileFSharp(string sourceCode)
{
CompilerResults compilerResults;
CompilerParameters compilerParameters = new CompilerParameters();

compilerParameters.GenerateExecutable = true;
compilerParameters.OutputAssembly = "FSharpExecutable.exe";

var fsharpCodeProvider = new FSharpCodeProvider();
compilerResults = fsharpCodeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);

if(compilerResults.Errors.HasErrors)
{
// Handle compiler errors
}
}

If I have made any mistakes in the post, please do not hesitate to inform me about them.

Friday, March 19, 2010

An introduction of sorts

A lack of idea in addition to lacking the ability to write well outside of academia have both been factors in my avoidance of creating a blog. The main problem however was a mixture of a lack of direction and being too strict on myself. At times I was unsure of what exactly I wanted to write about and other times I felt as though I was obligated to write about a certain topic even when I didn't want to. This time around, I intend on being a bit more relaxed in my writing.

Now, an extremely brief personal introduction. I am an 18 year old software developer living in Columbia, South Carolina. I enjoy writing software and learning about new technologies related to the field in general (more on this later). I am currently a sophomore in college aiming for a Bachelor's in Computer Science. This is likely a hint as to what type of content this blog will be mostly about.

My purposes for creating this blog are not unlike the common reasons people tend to give: I want to document my experiences for future reference, teach what little I know and learn what I don't, improve my writing skills - or lack thereof - and discuss the things that I find interesting on the web and in the world.

I don't believe this blog will be much different from most of the other blogs in existence, but for those that actually take the time out to read it I hope you benefit from my thoughts in one way or another.