İlkay İlknur
Just a developer...

Silverlight Focusing Issue

Friday, 18 February 2011 10:25 by ilkayilknur

As a developer or designer,making data entry simpler for users is our job. One of the most used technique in this process is giving focus to a control. Because, when a user navigates to a page, then user can start data entry without clicking to a control in the page. This technique is also available in Silverlight applications.

Let’s demonstrate focusing technique in a Silverlight Application.

Firstly, we are adding 2 textbox control in a stackpanel and then giving focus to second textbox control.

<StackPanel>
    <TextBox Width="208" x:Name="txtOne" Margin="0,10,0,0" />
    <TextBox Width="208" x:Name="txtTwo" Margin="0,10,0,0"/>
</StackPanel>

Code–Behind


public MainPage()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    txtTwo.Focus();
}

When we run our Silverlight application, we will see an unexpected behavior

 

If we click somewhere in the page, second textbox will take focus as expected. But why do we have to click ? It seems an unnecessary action.

The exact reason behind this behavior is so obvious. Because our Silverlight plugin does not have focus. We provide giving focus to plugin by clicking the page. After this process Silverlight could behave as expected. But how can we solve this problem ?

Actually, we have 2 different solution technique for his problem. First one is giving focus to Silverlight plugin using Javascript. The other one is doing the same operation using Silverlight

First Solution

We can give focus to Silverlight plugin using this javascript code like below.

var SlElement = document.getElementById('silverlightControlHost');
 if (SlElement)
     SlElement.focus();

Second Solution

In this solution, we will use HtmlPage type that is in System.Windows.Browser namespace. As you know, we can make some HTML DOM operations using this type. At this point, we will use static Focus method of HtmlPage’s Plugin property.

Final Code-Behind

public MainPage()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    System.Windows.Browser.HtmlPage.Plugin.Focus();
    txtTwo.Focus();
}

When we run this application, we can see expected behavior like below.

Hope this helps...

İlkay

Categories:   Silverlight | Silverlight
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Yorumlar RSSRSS Yorum Takibi
Share

Anonymous Methods - Behind The Scenes

Tuesday, 8 February 2011 22:35 by ilkayilknur

I think one of the most mysterious thing in software development world is compilers. Let's talk about C# compiler specifically.

C# compiler was written in C++ for historical reasons. C# compiler takes source files then generates IL code. But we don't know what's going on inside C# compiler when generating IL code. In this process C# compiler also creates different kind of new structures.

Anonymous methods provide us to define unnamed methods and most common usage of anonymous methods is delegates.

For Example,

btnLambda.Click += (_sender, _args) =>
{
   MessageBox.Show("This is an anonymous function");
};

Simply, this statement adds an event handler to the button's click event. But what's going on behind the scenes ? Does compiler make extra translations ? 

Firstly, we open this application with reflector.

We see that compiler has generated a different method and delegate named as <Window_Loaded>b__0 and CS$<>9__CachedAnonymousMethodDelegate1. This method includes anonymous method's inline statement.

If we put this event handler assignment into Loaded event of an WPF Window, we can see generated IL Code like below.

Simply, in this method first, compiler creates a delegate which takes generated static method as parameter and then adds this delegate as Button's click event handler.

Using Local Variables in Anonymous Methods

I think using local variables in anonymous methods is the most important feature of anonymous methods. If we add a delegate to an event as event handler explicitly, we can't use any local variables inside this method which assignment operation made in.

Let's demonstrate this scenario

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     int x = 10;
     double y = 198.30;
    btnLambda.Click += (_sender, _args) =>
     {
         MessageBox.Show(String.Format("This is an anonymous function 
             result={0}",(x*y)));
     };
}

In this method, anonymous method uses local variables which are defined in method that event handler assignment made in.

Now let's look at the new class view of the project with reflector again.

In this scenario, compiler generates a new type named <>c__DisplayClass1. This type includes 2 fields which we used in anonymous method declaration and also includes a method declaration that performs actions we specified in anonymous method declaration as we discussed in previous section.

Window_Loaded Method in IL Codes

In this method firstly, compiler generates new <>c__DisplayClass1 object then assigns field values. And finally, compiler creates a delegate by giving method which locates in <>c__DisplayClass1 type and adds this delegate to button's Click event as event handler. If we use a local object reference in an anonymous method, scenario will be the same. Compiler-generated type will include relevant fields in order to access used object.

As you see, compilers are really mysterious. When programming languages provide some new features, compiler deals with some extra translations and makes developer's life easier.

Maybe that's why I love compilers and programming languages. :) 

Hope this helps !

İlkay

Tags:  
Categories:   C#
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Yorumlar RSSRSS Yorum Takibi
Share

Hello World

Thursday, 3 February 2011 21:07 by ilkayilknur

"Hello World" like every developer says.:) Yes. I'm starting blogging in English. Firstly, I'm gonna review and rewritemy most read Turkish articles in English and then I'll write new articles in this blog. 

You can follow my English feeds from http://feeds.feedburner.com/ilkayilknur_en

And also you can follow me on Twitter : @ilkayilknur

You will find interesting .NET, Silverlight,WPF,C# articles in this blog ;)

İlkay

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Yorumlar RSSRSS Yorum Takibi
Share