Thursday, February 23, 2012

How to a service in silverlight timer Tick

1.Take silverlight application asp.net page as ahosting page

2.here we can get two projects one is silverapp, silverapp.web.

3.go to silverapp.web and service referance.

4.then add clientaccesspolicy.xml in silverapp.web


<UserControl x:Class="Sil_timer.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

d:DesignHeight="300" d:DesignWidth="400">

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

<Button Content="Start Timer" Height="23" HorizontalAlignment="Left" Margin="12,130,0,0" Name="btn_start_timer" VerticalAlignment="Top" Width="75" Click="btn_start_timer_Click" />

<Button Content="Stop Timer" Height="23" HorizontalAlignment="Right" Margin="0,130,186,0" Name="btn_stop_timer" VerticalAlignment="Top" Width="105" />

<TextBlock Height="23" HorizontalAlignment="Left" Margin="57,40,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="157" />

Grid>

UserControl>

-------------------------

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.Windows.Threading;

namespace Sil_timer

{

public partial class MainPage : UserControl

{

int i = 0;

DispatcherTimer Dtimerobj = new DispatcherTimer();

public MainPage()

{

InitializeComponent();

}


private void btn_start_timer_Click(object sender, RoutedEventArgs e)

{

Dtimerobj.Interval = new TimeSpan(0, 0, 10);

Dtimerobj.Tick+=new EventHandler(Dtimerobj_Tick);

Dtimerobj.Start();

}


void Dtimerobj_Tick(object sender, EventArgs e)

{

textBlock1.Text = System.DateTime.Now.ToString();


//..........service call 4m sil app.....................//


ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();

obj.count_recdAsync();

obj.count_recdCompleted += new EventHandler(obj_count_recdCompleted);


}


void obj_count_recdCompleted(object sender, ServiceReference1.count_recdCompletedEventArgs e)

{

textBlock1.Text = e.Result.ToString();

}

}

}



No comments:

Post a Comment