Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Monday, January 11, 2010

Webservices using Silverlight 2.0

In silverlight can't connect directly to the database. it can only connect through webservices.
Before connecting to webservices. we should aware of security, data integrity, binding, performance.
using silverlight user connect diferent services.the services are webservice, WCF service and REST.
hope know about webservice and wcf, so all are asking about REST. Rest means RePresentation of state transition.
through webclient and httpwebrequest can connect and get the response.
usually webservices can connect through http. while connecting you have to maintain the thread. it's should
run with in thread only. if you not connect through proxy. you should maintain the thread. other wise it would
be problem.
Method 1:
Calling ASMX webservices.
Step 1:
Create asmx webservice and in to silverlight application as a service reference.
Step 2:
ADD namespace in the using section.
Step 3:
Get path of asmx file through this method.
public string GetURLforResource(string resourcePage)
{
string webURL = HtmlPage.Document.DocumentUri.ToString();
string containerPage = webURL.Substring(webURL.LastIndexOf("/") + 1);
return webURL = webURL.Replace(containerPage, resourcePage);
}

Step 4:
Call webservice through this. string webServiceURL = GetURLforResource("WebserviceTest.asmx");
BasicHttpBinding binding = new BasicHttpBinding();
WebserviceTestSoapClient webSVC = new WebserviceTestSoapClient(binding,new EndpointAddress(webServiceURL));
webSVC.HelloWorldCompleted += new EventHandler(webSVC_HelloWorldCompleted);
webSVC.HelloWorldAsync();

also for the method has to create the eventhandler.
Step 5:
This is the event handler to display the result of the webservice method.
void webSVC_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
{

HtmlPage.Window.Alert(e.Result);
}

Method 2:
Calling WCF service.
Everything same as like the above. one thing must to do in web.config.
while creating wcf it will add automatically binding as wsHttpBinding instead of that
have to modify basicHttpBinding.
Method 3:
Coming soon










-

Friday, January 8, 2010

Navigation between XAML Pages using Silverlight 2.0

Before going in to navigation one thing keep in mind there is no direct way for navigating the xaml files in Silverlight 2.0.
Ok fine let's start our navigation journey. I have two ways to navigate between the xaml files.
let me explain one by one.
# Method 1: Using App.XAML
Step 1:
There is a Application_startup event in App.XAML which will decide the initial load page.
Before that comment all the line in this method. (Example://this.RootVisual = new Page();)
Here in this have to do some little trick.
Step 2:
Declare one public Grid outside the event.
like this: public static Grid root;
and then write this code in the application Startup event.

private void Application_Startup(object sender, StartupEventArgs e)
{
root = new Grid();
root.Children.Add(new Login());
this.RootVisual = root;
}

here only we are adding Grid as a parent control. under that we have xaml page.
Step 3:
Create one more method for navigation.
public static void Navigate(UserControl newPage)
{
UserControl oldpage = root.Children[0] as UserControl;
root.Children.Add(newPage);
root.Children.Remove(oldpage);
}
This is the method will navigate one xaml to another xaml. Basically it's just removing the old page and adding new page.
The above all three steps only in the App.XAML file.
Step 4:
Now come to Login.Xaml, this is the inital page which will load from App.XAML.
After successing the login user has to navigate to productlist page.
The below code will navigate to product list page.
private void Login_Success_Click(object sender, RoutedEventArgs e)
{
App.Navigate(new Product());
}


#Method 2:
This is better approach compare to above.
In the silverlight application should have two files.
1) App.XAML2) Master.XAML
here in Master is the common file which will navigate the files through this.
let me go through step by step:
Step 1:
In the application_startup event load the Master.XAMl (Remove Automated code)
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Master();
}

Step 2:
In the master page clear all the content except usercontrol tag.
<UserControl x:Class="SilverlightThird.Master" xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> </UserControl>
Step 3:
In the code behind of Master page write the below code.
public partial class Master : UserControl
{
public Master()
{

InitializeComponent();
if (this.Content == null)
{
this.Content = new Login();
}
}

public void Navigate(UserControl Nextpage)
{
this.Content = Nextpage;
}
}

Step 4:
Now come to Login.xaml file.
In the success method of login.xaml.cs file write this below it will navigate.

hope this will help.













Custom Fonts in Silverlight 2.0

Hi all today i have learned something called custom fonts in silverlight 2.0.
it's really surprising me, it's very good feature.
follow the below stpes to create custom fonts in silverlight 2.0

Step1:
Create one silverlight applicaton. mean while get ttf extension file name.
Note: ttf is the extension for font.
step2:
Add atleast one ttf file in to our project folder
Example:webdings.ttf
step3:
after adding in to our project. add the code like the below in example.
then you will get the custom fonts in your browser.
Syntax:
FontFamily="Font_File_Name#Font_Name”
Example:
<TextBlock FontFamily="webdings.ttf#webdings" Text="Test"> </TextBlock>


Note:font_file_name and font_name is different except few.
if you give correct font_name then only it will work.

Accesssing Client bin Images in XAML.

In the client can not only xap file also can contain images. Those images can acccess from silverlight application.


Client Bin
Bluehils.jpg
imageaccess.xap

the above two files are there in the client bin.


if you want to access that image have to simple thing.


<Image Source="/Bluehills.jpg"> </Image>


Note:"/"

Thursday, January 7, 2010

Stylesheet using Silverlight.


In silverlight having different approach to creating styles. also styles can write in app.xaml in silverlight application.
app.xaml is a application resource file. it will shares styles accross all the pages.
also it will maintain all the configuration details.
This is the syntax for creating styles in App.XAML.


<Application.Resources>
<Style x:Key="stTextblock" TargetType="TextBlock"> <
Setter Property="Foreground" Value="Red"/> </Style>
</Application.Resources>

we can call this style accross all the pages and it's applicable only for TextBlock controls.


<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="txtShow" Style="{StaticResource stTextblock}" Text="Bala">
</TextBlock> </Grid>

Tuesday, January 5, 2010

Best Practices for keeping large number of images in Silverlight

Before creating silverlight application. Create seprate soltion for keeping large number images.Application performance will increase. also maintaining the application will be easy.

Step 1:
Create one silverlight application.
step 2:
Create silverlight class library.The name of the project solution is "Resource"
step 3:
maintain all the images under "Resource" library.
step 4:
Add the "Resouce" reference to silverlight application.
step 5:
Bind the images in to silverlight application.
Example
like this:<image source="Resource;component/picture/winter.jpg>
"Reource" is Assembly or application name.

Picture is folder name

SLsvcutil.exe in silverlight

Generating Proxies for WCF Services in Silverlight.
The only way to generate the proxies in silverlight 2 through service reference.
But in silverlight 3 is different. it's having command line tool to geneate the proxies.
SLsvcutil.exe.
happy coding with silverlight.

Tuesday, December 29, 2009

Silver light 2.0 Timer

I have seen many forums asking about the timer so i thought i'd toss up a simple example.
To create timer have to use Dispatchtimer in System.Windows.Threading.
Go through the below example.
<Canvas>
<Canvas.Resources>
<Storyboard x:Name="Timer">
<DoubleAnimation Storyboard.TargetName="rectTimer" Storyboard.TargetProperty="Width" BeginTime="0:0:0" Duration="2"></DoubleAnimation>
</Storyboard>
</Canvas.Resources>
<Rectangle Loaded="rectTimer_Loaded" Visibility="Collapsed" x:Name="rectTimer">
</Rectangle>
</Canvas>

private void rectTimer_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0,1, 100);
myDispatcherTimer.Tick +=new EventHandler(myDispatcherTimer_Tick);
myDispatcherTimer.Start();
}
void myDispatcherTimer_Tick(object sender, EventArgs e)
{
HtmlPage.Window.Alert("TickEvent");
}

Monday, December 28, 2009

Making Transparent In Silverlight..

Now take a example of grid
Example:
<Grid x:Name="LayoutRoot" Background="{x:Null}">

How do we extract "XAP" file in Silverlight?


It's like archive and contains all the resources of silverlight application.
How do we extract xap file?
Step1:
Copy the xap file from clientBin and store in desktop of your machine.
step2:
change the extension in to zip
Example:
test.xap
test.xap.zip
Now you can extract the file.
it's contains one dll and asemblymanifest.info







Event Handler Using Silverlight

It's possible to create the eventhandler through XAML. also using codebehind can attach
event dynamically. It's usefull for dynamically craeate controls.
Example:
<Button Name="btnTest" Content="Test" Width="100" Background="Blue" Margin="1" Height="25"></Button>
public Page()

{
InitializeComponent();
btnTest.Click += btnTest_Click; //Attach Event
btnTest.Click -= btnTest_Click; //Detach Event
}

void btnTest_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hi How are you");
}

What is use of AppManifest.xml and Assemblyinfo.cs in Silverlight


App(Application) Manifest.
This is the place where we can find the list of assemblies used in the application.
Location Path:Properties/AppManifest
ApplicationInfo
It's contains the information about the project like version, name and publisher.
Location: Properties/Assemblyinfo.cs

Wednesday, December 23, 2009

HtmlPage.Window.Invoke in Silverlight

This method will invoke the javascript method from code behind.
Example

Page.xaml
<UserControl x:Class="_22Dec2009.Page" xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="1024" Height="800"> <Grid x:Name="LayoutRoot" Background="White"> <Border BorderThickness="2" BorderBrush="AliceBlue" CornerRadius="2" Margin="3"> <Button x:Name="Test" Click="Button_Click" Content="Test" Background="AliceBlue" Width="30" Height="20"> </Button> </Border> </Grid></UserControl>
Page.xaml.cs
This is code it will call the javascript method.
private void Button_Click(object sender, RoutedEventArgs e)

{ HtmlPage.Window.Invoke("getAlert", false); }
Default.aspx
<script language="javascript">
function getAlert() {
alert("sdf"); } </script>
Have you noticed in the eventargs. here in silverlight different..

Note:
Also we can call the javascript method throgh this code.
HtmlPage.Window.CreateInstance("getAlert", false);
happy coding! Enjoy


Response Redirect in Silverlight.


here in silverlight we have to use below code navigate to from one pages to another pages.

in the code we can mention target as well as browser features like height width....

Example
HtmlPage.Window.Navigate(new Uri("
http://www.google.com"),"_blank","width:100,height:20");

Full Screen Options in Silverlight.

i am just come to know in silverlight from the coding level we can make full screen for the browser.

This is namespace have to use for the making the full screen.
System.Windows;
Example code is
Application.Current.Host.Content.IsFullScreen=!Application.Current.Host.Content.IsFullScreen

Wednesday, August 12, 2009

What is the use of "Canvas" Controls?????

Canvas is one of the layout control for the silverlight. This is just like container will contain the controls.

In silverlight wright now we have three layout controls. The layout types are as given below.

1)Canvas.
2)StackPanel.
3)Grid.


My first feel with Silverlight

first of all have to say thanks to microsoft to developing this kind of product.

I have started learning about the silverlight. it's nearly nice to see in the browser with rich user experience. Really interesting to learn all new controls and new way of approuch. This is really very good treat for dotnet developer.

Let's start learning with next generation technology.