syrian developers community
silver light
How to display custom Rightclick context menu in Silverlight controls?
Dec 23rd
When you right click on any Silverlight controls in a web page, it display the default Silverlight context menu which with a single option view the Silverlight Configuration. You may intercept this context menu and display your own menu.
The first step you have to do is, go to the aspx page where you are hosting the Silverlight control and set the property “Windowless” to “True”
<asp:Silverlight ID="Xaml1"
runat="server"
Windowless="true"
Source="~/ClientBin/ContextMenu.xap"
MinimumVersion="2.0.30923.0"
Width="100%" Height="100%" />
Now, you can use the “AttachEvent” property of the browsers Document object to attach your own event handler to the “oncontextmenu” event from your xaml pages.
Sample code:
System.Windows.Browser.HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu); More > How to play audio or sound files from Silverlight
Dec 23rd
Silverlight provides a class called MediaElement which can be used to play audio or video files.
Silverlight MediaElement supports playing video/audio files in MP3 and WMV formats. The current version of Silverlight does not support .WAV files and .AVI files.
If you attempt to use .WAV or .AVI files with the MediaElement control, you will get the following error: Error: Unhandled Error in Silverlight 2 Application <Application Name>Code: 3001 Category: MediaError Message: AG_E_INVALID_FILE_FORMAT
In order to play an .MP3 or .WMV file, you must first include the file in your Silverlight project and then set it as an Embedded Resource.
In order to More >
Display Tooltip for Silverlight controls
Dec 23rd
The below sample code shows how to display a simple text tooltip for a Button control:
<Button Width="60" Height="20" Content="My Button">
<ToolTipService.ToolTip>
<ToolTip Content="This is tool tip for Button"></ToolTip>
</ToolTipService.ToolTip>
</Button>
See this example which sets a tooltip for a textbox control:
<TextBlock Width="60" Height="20" Text="My Text">
<ToolTipService.ToolTip>
<ToolTip Content="This is tool tip for TextBlock"></ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
Display rich UI elements in tooltip
Silverlights ability to support UI More >
How to show a popup layer within a Silverlight web page?
Dec 23rd
Add a button to your xaml page as shown below:
<Grid x:Name="LayoutRoot" Background="White" > <Button Width="100" Height="50" x:Name="showPopup" Click="showPopup_Click" Content="Show Popup" /> </Grid>
Add the following code to your code behind file (page.xaml.cs)
Popup p = new Popup();
private void showPopup_Click(object sender, RoutedEventArgs e)
{
// Create a panel control to host other controls
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.Gray);
// Create a button
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(5.0);
button1.Click += new RoutedEventHandler(button1_Click);
// Create a text label
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0); More > How to call Javascript functions from Silverlight code ?
Dec 23rd
One of the advantages of Silverlight is, it has access to the Html document of the web page in which it is hosted. This enables Silverlight to access HTML elements in the page and also call Javascript methods.
The following steps shows how to call a Javascript method from Silverlight.
Open the ASP.NET or HTML page which hosts the Silverlight control. Add the Javascript method as shown below:
<script language="javascript"≫
function SayHello()
{
alert("Hello from JavaScript, invoked by Silverlight");
}
</script>
Now, open your XAML control file and add a button control as shown below:
<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="btnSayHello" Content="Say Hello"
Click="btnSayHello_Click"></Button>
</Grid> More > Create WCF service to retrieve session data – part II
Dec 23rd
Create a new Silverlight project with a web project to host the Silverlight control.
Steps:
1. Open Visual Studio and select the menu “File” > “New” > “Project”
2. Select the Project Type as “Silverlight” under your favorite language and choose the template “Silverlight Application”. I have selected Visual C# as the tutorials here. I have named my project as “AccessSessionFromSilverlight” and have selected the option “Create directory for solution” so that all my project files are organized within a folder structure.
3. In the next screen, choose the option “Add a new web to the solution for hosting the control”.
4. After Visual Studio More >
Access Session variables from Silverlight controls
Dec 23rd
Silverlight controls are client side controls. They get executed on the browser on the client machines and they do not have direct access to the server side data.
Since Session variables live on the web server, Silverlight controls cannot access them directly. However, there are several ways Silverlight can retrieve the server side data including Session information.
Let us explore some of the possible options to make Session data available to Silverlight controls.
Option 1: Push session data to the client in the form of VIEWSTATE data or data for UI controls. The dis advantage with this approach are no type safety, only More >
Calling WCF from Silverlight controls – Part II
Dec 23rd
This chapter is the continuation of the previous chapter. If you havent read the , please read it before you continue here.
Let us add a new method to our WCF service class, decorated with the [OperationContract] attribute. Add a method as shown below:
[OperationContract]
public string GetName()
{
return "John";
}
As you can see, it is a simple method which returns a hard coded name.
Open your solution explorer. Right click on the file “Service1.svc” and select “Set as Startup Page”.
Now run the application by pressing Ctrl + F5 (or, using the menu “Debug” > “Start Without Debugging”)
The browser will be More >
How to call WCF methods from Silverlight controls ?
Dec 23rd
Silverlight controls get executed on the client browser. It does not have direct access to the data on the serverside. So, if your Silverlight controls need to retrieve data from database or other data sources on the server, we have to use various approaches like WCF calls or depend on the InitParameters property of the Silverlight controls.
The recommended approach to get server side data to Silverlight controls is using WCF method calls. The most important benefit is the type safety.
The following sample demonstrates how to use WCF to retrieve data from Server side to Silverlight controls on the client side.
In More >
How to pass parameters to Silverlight controls from ASP.NET pages ?
Dec 23rd
You can pass parameters from your aspx pages and html pages to the Silverlight controls. This chapter explains how to pass parameters to Silverlight controls from your aspx page and code behind files.
InitParameters
The Xaml page user control has a property called InitParameters. You can set a value in the form of key-value pairs from your ASPX pages. Since this property accepts key-value pairs, you can pass any set of string values.
How to set InitParameters
Example – Set InitParameters property from ASPX page:
<asp:Silverlight
ID="Xaml1"
runat="server"
Source="~/ClientBin/MySilverlightApp.xap"
InitParameters="City=Houston,State=Texas,Country=USA"
Width="300"
Height="300" />
You can set this property from the code behind file of your ASPX More >