Hyperlink or Linklabel in Silverlight

Silverlight 3 does not have a control similar to link label. This post tells you how you can add a hyperlink or linklabel in Silverlight 3.

Expression Blend:

1. Create a Silverlight project.

2. Add a Text Block object. Change the text to suit your requirement, let’s say My Website.

3. Under Properties->Brush tab, change the color to R=0, G=0, B=204.

4. Under Properties->Text tab, click on the Underline button. This will add underline to the text.

5. Under Properties->Common Properties, change the Cursor to Hand. (Choose from the drop down list)

6. Under Events tab, double click on MouseLeftButtonUp to add a event handler. In the code-behind, you can open the desired website.

Modifying XAML:

1. Create a Silverlight project

2. In MainPage.xaml, add the following lines

<TextBlock Text="My Website" TextWrapping="Wrap" TextDecorations="Underline" Foreground="#FF0000CC" Cursor="Hand" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp"/>

In the event handler, add the code to open your website.

Advertisements
Tagged , , , , , ,

Calling a Javascript function from Silverlight

This article talks about how you can call a javascript function from Silverlight.

1. Create a Silverlight project of type ‘Silverlight Application’ in Visual Studio. You will get a dialog box asking you whether you want to host your application on a website. Check this checkbox and click on OK.

2. Open MainPage.xaml and replace the existing content with this:

<UserControl x:Class="Blog1.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 <Grid x:Name="LayoutRoot">

 <Button x:Name="testButton" Content="Call js function" Click="testButton_Click"
 Width="150" Height="40" />

 </Grid>
</UserControl>

3. In MainPage.xaml.cs, a method testButton_Click should have been created. Replace the method with the one below. If the method is not present already, add it yourself.

 private void testButton_Click(object sender, RoutedEventArgs e)
 {
 HtmlPage.Window.CreateInstance("calledFromSilverlight", new string[] { "Silverlight User" });
 }

4. Save all the files and build your solution.

5. Go to the Bin folder where the xap file is located. Normally under <project>/Bin/Debug. There should be a file named TestPage.html. Open this file in your Visual Studio and add the following function.

 function calledFromSilverlight(user) {
 alert("Hello " + user);
 }

6. Open this page in a browser. You should see a button with text ‘Call js function’. Click on this and if everything is fine, you should see a pop-up window saying ‘Hello Silverlight User’.

That’s it. You just invoked a javascript function from Silverlight. Wasn’t that easy?

Note:

  1. If you rebuild the solution, the TestPage.html is created again, removing all the changes that you have made. You can make a copy of this file, name it MyTestPage.html and add the javascript function in this.
  2. This works for Silverlight 3 and Visual Studio 2008.

Next up: Calling Silverlight method from Javascript.

Firefox 3.5: Firefox exits when you close the tab

There is a serious problem with Firefox 3.5. When you have only one tab open in Firefox and when you close that tab, instead of opening a new, blank tab, Firefox itself exits. This is annoying because you would expect a new tab to open, but you see Firefox disappear in front of you and your heart sinks.

Turns out there is an easy fix for this.

1. Open Firefox and type about:config in the browser address bar. You will get a warning, acknowledge it and carry on.

2. In the filter, type browser.tabs, you will see some entries in the table. You will also see ‘browser.tabs.closeWindowWithLastTab’.  Its value should be true. Right click on this and click on Toggle. The value should change to false.

3. Close the browser and open again.

4. Verify that the browser doesn’t close when you close the only open tab.

Tada!

Tagged ,

Visual Studio: Restore Missing Templates

Symptom: You cannot add a new project or an item in Visual Stuio IDE.

Problem: Open the Event Viewer (Control Panel->Administrative Tools->Event Viewer) and see if the suggestion says “Run devenv.exe /installvstemplates”. If this is the case, then the problem is the template files are missing.

Solution:

1. Close all instances of Microsoft Visual Studio.

2. There are two folders under IDE folder (C:\Program Files\Microsoft Visual Studio xx\Common7\IDE), namely ProjectsTemplateCache and ItemsTemplateCache. Delete these two folders. Yes, I said, delete. If you are hesitant, don’t worry, it will go to the recycle bin from where you can retrieve them.

3. After deleting the folders, open the command prompt and run this command: Devenv /InstallVSTemplates .

4. Done. The template files should be restored and you should be able to add new projects and items.

I found myself in this situation out of nowhere. I was innocently coding and debugging and when I tried adding a control to my project, I suddenly see this error message. Thank God, the solution was this simple. Imagine spending another 4 hours on reinstalling Visual Studio. I wonder why I got into this situation, in the first place. Any idea?

Tagged , ,

Hello World

After a short hiatus, I am back again into this corporate world. So, it’s hello to the world all over again.

As I play with some new tools, expect some posts on WCF, WPF and Silverlight in the coming days. Watch this space.

 

Telecon with global team lead

We all know what conference rooms are. We also know that employees-conference rooms ratio is not really ideal in most of the organizations. People haggle over rooms for their meetings, telecons, presentations and what nots.

Thanks to a very strict manager in my previous organization, I know the importance of booking a conference room for any meeting and being present in the room well before the designated telecon time. Looks like no manager in my current organization has been able to teach these simple “rules” to the employees here.

We have meetings like innovation workshops, code reviews and tracking meetings and are less critical compared to telecon with the overseas team or “global team lead”. Still, we religiously book a conference room and park ourselves there well before time. 9 out of 10 times somebody would have occupied it. We have to “display” ourselves and let them know that we are waiting for the room. Apparently, they don’t get the message. They think we are snooping around to see if there is any room which we can hijack. After waiting for a few minutes, we knock and tell them in clear words that the room is ours because we have booked it.

What happens next depends on who is asking for the room. If it is a young person, they usually write off saying they booked the room and kick us out. If there is a guy looking like a lead, then there is a sarcastic “You booked the room? We have been using this room for telecon since 2 years” as if that gives them the authority to hijack the room whenever they want. If there is a manager around, a senior guy from the hijackers team will make a sincere yet authoritative request “We are having a telcon with the global team lead. I am already on the call. Can’t hang up now. Please find another room for yourself.” The word ‘please’ is hardly audible.

Now this is where my blood starts boiling. Nobody, not even the manager, not even the lead dare even ask “If you have a telecon with your team lead, why didn’t you book a room”. Why doesn’t anybody have the guts to say that? If I could have it my way, I would barge into the room and ask all of them to leave.

Somebody please come to my organization and put some sense into all the heads here. All the people need their brains to be washed with Mr. Brain Whitener and need to be taught about etiquettes.

If you are reading this and are guilty of hijacking a room, I urge you to stop doing that. Please.

The Last Lecture by Randy Pausch

Randy Pausch, a professor at Carnegie Mellon university has only a few days to live. He gave his last lecture a few days ago. This is the video of his now famous ‘The Last Lecture’. What do you think would be the last words of a dying man?

Youtube link

Google Video link

The complete lecture (1 hour 25 mins)

Hungry eyes

A darshini in J.P.Nagar. Friday Evening. Around 8.30.

It was a long, tiring day. No mood to cook and no patience to wait at restaurants waiting for food. “Dinner at a darshini?”, asks my husband. Sounds good.

We ordered dinner, as in we paid for it and collected it from the counter. A young man is relishing his noodles. A couple is busy stuffing the kid with idli. The road is almost deserted – 9 is late in Bangalore. An empty table at the far end, almost touching the road. That will do.

As I am gulping down my meal, my eyes lock with another pair of hungry eyes. I look away embarrassed. The other pair continues to stare alternately at me and my plate. A tug at my heart, a wrench in my stomach and my mind made up itself to face the situation.

I walk up to the owner of those hungry eyes.

“What’s your name?”

“Asif.”

“Do you know Kannada?”

He shakes his head.

“English?”

Shakes again.

“Which language?”

“Urdu.”

“Kya khaoge?”

No answer. I can see he is embarassed, ashamed.

“Idli khaoge?”

His eyes light up. He is almost inaudible. “Bhel Puri.”

“Idhar hi ruko.”

After five minutes, the darshini-wala calls out, “One plate bhel puri.”

I look around to spot the eyes. They are no where to be seen.

Darshini-wala says, “Is this for that boy?”

I nod.

He and his colleagues start searching for that boy. He is hiding behind a car. They call him. He shakes his head.

One guy shows the parceled bhel puri to the boy. “This is for you”, he says.

The boy is still not sure. Reluctantly he comes to collect it. He collects the parcel, and walks out, not even once looking at me.

Darshini-wala says, “He comes here everyday. Some four-five times. He will come back again after some time.”

I ask myself, “Did I do the right thing?”

Why do we support the weaker one?

Watching Australian Open last month was fun, to say the least. We saw a lot of upsets. Federer challenged by Tipsarevic in round 3, the defending champion Serena Williams thrown out in quarter finals, the World No.1 Federer and No. 2 Nadal failing to reach the finals – this was one nail biting tournament.

I watched the semi-finals between Djokovic and Federer and then the finals between Djokovic and Tsonga. Amazing matches, both of them. In the semi-finals, the crowd was cheering for Djokovic. There were many Federer fans present, no doubt, but Djokovic fans were more vocal in their support.

Cut to the finals match and the exact opposite happened. People were cheering for Tsonga. Djokovic fans were unusually quiet. Djokovic showed his disappointment about this in his acceptance speech.

The match was not about Serbia vs. Switzerland or Serbia vs. France. This was about strong vs. week. The crowd was not cheering for the country the player represented but the quality he represented. In both the matches, the crowd wanted the weaker player to win.

Why are we humans like this? Why do we want the weaker player to win? Is it because we want to boost his confidence or empower him? Even if either person wins, the crowd doesn’t really gain anything. What do we gain by supporting the weaker player? To feel good that we didn’t support the obvious winner but supported the less obvious one and hence did a good deed? Or is it just because we want to see an upset and hence a hot news to talk about? What is the intention behind this loyalty shift?

Why only the crowd, even I wished the same. My loyalty towards Djokovic suddenly shifted in the finals and I was hoping Tsonga wins. If I ask myself why, I am not happy with the answer I get. I want the weaker player to win because this will create a new sensation and breaking news and I have something to talk about. I want to discuss/gossip about how the champion was defeated and that gives me some wild pleasure. Strange!

What’s your reason? Why do you support the weaker player?

Being nice to rude people

During our routine post-lunch chit-chat, my friend made a gem of a statement – “It is easy being nice to nice people, but how can you be nice to rude people?”

I had been thinking about it for some days now and I was shocked when she made that statement. It was as if she was reading my mind. Many people would term it as a co-incidence, but I believe that incidents happen for a reason. This was no co-incidence. This was His way of sending a message across. But, I digress.

So, as soon as she said it, the words struck me. Few of my acquaintances are extremely sweet to me- always ready to help in any way they can, and I too reciprocate. I try to be nice to them as much as I can and all is well.

For the past few days, I am trying to be nice to people, and no where near being successful. I try to think before uttering a word so as not to offend anyone. But, whenever I encounter rude people, caution goes flying out of the window. Their act might be intentional or unintentional, but the point is they are rude. I find myself reciprocating in this case too. I use harsh words, sarcastic tone and I get a warm, fuzzy feeling for settling scores with them.

I know a few people who are nice to everybody. Even if someone is rude to them, they smile and act as if nothing happened – not a glimpse of anger, no teeth-grinding. I wonder how they can be nice to people who are mean to them?

Harsh words don’t upset them? Or they do get upset, but are capable of hiding it so well? Do they smile and bear with it only to bitch later and vent their anger? Do they feel superior to the other set and prove their superiority by forgiving? Do they feel sorry for the lesser mortals and hope they rise above the sins? Or is the secret word empathy here?

I will go on asking questions, but will never get a definitive answer. In effect, we come back to the statement we started with: “It is easy being nice to nice people, but how can you be nice to rude people?”

Your thoughts?