Shalvin Interests

Thursday, April 21, 2011

WPF Animation

Animation in code

using System.Windows.Media.Animation;
private void button1_Click(object sender, RoutedEventArgs e)
{
    DoubleAnimation widthAnimation = new DoubleAnimation();
    widthAnimation.To = cmdGrow.Width * 2;
    widthAnimation.Duration = TimeSpan.FromSeconds(5);

    DoubleAnimation heightAnimation = new DoubleAnimation();
    heightAnimation.To = cmdGrow.Height * 2;
    heightAnimation.Duration = TimeSpan.FromSeconds(5);

    cmdGrow.BeginAnimation(Button.WidthProperty, widthAnimation);
    cmdGrow.BeginAnimation(Button.HeightProperty, heightAnimation);
}

Animation with Styles
<Window x:Class="XamlAnimationShalvin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
 <Window.Resources>

  <Style x:Key="GrowButtonWindowStyle">
   <Style.Triggers>
    <Trigger Property="Button.IsPressed" Value="True">
     <Trigger.EnterActions>
      <BeginStoryboard>
       <Storyboard>
        <DoubleAnimation Storyboard.TargetProperty="Width"
          To="250" Duration="0:0:5"></DoubleAnimation>
       </Storyboard>
      </BeginStoryboard>
     </Trigger.EnterActions>
    </Trigger>
   </Style.Triggers>
  </Style>

 </Window.Resources>
   <Button Padding="10" Name="cmdGrow" Height="40" Width="160" Style="{StaticResource GrowButtonWindowStyle}"
          HorizontalAlignment="Center" VerticalAlignment="Center">
        Click and Make Me Grow
  </Button>
</Window>

Wednesday, March 23, 2011

Bing Map Silverlight Control Dynamically Adding Pushpins

Silverlight Bing Map controls enables you to have Map functionality in your Silverlight application. Once you install Bing Maps Silverlight Control you will get a directory name Bing Maps Silverlight Controls under Program Files. Inside V1, Libraries you can find the associated dlls.

Add a reference to both the controls.
Add a namespace to Page section pointing to the dll.

  xmlns:Map="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"

 Now you can use the control

 <Map:Map Name="map1">
    <Map:Pushpin Location="9,76" Content="Kochi"/>
 </Map:Map>

Creating dynamic Pushpins
using Microsoft.Maps.MapControl;

Shalvin.Service1Client sc = new Shalvin.Service1Client();
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           
            sc.getlocationCompleted += new EventHandler<Shalvin.getlocationCompletedEventArgs>(sc_getlocationCompleted);
            sc.getlocationAsync();
        }

        void sc_getlocationCompleted(object sender, Shalvin.getlocationCompletedEventArgs e)
        {
            var locs = e.Result.ToList();
            Pushpin p;
            Location l;
            foreach (var g in locs)
            {
                p = new Pushpin();
                double la = double.Parse(g.Latitude.ToString());
                double lo = double.Parse(g.Longitude.ToString());
                l = new Location { Latitude = la, Longitude = lo };
                p.Content = g.LocationName;
                p.Location = l;
                map1.Children.Add(p);
            }
        }
    }

Thursday, January 27, 2011

Asp .Net Interview Question

1. What are System.Web.Mail and System.Net.Mail?

System.Web.Mail has been deprecated, use System.Net.Mail instead.

System.Net.Mail namespace is used to send electronil mail to Simple Mail Transfer Protocol (SMTP) server for delivery.

2. What is generics?
Generic is a language feature introduced in .Net 2.0. Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Use of generics makes your code type safe. It is a very efficient way to work with collections.

C# does not allow non-type template parameters.

C# does not support explicit specialization; that is, a custom implementation of a template for a specific type.

C# does not allow the type parameter to be used as the base class for the generic type.

In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters.

Generics and Collection Initializer in .Net

3. What is boxing and unboxing?

Boxing is the process of converting a value type to the type object. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system, in which a value of any type can be treated as an object.
.NET Interview Questions You'll Most Likely Be Askedint i = 12;

object o = i; 
 

 Unboxing

o = 12;
i = (int)o; 


4. What is the difference between DataRowCollection.Remove method and DataRowCollection.Delete method?

DataRowCollection.Remove method removes a DataRow from a DataTable where as DataRowCollection.Delete method marks a row for deletion.


Calling Remove is the same as calling Delete and then calling AcceptChanges.
You can also use the Clear method to remove all members of the collection at one time.

 5. What are Extension Methods?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.



6. What is the use of System.Threading.ReaderWriterLockSlim Class?

Use ReaderWriterLockSlim to protect a resource that is read by multiple threads and written to by one thread at a time. ReaderWriterLockSlim allows multiple threads to be in read mode, allows one thread to be in write mode with exclusive ownership of the lock, and allows one thread that has read access to be in upgradeable read mode, from which the thread can upgrade to write mode without having to relinquish its read access to the resource.

Wednesday, January 19, 2011

Compression in .Net

using System.IO;
using System.IO.Compression;

static void Main(string[] args)
{
CompressFile(@"d:\ShalvinSmall.jpg", @"d:\S.jpg.zip");
DecompressFile(@"d:\s.jpg.zip", @"d:\Aju.jpg");
System.Console.ReadLine();
}

static void CompressFile(string inFilename,
                     string outFilename)
{

FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);

// Create the Compressed stream
GZipStream compStream =
new GZipStream(destFile, CompressionMode.Compress);

// Write the data
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}

// Clean it up
sourceFile.Close();
compStream.Close();
destFile.Close();
}

static void DecompressFile(string inFilename, string outFilename)
{
FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);

// Create the Compressed stream
GZipStream compStream =
new GZipStream(sourceFile, CompressionMode.Decompress);

// Write the data
int theByte = compStream.ReadByte();
while (theByte != -1)
{
destFile.WriteByte((byte)theByte);
theByte = compStream.ReadByte();
}

// Clean it up
sourceFile.Close();
compStream.Close();
destFile.Close();
}

Friday, January 14, 2011

jQuery with Visual Studio 2010

Applying CSS based on click event
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="5JQueryCSS.aspx.cs" Inherits="_5JQueryCSS" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head> 
    <title></title>
    <script src="Scripts/jquery-1.12.1.js"></script>
    <script>
        $("document").ready(function()
        {
            $("#btnHighlight").click(function ()
            {
                $("p").css("border", "3px solid red");
                return false;
            });
         
        });
        </script>
    <form id="form1" runat="server">
    <div>
    
        <p>Shalvin</p>
        <p>Biju</p>
        <p>Reny</p>

        <h3>JavaScript Technologies</h3>
        <ul>
            <li>JQuery</li>
            <li>AngularJS</li>
            <li>CommonJS</li>
            <li>NodeJS</li>
        </ul>
    </div>
        <asp:Button ID="btnHighlight"  runat="server" Text="Highlight" />
    </form>
</body>
</html>

Tuesday, January 11, 2011

Symmetric Encryption in .Net

using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;


       Rfc2898DeriveBytes passwordKey;
        RijndaelManaged alg;
        FileStream inFile;
        ICryptoTransform encryptor;
        FileStream outFile;
        CryptoStream encryptStream;

        string inFileName;
        string outFileName;
        string password;
        byte[] saltValueBytes;
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            saltValueBytes = Encoding.ASCII.GetBytes("ShalvinPD");
        }
        private void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EncPrepare();

                // Read the unencrypted file into fileData
                inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                byte[] fileData = new byte[inFile.Length];
                inFile.Read(fileData, 0, (int)inFile.Length);


                encryptor = alg.CreateEncryptor();
                outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);

                // Write the contents to the CryptoStream
                encryptStream.Write(fileData, 0, fileData.Length);

                // Close the file handles
                encryptStream.Close();
                inFile.Close();
                outFile.Close();
                MessageBox.Show("File encrypted successfully");
                Process.Start("notepad", txtDestination.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

      

        private void btnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            EncPrepare();
 
            // Read the encrypted file into fileData
            ICryptoTransform decryptor = alg.CreateDecryptor();
            inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
            CryptoStream decryptStream = new CryptoStream(inFile, decryptor, CryptoStreamMode.Read);
            byte[] fileData = new byte[inFile.Length];
            decryptStream.Read(fileData, 0, (int)inFile.Length);

            // Write the contents of the unencrypted file
            outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
            outFile.Write(fileData, 0, fileData.Length);

            // Close the file handles
            decryptStream.Close();
            inFile.Close();
            outFile.Close();
            MessageBox.Show("File encrypted successfully");
        }

        private void EncPrepare()
        {
            inFileName = txtSource.Text;
            outFileName = txtDestination.Text;
            password = txtKey.Text;
            passwordKey = new Rfc2898DeriveBytes(password, saltValueBytes);


            alg = new RijndaelManaged();
            alg.Key = passwordKey.GetBytes(alg.KeySize / 8);
            alg.IV = passwordKey.GetBytes(alg.BlockSize / 8);

          
        }
    }

WPF Xaml


<Window x:Class="WpfEncryptionShalvin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shalvin Encryption" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Label Content="Source File Name" Height="28" HorizontalAlignment="Left" Margin="62,42,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,42,0,0" Name="txtSource" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,135,0,0" Name="txtDestination" VerticalAlignment="Top" Width="120" />
        <Label Content="Destination File Name" Height="28" HorizontalAlignment="Left" Margin="62,130,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="Key" Height="28" HorizontalAlignment="Left" Margin="62,88,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,88,0,0" Name="txtKey" VerticalAlignment="Top" Width="120" />
        <Button Content="Encrypt" Height="23" HorizontalAlignment="Left" Margin="62,202,0,0" Name="btnEncrypt" VerticalAlignment="Top" Width="75" Click="btnEncrypt_Click" />
        <Button Content="Decrypt" Height="23" HorizontalAlignment="Left" Margin="221,202,0,0" Name="btnDecrypt" VerticalAlignment="Top" Width="75" Click="btnDecrypt_Click" />
    </Grid>
</Window>


Hashing

using System.Security.Cryptography;

private void btnHash_Click(object sender, EventArgs e)
{
    HashAlgorithm hashA = new SHA1CryptoServiceProvider();
byte[] pwordData = Encoding.Default.GetBytes(txtPlainText.Text);
byte[] hash = hashA.ComputeHash(pwordData);
txtHashCode.Text = BitConverter.ToString(hash);
}

Monday, December 13, 2010

WPF Console

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;

namespace WpfBlankProjShalvin
{
    class Class1 : Window 
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new Class1 { Title = "Shalvin", WindowStartupLocation = WindowStartupLocation.CenterScreen, Width = 300, Height = 200  });
        }
    }
}



WPF StackPanel and Button Click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;

using System.Windows.Controls;

namespace WpfApplication1
{
    class Class1 : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new Class1());
        }

        StackPanel stp;

        Button btn;
        public Class1()
    {
 stp = new StackPanel();
 this.Content = stp;

 btn = new Button { Content = "Shalvin" };
            btn.Click += new RoutedEventHandler(btn_Click);
 stp.Children.Add(btn);

    }


        private void btn_Click(Object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello");
        }
    }
}



Grid RowDefinition

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;

using System.Windows.Controls;

namespace WpfGrid
{
    class Class1 : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new Class1());
        }

        Grid grd;

        TextBlock tbl;
        RowDefinition rd;
        
        public Class1()
        {
            grd = new Grid { ShowGridLines = true };
            this.Content = grd;
            rd = new RowDefinition ();
            grd.RowDefinitions.Add(rd);
            
            rd = new RowDefinition();
            grd.RowDefinitions.Add(rd);

            tbl = new TextBlock { Text = "Name" };
            grd.Children.Add(tbl);
        }

    }
}