Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Archive for July 30th, 2008

Switch SilverLight UserControl

Posted by ken zheng on July 30, 2008

Silverlight does not have the Page type, the term is currently used is RootVisual UserControl. RootVisual object is analogous to the root window in WPF and can only be set once for the lifetime of the app, and is effective once the Application’s Startup event is raised. But this not true, you can set RootVisual as many times you want. So Application_Startup can be used to call multiple Silverlight controls.

    <form id="form1" runat="server" style="height:100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div  style="height:100%;">
            <asp:Silverlight ID="Xaml1" runat="server" InitParameters="ControlId=Page" Source="~/ClientBin/SilverlightApplication1.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
        </div>
    </form>
		<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
			<param name="source" value="ClientBin/SilverlightApplication1.xap"/>
			<param name="onerror" value="onSilverlightError" />
			<param name="background" value="white" />
			<param name="initParams" value="ControlId=BookStore" />

			<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
     			<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
			</a>
		</object>

and in the App.xaml.cs

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.InitParams.ContainsKey("ControlId"))
            {
                switch (e.InitParams["ControlId"])
                {
                    case "BookStore":
                        this.RootVisual = new BookStoreWithConverters();
                        break;

                    case "Page":
                        this.RootVisual = new Page();
                        break;
                }
            }
        }

Posted in Silverlight | Tagged: | 2 Comments »

Event Handling

Posted by ken zheng on July 30, 2008

Just like to post code for event handling for VB .NET and C# for reference
VB .NET

Imports System

Public Class CTimer

    Delegate Sub SecondDel(ByVal xintTime As Integer)
    Private evtSecond As SecondDel
    Public Event evtMinute As SecondDel
    Public Event evtHour(ByVal xHour As Integer)
    public Shared lngSeconds As Long

    Public Sub Register(ByVal objSecond As SecondDel)
        evtSecond = evtSecond.Combine(evtSecond, objSecond)
    End Sub

    Public Sub OnTimer()
        lngSeconds = lngSeconds + 1
        If lngSeconds Mod 5 = 0 Then
            evtSecond(lngSeconds)
        End If
        If lngSeconds Mod 10 = 0 Then
            RaiseEvent evtMinute(lngSeconds)
        End If
        If lngSeconds Mod 30 = 0 Then
            RaiseEvent evtHour(lngSeconds)
        End If
    End Sub

End Class

Public Class CClock

    Private WithEvents mobjTimer As CTimer

    Sub New()
        mobjTimer = New CTimer()
        mobjTimer.Register(New CTimer.SecondDel(AddressOf SecondEvent))
        AddHandler mobjTimer.evtMinute, AddressOf MinuteEvent
        While (mobjTimer.lngSeconds < 60)
            mobjTimer.OnTimer()
            System.Threading.Thread.Sleep(100)
        End While
    End Sub

    Private Sub SecondEvent(ByVal xintTime As Integer)
        Console.WriteLine("Second's Event")
    End Sub

    Private Sub MinuteEvent(ByVal xintTime As Integer)
        Console.WriteLine("Minute's Event")
    End Sub

    Private Sub mobjTimer_evtHour(ByVal xintTime As Integer) _
            Handles mobjTimer.evtHour
        Console.WriteLine("Hour's Event")
    End Sub

    Public Shared Sub Main()
        Dim cc1 = New CClock()
    End Sub

End Class

C#

using System;

public class CTimer
{

    public delegate void SecondDel(int xintTime);
    private SecondDel evtSecond;
    public event SecondDel evtMinute;
    public event evtHourEventHandler evtHour;
    public delegate void evtHourEventHandler(int xHour);
    public static long lngSeconds;

    public void Register(SecondDel objSecond)
    {
        evtSecond = evtSecond.Combine(evtSecond, objSecond);
    }

    public void OnTimer()
    {
        lngSeconds = lngSeconds + 1;
        if (lngSeconds % 5 == 0) {
            evtSecond(lngSeconds);
        }
        if (lngSeconds % 10 == 0) {
            if (evtMinute != null) {
                evtMinute(lngSeconds);
            }
        }
        if (lngSeconds % 30 == 0) {
            if (evtHour != null) {
                evtHour(lngSeconds);
            }
        }
    }

}

public class CClock
{

    private CTimer mobjTimer;

    public CClock()
    {
        mobjTimer = new CTimer();
        mobjTimer.Register(new CTimer.SecondDel(SecondEvent));
        mobjTimer.evtMinute += MinuteEvent;
        while ((mobjTimer.lngSeconds < 60)) {
            mobjTimer.OnTimer();
            System.Threading.Thread.Sleep(100);
        }
    }

    private void SecondEvent(int xintTime)
    {
        Console.WriteLine("Second's Event");
    }

    private void MinuteEvent(int xintTime)
    {
        Console.WriteLine("Minute's Event");
    }

    private void mobjTimer_evtHour(int xintTime)
    {
        Console.WriteLine("Hour's Event");
    }

    public static void Main()
    {
        object cc1 = new CClock();
    }

}

Posted in Uncategorized | Tagged: | Leave a Comment »