Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
With most browser plug-ins, you are not allowed to communicate with third-party domains. That means that an app loaded from the lynda.com web site does not automatically get to make web requests for content running on the Microsoft.com site. The owner of the foreign server can permit these kinds of cross-domain calls if they set up a cross-domain policy file. This is usually a file called crossdomain.xml. In real world terms, these restrictions mean that your Silverlight application cannot access resources such as videos, images, or other HTML content unless given permission.
But here is the interesting bit. If you are running an elevated trust out-of-browser application, these restrictions no longer apply. I am going to show you how to set up a cross-domain call using Visual Studio. I have opened up our project called MakeCrossDomainCalls. This is my main UI. It consists of a button, a textbox for me to type in an Xbox LIVE alias, and an image control on the bottom of the page that I will load with an avatar from the Xbox LIVE web site.
Let me show you what kind of images I am talking about. I am going to switch over to a browser window and I am going to go to avatar.xboxlive.com. This section of the URL contains the alias. So if I type my alias in here, waltritscher, and hit Enter, you will see my Xbox avatar show up. This is what I want to call from my Silverlight application. So let me switch back to Visual Studio and go to my code behind page. I am going to press F7.
Here on line 37 is the URL from the Xbox LIVE site. Notice that it has a curly brace, 0, curly brace in the URL. I am going to replace that at runtime with the item from the textbox. So that's what's happening on this line of code right here. I get the gamerTag, and then I inject the gamerTag into this location, in the string. Then I set up a webRequest call. I am going to do that here on line 39, and then I am actually gong to invoke the web request in an asynchronous fashion on line 40.
I am going to call a method called BeginGetResponse, which grabs a thread out of the thread pool and sends it off to do the work. When the thread is done, it's going to return on this GetResponseCallback function down here, and inside here, I am going to marshal back to the UI thread, and then I am going to de-serialize the content coming back from Microsoft's servers. That's what's happening on these two lines of code. Then I create a brand-new image called BitmapImage, and then I set the source to that to the de-serialized bits I got from Microsoft's site, by calling GetResponseStream here.
Then I store that image into the image on my page. And then finally, I have a storyboard set up in my XAML that I am going to trigger here on line 60 to animate the image around, make it dance a little bit. Let's see what it looks like. I am going to press F5, and then I am going to click the button and it's going to pass my waltritscher string in and return my avatar. Now, we have an editor here at lynda.com that has graciously granted permission to use their avatar. It's thatgi42, and there you see their avatar.
I am sure you will have fun putting your own avatar in the textbox, or just take a wild guess. Here is one, some random person's avatar. I mentioned earlier that you have to be in full trust to make this happen. Let's see what happens if you're not in a full trust application. I am going to go to Properties, click the Silverlight tab, move down to the Enable running application out of the browser, and unselect that checkbox. Then rerun the application, and when I click on the button, it's going to throw an exception. There it is! Security error. Visual Studio has captured that, taken me over to my code, showing me what's wrong.
I want to fix this so I am going to make a slight modification to my code so that this doesn't happen at runtime. I will shut the application down and then I am going to uncomment this line of code right here, and this one down here. This checks with the application to see if I am currently in an elevated permission mode, and if so, then go ahead and run this code. I apparently put a break point in there without me seeing it, so I am going to press F9 to turn off the break point and then F5 to continue running the application.
What this demo shows is that I am not in an out-of-browser application, but when I click on the button, I don't get an exception happening. So as you see, cross-domain calls are much easier in Silverlight 4 now that we have elevated trust applications.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104465 Viewers
56 Video lessons · 116380 Viewers
71 Video lessons · 85608 Viewers
131 Video lessons · 40973 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.