Archive for category Silverlight

Finding Silverlight configuration files

I was working with Silverlight application that had some a couple of configuration files. When starting up my application, I got a relatively helpful error message that pointed to the missing configuration file, which was http://localhost:2784/Config/cool_config.xml

I’m always pleasantly surprised when I get a helpful error message when developing.

However, there was no such directory. So where was my missing file?

If you pull up the ASP.NET Development Server window that starts when you are debugging an web-based application, you will see a window similar to this:

Development Server

Development Server

 

The “Physical Path” field contains the location of the application. From that base directory, I was able to find where my configuration file should have been and put it there.

Leave a comment

Adding a glow in Silverlight with the DropShadowEffects class

I wanted to have a mouse-over effect for my Path – I mean, who doesn’t? Ladies love guys that can dynamically add Silverlight glow effects as much as they like guys that can use the ternary operator. So I’m going to tell you how to improve your love life.

First, the XAML. Notice the use of MouseEnter and MouseLeave events on my Path object:

    <Grid x:Name=”LayoutRoot” Background=”White”>
        <Path x:Name=”ScottPath” Stroke=”Black” Data=”M 20,100 C 10,300 300,-200 300,100″ MouseEnter=”Path_MouseEnter” MouseLeave=”Path_MouseLeave” />
    </Grid>

Oh yeah, that’s right – I’m using a Bezier curve. This keeps getting better.

Now into the code behind we go. Before you enter the wonderful world of DropShadowEffects, you’ll need to add a reference to the right library:

using System.Windows.Media.Effects;

And away we go:

        private void Path_MouseEnter(object sender, MouseEventArgs e)
        {
            DropShadowEffect dropShadow = new DropShadowEffect();
            dropShadow.BlurRadius = 10;
            dropShadow.Color = Colors.Purple;
            dropShadow.Direction = 0;
            dropShadow.Opacity = 1.0;
            dropShadow.ShadowDepth = 0;

            ((Path)sender).Effect = dropShadow;
        }

        private void Path_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Path)sender).Effect = null;
        }

What I have going on is that when the mouse hovers over the Path object, I create an instance of the magical DropShadowEffect class, set all the variables that make me feel happy and wonderful, and add the DropShadowEffect  to the Effect property of the Path object. And when the mouse leaves the path, I set the Path.Effect to null to get rid of the effect.

No worries, no fuss, just an awesome purple glow on your line.

Leave a comment

Unable to cast object of type ObservableCollection

I break out some Silverlight coding from time to time, to keep the rust off. And most of the time it goes OK – you get the occasional error that come out of nowhere. Like this dreaded one:

Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection' exception

Where did that come from? I had bound a collection of MyGroup items to a ListBox. I later wanted to retrieve the items that were selected by using the SelectedItems collection:

ObservableCollection<MMLService.MyGroup> MyItem = 
(ObservableCollection<MMLService.MyGroup>)SecurityGroupGroups.SelectedItems;

When I tried to cast the ListBox.SelectedItem collections to the type of object I had bound to it, I received this error:

Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]' to type 
'System.Collections.ObjectModel.ObservableCollection`1[MML2010.MMService.MyGroup]

My attempts to cast the collection failed. But, I managed to skirt the error by using an Enumerator:

List<Int64> Groups = new List<Int64>();
IEnumerator<object> MyEnum = (IEnumerator<object>)SecurityGroupGroups.SelectedItems.GetEnumerator();
while (MyEnum.MoveNext())
{
    MMLService.MyGroup Temp = (MMLService.MyGroup)MyEnum.Current;
        Groups.Add(Temp.ID);
}

That can’t possibly be the best way to get around that exception. I know this with all my heart. That said, it worked, and I wanted to get around the issue and onto the rest of the code. And for the curious, the error occurred in Silverlight 4.

Leave a comment

FYI – refreshing a Silverlight XAP file

Something that will save you some hair and time, I know that I have very little of the former left. No, really – most of my hair left me in my twenties.

Anways, I was experimenting with a Silverlight application on my  hosting provider, and I would make a change, upload the XAP, and refresh the browser, but nothing would happen. I would close the browser tab and reopen it, and the change I just made wouldn’t appear.

That was because I needed to open a whole new browser, not a new tab. After I opened the new browser window, then my changes were displayed. I would have liked to have remembered that earlier in the morning and saved myself an hour, but this time the lesson has really sunk into my brain.

Leave a comment

Communicating between Silverlight and ASP.NET via JavaScript

You love Silverlight, you love ASP.NET. You want Silverlight and ASP.NET to love each other. Enter the third party in the technology love triangle, JavaScript.

In my scenario, I had an ASP.NET page that opened another page that hosted a Silverlight control. When the user selected some data and clicked the Save button, the Silverlight window should pass that data to the original ASP.NET page, as well as close the opened window.

So in the opening page, I use some JavaScript to open a new window:

window.open('SilverlightPage.aspx', 'MyWindow');

After I’ve done wonderful things in Silverlight, I click my Save button:

<Button x:Name="SaveProperty" Content="Save" Click="SaveProperty_Click"  />

Which calls it’s code-behind function. Here is the magic, the HtmlPage.Window.Invoke function. In my code, I use it to call a JavaScript function defined in SilverlightPage.aspx, passing in the values from two TextBox controls in Silverlight.

 private void SaveProperty_Click(object sender, RoutedEventArgs e)
{
 HtmlPage.Window.Invoke("PopulateOneTwo ", TextBox1.Text, TextBox2.Text);
}

All the PopulateOneTwo function does is pass on the variables from Silverlight to the calling page, then close itself. Remember, PopulateOneTwo  is defined in SilverlightPage.aspx, the page that holds the Silverlight control.

function PopulateOneTwo(varOne, varTwo) {
window.opener.PopulateValues(varOne, varTwo);
     window.close();
}

Back to the original ASP.NET page, I have the PopulateValues function to actually take the values passed from the SilverlightPage.aspx (and the Silverlight code) and populate the TextBox controls:

function PopulateValues(varFirst,varSecond) 
{
     var One= document.getElementById("txtOne");
     var Two= document.getElementById("txtTwo");
     One.value = varFirst; 
     Two.value = varSecond;
 }

<asp:TextBox ID="txtOne" runat="server"/> 
<asp:TextBox ID="txtTwo" runat="server"/>

Bada bing, done, and I have the values passed into an ASP.NET from Silverlight.

Leave a comment

Silverlight – Error when changing the application library cache setting

I’m working on a Silverlight project whose XAP file was over 1.4 MB, which was larger than I wanted. One of the websites I saw suggested changing the value of the property “Reduce XAP file size by using application library caching”. This sounded like a good plan. BTW, the property can be viewed by selecting your Silverlight project and selecting Project, Properties…

But like even the best of plans, there was a problem. When I built the solution, I got this error:

"The file cannot be checked out for editing because this would cause it to be reloaded, 
and reloads are not currently allowed for this file."

The in my case was ScottsRockinSweetProject.csproj. Or something to that effect.

The solution was pretty easy. I right clicked on my web project and selected Check Out for Edit. Then I selected the ScottsRockinSweetProject.csproj file and checked it out. After I rebuilt the solution, all was well.

And, my XAP file was under 700 KB, so the property definitely helped.

Leave a comment

Cross-domain policy files, web services, and Silverlight

I was deploying a Silverlight site recently, and I’ve learned a lot about it, including how unprepared I was the first time to do it successfully. Live and learn.

One of the issue I ran across was accessing a web service from the Silverlight file. When I deployed my service to the remote system (they don’t work so well when trying to connect from the Internet to the localhost), I got this message:

“An error occurred while trying to make a request to URI ‘http://www.scottscoolsite.com/Service.svc&#8217;. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.”

Bang, bang, bang goes my forehead…

So I was off to find out what the heck they were talking about. I found at article on MSDN title Network Security Access Restrictions in Silverlight that seemed to be what I needed. I then typed in this XML code in the policy file, but it didn’t work.

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="http://www.scottscoolsite.com" />
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

Then I tried this policy file, and it worked. Can you spot the difference?

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="http://scottscoolsite.com" />
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

It’s this line:

 <domain uri="http://scottscoolsite.com" />

In the first policy file, it started with www. In the one that works, I excluded it. Tricky.

1 Comment

Silverlight tutorial series

In the vein of the Twelve Days of Christmas, Jeff Blakenburg has the 31 day of Silverlight. I’ve had some experience with Silverlight, but I’m going through all 31 days, because you never know when you can pick up something useful. The tutorials are well-written and easy to follow.

1 Comment

Global application variables in Silveright

I’ll take the most simple path for creating global variables in Silverlight. In the Appl.xaml.cs file, declare a public variable:

         public string MyValue = "The Lions won a game!";


To get the value of this variable from another XAML code behind file:

         string SomeValue = ((App)Application.Current).MyValue;

Leave a comment

The Silverlight value converter

Dan Wahlin has a nice article on implementing the IValueConverter interface on his blog. I’ve used value converters in the past and not fully known the intricacies of them, so this was a clear tutorial and explanation of what they are for and how to use them.

Leave a comment