Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » Silverlight » Interesting things to do with Silverlight 2

Interesting things to do with Silverlight 2

This article will show you how you can experiment with Silverlight.

Author Rank :
Page Views : 3129
Downloads : 32
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ThreadDemo.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

Apart from doing the usual "rich user interface" building tasks, you can also experiment with Silverlight 2 to achieve interesting results and outcomes. For example, you can create a Silverlight 2 application having a button with a filled rectangular content, like the one you see on media players.

Let's see how you can do this.

Procedure:

Create a Silverlight 2 application and add a Button control element in the Page.xaml.

Assume that the resultant code in Page.xaml looks like this:

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=
System.Windows.Controls"
 x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml
/presentation
"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Height="100" Width="100">
        </Button>
    </Grid>
</
UserControl>

Now add a Button.Content element between the <Button></Button> tags as shown below.

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=
System.Windows.Controls"
 x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml
/presentation
"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Height="100" Width="100">
            <Button.Content>
           </Button.Content>
        </Button>
    </Grid>
</
UserControl>

Now add a Rectangle element as shown below.

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Height="100" Width="100">
            <Button.Content>
                <Rectangle Fill="BlueViolet"  Height="20" Width="20"></Rectangle>
            </Button.Content>
        </Button>
    </Grid>
</
UserControl>

Save, build and execute the application. The output would be as shown in Figure 1.



Figure 1: Button with Rectangle content

Another interesting thing to do is make use of threading support to create a marquee or scrolling text. Let's see how you can do this.

Create a Silverlight 2 application and add the following code to Page.xaml.

UserControl x:Class="ThreadDemo.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="250">
    <Grid x:Name="LayoutRoot" Background="Lavender">
        <Canvas Name="MyCanvas" Margin="50" Background="PaleGreen" Width="300">
            <Button Click="Button_Click" Content="Start"></Button>
            <TextBlock x:Name="Marquee" Canvas.Top="50" Canvas.Left="1" >This is a scrolling text</TextBlock>
        </Canvas>
    </Grid>
</
UserControl>

The resulting user interface will be as shown in Figure 2.



Figure 2: Creating a Scrolling text application

In the .cs file of Page.xaml, that is, Page.xaml.cs, replace the default existing code with following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace ThreadDemo
{
    public partial class Page : UserControl
    {
        private Thread _thread1;
        private static TextBlock _marquee;
        private static Canvas _canvas;
        private static bool _done = true;    

        public Page()
        {
            InitializeComponent();
            _marquee = Marquee;
            _canvas = MyCanvas;
        }

        public bool Done 
        {    
            get { return _done; } 
            set { _done = value; }  
        }       
        private void StartScroll()
        {
           
            _done = false;     
            _thread1 = new Thread(DoThread1);     
            _thread1.Start();        
        }       
        public static void DoThread1()
        {          
            while (!_done)       
        {
                _marquee.Dispatcher.BeginInvoke(delegate()   
            {
                double x = double.Parse( _marquee.GetValue(Canvas.LeftProperty).ToString()) ;
                x++;
                double canvas = double.Parse(_canvas.Width.ToString()); 
                _marquee.SetValue(Canvas.LeftProperty, x);
                if (x >= 180)
                {
                    _done = true;
                }
            });      
                Thread.Sleep(100);
                if (_done)
                {
                    break;
                }
        }     
        }    

        private void Button_Click(object sender, RoutedEventArgs e)
        {
         
            if (false == _done)          
            _done = true;        
        else           
            StartScroll(); 
        }  
    }
}


Here, you have created a Thread object and used an anonymous method inside the Thread worker method to perform the scrolling action. The variable x indicates the begin point of the scrolling of the text. It is incremented in the loop. The value at which x should stop incrementing has been hard-coded here as 180, if you wish, you can replace that with a programmatically computed value. When you save, build and run the application, and then click Start, you will see the text scrolling.

Conclusion: This article showed you how you can experiment with Silverlight.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mamta M
Mamta M has over 8 years of experience in the IT industry and loves to learn and experiment with new technology trends. She is passionate about in C#, WPF, ASP.NET, Silverlight and other .NET related technologies. She believes knowledge grows through sharing and loves being active in the .NET community.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.