Eclipsed4utoo's Blog
Not Your Ordinary Programmer

Silverlight 4 – Creating a Custom Modal Dialog

     Posted on Fri ,23/07/2010 by Ryan Alford

For a Silverlight application I did recently, I wanted to do a custom modal dialog box for showing informational messages, error messages, or other messages to the user.  This dialog box would have no title bar or “X” button, and would have rounded corners.

For this tutorial, we will use Blend 4 to accomplish our task.

First, create a new Silverlight 4 application.  I named mine CustomDialog.

Image1

Once the project has been created, we are going to add a new item to our Silverlight project.  This item will be a ChildWindow.  I named mine DialogPopup.

Image2

The ChildWindow will handle the modal dialog part of our window.  However, there are parts of the ChildWindow that I didn’t like and wanted to remove/change, such as the window chrome.  I also wanted to round the corners of the window.   To do this, we need to edit the template for the window.  In the Objects and Timeline window, right-click on the ChildWindow parent –> Edit Template –> Edit a Copy

Image3

We are going to keep it simple and save the new style to the ChildWindow document.

Image4

So first, we are just going to delete the window chrome from the window.  The window chrome is the title bar and the “X” used to close the window.  Drill-down in the treeview until you find the Chrome entry.  Right-click, and choose Delete.

Image5

Next, we need to delete some of the Borders.  These give the window a grey border that I didn’t want.

Image11

Next, we are going to round the corners of the window.  Drill-down and click on the Border shown below.

Image12

In the Properties window, set the CornerRadius to 20.

Image7

Move down to the next Border,

Image13

And set it’s CornerRadius to 20.

Image9

With the same Border selected, in the Properties window, set the Background Brush to a color of your choice.

Image10

The window “jazz” is now complete.  Next, we are going to move to the XAML.

First, we need to give our window a name so we can handle binding.  I named mine myCustomPopup.

<sdk:ChildWindow
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
   x:Class="CustomDialog.DialogPopup"
   x:Name="myCustomPopup"
   Title="DialogPopup"
   Width="400" Height="300">

Now we need to add a TextBlock to the window to hold our message.

<TextBlock
    Text="{Binding Message, ElementName=myCustomPopup}"
    Margin="51,88,57,113"
    TextWrapping="Wrap"
    Foreground="#FF1D1515"
    FontWeight="Bold"
    TextAlignment="Center"/>

You may notice that we are binding to a property called Message.  This is a custom DependencyProperty that will correspond to a public property that we will set when we create an instance of the window.

/// <summary>
/// Creates a public property for the TextBlock to bind to
/// </summary>
public static DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(DialogPopup), new PropertyMetadata(""));
public string Message
{
     get { return (string)GetValue(MessageProperty); }
     set { SetValue(MessageProperty, value); }
}

This will complete our work for the window.  To show off the new window, lets add a Button to the MainPage.xaml, create an EventHandler for the Click event.

<Grid
   x:Name="LayoutRoot"
   Background="White">

     <Button
        x:Name="btnShowMessage"
        Content="Click Me"
        Click="btnShowMessage_Click"
        Margin="251,207,289,236"  />
</Grid>

And the code for the Click event…

private void btnShowMessage_Click(object sender, System.Windows.RoutedEventArgs e)
{
     DialogPopup dialog = new DialogPopup();
     dialog.Message = "Our message";
     dialog.Show();
}

And that will complete our tutorial.  Run the application from Blend using F5, and you should see this..

Image14

Silverlight 4 – Publishing Application to Remote Server

     Posted on Thu ,22/07/2010 by Ryan Alford

This blog post will talk about the steps needed to publish a Silverlight 4 applications that uses WCF RIA Services for communication.  There were a number of issues that I ran into trying to publish, so I decided to do a blog post to keep other’s from having to search and hunt for the fixes.

NOTE:  These steps are for IIS running on Windows Server 2003 SP2.  Steps may be different for other versions of IIS.

1.  First, you will need to fix your web.config file as I have noted in this blog post.  This is not added by default.

2. (Optional) If you are running a .Net 2.0 or .Net 3.5 website on this same server, you will need to create a new AppPool in IIS.  This is required because an AppPool can only run one version of the .Net CLR.  With .Net 4, Microsoft created a new CLR.  Therefore, you will need an AppPool for the .Net 2.0 and .Net 3.5 websites, and an AppPool for the .Net 4 websites.  NOTE:  This may have changed in later versions of IIS.

3.  Next, in Visual Studio 2010, you will need to set a number of the references of the .Web project to Copy to Local.  I had to do it for all of the System.ServiceModel references.  You may not have all of these references, and that is fine.

SLImage4

Then go to the Properties window, and set Copy Local equal to True.

SLImage5

4.  Next, you need to add a new MIME type for both the .XAP extension and the .XAML extension.

Open the IIS Management console.  Drill-down until you get to your domain/default website.  Right-click and click Properties.
SLImage1

Next, go to the HTTP Headers tab, then click on the Mime Types… button.
SLImage2

Now add the two new mime types..
SLImage3

Now simply publish your web project to your server, and you should be ready to go.

NOTE: This blog post does not talk about creating a Virtual Directory or making the Virtual Directory an application.  There are plenty of other blog posts that can walk you through that.  However, make sure the Virtual Directory is set to the AppPool for .Net 4(if it was required for your setup).

WCF RIA Services – Error When Moving Service To Server

     Posted on Thu ,01/07/2010 by Ryan Alford

I recently tried publishing a WCF RIA Service to my server, and came across an error.  I was using all the latest Windows Phone 7 and RIA Services Tools, and targeting the .Net 4 framework.  However, since I have both .Net 3.5 and .Net 4 installed on my server, I ran into this error…

WCFError1

“System.Configuration.ConfigurationErrorsException: The value for the ‘compilerVersion’ attribute in the provider options must be ‘v4.0′ or later if you are compiling for version 4.0 or later of the .NET Framework. To compile this Web application for version 3.5 or earlier of the .NET Framework, remove the ‘targetFramework’ attribute from the <compilation> element of the Web.config file.”

It threw me for a loop because it worked fine in Visual Studio 2010 when testing on my development PC, but when moving it to the server, it had this problem.

The fix was fairly easy, once I was able to find it.  Basically, you need to add the a section to the web.config that doesn’t get added when adding the RIA Services DomainService to the project.

<system.codedom>
  <compilers>
     <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
       <providerOption name="CompilerVersion" value="v4.0" />
       <providerOption name="WarnAsError" value="false" />
     </compiler>
  </compilers>
</system.codedom>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes