Monday 9 September 2013

WPF - Create a Glassy Effect button

Create a Glassy effect button along with some effects is simple in WPF. On Click the button it will give a effect of zoom in and zoom out



WPF Code:
<Window x:Class="Glassy_Buttons.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="Glassy" TargetType="{x:Type Button}">
            <Setter Property="FontSize" Value="42" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="ButtonBorder"
                  CornerRadius="25,25,25,25"
                  BorderThickness="4,4,4,4"
                  Background="#AA000000" 
                  BorderBrush="#99FFFFFF"
                  RenderTransformOrigin="0.5,0.5">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="1.7*"/>
                                </Grid.RowDefinitions>
                                <Border Grid.Row="0" CornerRadius="23,23,0,0">
                                    <Border.Background>
                                        <LinearGradientBrush StartPoint="0.5,0"
EndPoint="0.5,1">
                                            <GradientStop Color="#08FFFFFF" Offset="0"/>
                                            <GradientStop Color="#88FFFFFF" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                </Border>
                                <Canvas Width="50.3908446126037"
Height="50.3053963385331" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                                <Canvas Canvas.Left="0.000932676260788412"
Canvas.Top="-0.000155911959353716">
                                        <Canvas>
                                            <Canvas>
<Path Data="F1M566.74606,-39.409218A35.708893,35.708893,0,1,1,566.73848,-40.145326" StrokeThickness="2.95431923866272" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" StrokeMiterLimit="4">
     <Path.Fill>
 <RadialGradientBrush Center="531.03717,-27.63166" RadiusX="37.708893" RadiusY="37.708893" GradientOrigin="531.03717,-27.63166" MappingMode="Absolute">
                                                            <RadialGradientBrush.GradientStops>
    <GradientStop Color="#FF8FE68C" Offset="0" ></GradientStop>
    <GradientStop Color="#FF29902B" Offset="1" ></GradientStop>                                                            </RadialGradientBrush.GradientStops>
                                                            <RadialGradientBrush.Transform>
  <MatrixTransform Matrix="0.870558202266693,0,0,0.870558202266693,68.7383880615234,-5.10119867324829" ></MatrixTransform>
                                                            </RadialGradientBrush.Transform>
 </RadialGradientBrush>
            </Path.Fill>
        <Path.Stroke>
       <LinearGradientBrush StartPoint="531.00635,-1.2397567"
EndPoint="531.00635,-76.400444" MappingMode="Absolute"
SpreadMethod="Pad">
                                                            <LinearGradientBrush.GradientStops>
    <GradientStop Color="#FF286A27" Offset="0" ></GradientStop>
   <GradientStop Color="#FF264826" Offset="1" ></GradientStop>                                                            </LinearGradientBrush.GradientStops>
 </LinearGradientBrush>
     </Path.Stroke>
       <Path.RenderTransform>
 <MatrixTransform Matrix="1,0,0,1,-49.888427734375,-125.27271270752" >
</MatrixTransform>
</Path.RenderTransform>
                                                </Path>
                                                <Canvas>

<Path Data="F1M523.42302,-241.28365L523.42302,-202.95888 556.35974,-221.9749 523.42302,-241.28365z" Fill="#80000000" Stroke="#80000000" StrokeThickness="3.07599997520447" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" StrokeMiterLimit="4" Opacity="0.5" ></Path>

<Path Data="F1M521.65524,-243.05142L521.65524,-204.72664 554.59196,-223.74267 521.65524,-243.05142z" Fill="#FFD6FFDC" Stroke="#FF33543C" StrokeThickness="3.07619142532349" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" StrokeMiterLimit="4" ></Path>
<Canvas.RenderTransform>

<MatrixTransform Matrix="1,0,0,1,-54.858699798584,58.8232688903809" >
</MatrixTransform>
</Canvas.RenderTransform>
</Canvas>

<Path Data="F1M581.24179,-21.201218A23.688078,17.854446,0,1,1,533.86563,-21.201218A23.688078,17.854446,0,1,1,581.24179,-21.201218z" Stroke="{x:Null}" StrokeThickness="1">
<Path.Fill>
<LinearGradientBrush StartPoint="557.55371,-39.055664"
EndPoint="557.55371,-3.3467712" MappingMode="Absolute" SpreadMethod="Pad">
                                                            <LinearGradientBrush.GradientStops>
<GradientStop Color="#FFFFFFFF" Offset="0" ></GradientStop>
<GradientStop Color="#00FFFFFF" Offset="1" ></GradientStop>
                                                            </LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Path.Fill>
<Path.RenderTransform>

<MatrixTransform Matrix="1.01492536067963,0,0,1.03960394859314,-84.7266845703125,-157.136749267578" >
</MatrixTransform>
</Path.RenderTransform>
</Path>
<Path Data="F1M451.4166,-150.26926C458.5178,-134.47141 476.85907,-126.84623 493.16944,-133.22123 509.47092,-139.59275 517.7814,-157.61841 512.30576,-174.03735 507.88172,-162.93148 499.03206,-153.62636 487.02784,-148.93445 475.01193,-144.23796 462.20568,-145.09352 451.4166,-150.26926z" Stroke="{x:Null}" StrokeThickness="1">
                                                    <Path.Fill>
                                                        <LinearGradientBrush StartPoint="493.474169940959,-133.338522737016" EndPoint="486.709026263318,-158.58628965847" MappingMode="Absolute" SpreadMethod="Pad">
                                                            <LinearGradientBrush.GradientStops>
<GradientStop Color="#FFFFFFFF" Offset="0" ></GradientStop>
<GradientStop Color="#00FFFFFF" Offset="1" ></GradientStop>                                                            </LinearGradientBrush.GradientStops>
  </LinearGradientBrush>
  </Path.Fill>
  </Path
 <Canvas.RenderTransform>
                                                   
<MatrixTransform Matrix="0.677549779415131,0,0,0.676400482654572,-394.662322998047,131.968307495117" >
</MatrixTransform>
</Canvas.RenderTransform>
</Canvas>
<Canvas.RenderTransform>
<MatrixTransform Matrix="1,0,0,1,93.8546295166016,4.5761513710022" >
</MatrixTransform>
</Canvas.RenderTransform>
</Canvas>
</Canvas>
</Canvas>
<ContentPresenter x:Name="ButtonContentPresenter"
                                VerticalAlignment="Center" 
                                Grid.RowSpan="2"
                                HorizontalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="RenderTransform"
TargetName="ButtonBorder">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                        </TransformGroup>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Button  Height="58" Width="100" Style="{DynamicResource Glassy}"></Button>

</Window>

Output:


On Press the button gives you a Zoom in and Zoom Out Effect.From this article you can learn how to create a glassy effect buttons.

Sunday 8 September 2013

Javascript - Allow textbox only numeric on enter

Javascript:

        Using Javascript we can enable a textbox to allow only numeric , this is can done by following function. use the onkeydown this event is fires when user enter the value in Textbox.Passes the event as parameter to the function using that get the keyCode from that event. keyCode is case sensitive. Based on the keyCode validate and return the boolean type to onKeydown event.

keyCode : 48-57 is numeric type

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
    function numeric(ent) {

        ent = (ent) ? ent : window.event;
        var keycode = (ent.keyCode) ? ent.keyCode : ent.which;
        if(keycode > 31 && (keycode < 48 || keycode > 57))
        {
        return false;
        }
        else
        {
        return true;
        }
    }
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="txt" onkeydown="return numeric(event);"></asp:TextBox>
    </div>
    </form>
</body>
</html>





WCF Basic - Part 1

          Windows communication Foundation (WCF) is a framework for building a service oriented loosely coupled application.You can send the messages to one endpoint to another endpoint. WCF consists of all functional of web service and also additional many more things. It is designed for Distributed computing, broad interoperability and  for service orientation.


Contract :  Which specifies the what are the operations can be perform by end user.

To create a WCF service 

1. Create a Interface and specifies a Service Contract as attribute.
2. Mention the methods inside the Interface and specifies the Operation contract as attribute 
3. Derive the Interface in the service class and implement the method this is the good practice
4. Specifies the input parameter for the method as Message Contract.
5. If DataContract parameter is uses and Messgae contract parameter is uses then Message contract is overrides the Data Contract parameter

Message Contract:
  Message Contract is to specify in which format that the soap message is organised while sending and receiving the data. MessageBodyMember is used to specify the parameters of DataContract So many properties are there for MessageBodyMember

         Name : Which is used to specify the external name for client to access.
         ProtectionLevel : three types None, Sign, EncryptAndSign
        Order : Specify the order of the parameter


[MessageContract]
    public class Offer
    {
        [MessageHeader]
        public DateTime JoinedDate { set; get; }
       
        [MessageBodyMember]
        public bool IsPermenant { set; get; }

        [MessageBodyMember]
        public bool IsExperience { set; get; }

        [MessageBodyMember(Name="OfferLetter",Order=2,ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
        public OfferLetter offer { set; get; }

    }


Service Contract :
     Service Contract is like WebService and operation contract is like webMethod notation.

Data Contract:
  Data Contract is used to specify the data and DataMember is a indication of Property,Order property specify in the parameter of DataMember in the same order it is serialized. Name is property in DataMember class is used as Alias name in client side.
     
     [DataMember(Order = 1, Name = "Exception")]


EndPoint
   EndPoint is the notation at which place we can access the service through which binding.


<endpoint address="http://localhost:14849/EmployeeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IOffice"
                contract="ServiceReference1.IOffice" name="BasicHttpBinding_IOffice" />


Address
  Address of the service URL. That indicates where the end point can be found.

Binding
  Binding indicates that how we can communicate with the end point


<bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IOffice" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>

        
   Behavior
  You can use the end point behavior to customize the local behavior of Service end point.

FaultException:
  Specify the Fault Exception instead of throw general exception from the application. Specifies the Fault Contract attribute after the operation contract declaration in interface and there typeof Fault is pass as parameter to the constructor.
        [OperationContract]
        [FaultContract(typeof(EmployeeException))]

        Employee GetEmployee(Employee empid);


Create a WCF Service :
1. Launch a Visual Studio 2010
2. Create a new project.
3. Select WCF Service Application.


Interface : Service Contract 
[ServiceContract]
    public interface IOffice
    {
        [OperationContract]
        Offer Joined();

        [OperationContract(Name = "EmployeeExists")]
        bool EmpExists(Employee emp);

        [OperationContract]
        [FaultContract(typeof(EmployeeException))]
        Employee GetEmployee(Employee empid);

    }


Message Contract 
Name property specifies a alias name , order property specifies the order of the property, protection level specifies the type of encryption.

[MessageContract]
    public class Offer
    {
        [MessageHeader]
        public DateTime JoinedDate { set; get; }

        [MessageBodyMember]
        public bool IsPermenant { set; get; }

        [MessageBodyMember]
        public bool IsExperience { set; get; }

        [MessageBodyMember(Name = "OfferLetter", Order = 2, ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
        public OfferLetter offer { set; get; }

    }


Data Contract 

[DataContract]
    public class OfferLetter
    {
        [DataMember]
        public string Message { set; get; }

        [DataMember]
        public int SalaryPackage { set; get; }
    }

********************************************************************************
Service Code: In this service we have three methods, 1. Joined()  2. EmpExists() 3. GetEmployee()
********************************************************************************

[DataContract]
    public class OfferLetter
    {
        [DataMember]
        public string Message { set; get; }

        [DataMember]
        public int SalaryPackage { set; get; }
    }

    [MessageContract]
    public class Offer
    {
        [MessageHeader]
        public DateTime JoinedDate { set; get; }

        [MessageBodyMember]
        public bool IsPermenant { set; get; }

        [MessageBodyMember]
        public bool IsExperience { set; get; }

        [MessageBodyMember(Name = "OfferLetter", Order = 2, ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
        public OfferLetter offer { set; get; }

    }

    [ServiceContract]
    public interface IOffice
    {
        [OperationContract]
        Offer Joined();

        [OperationContract(Name = "EmployeeExists")]
        bool EmpExists(Employee emp);

        [OperationContract]
        [FaultContract(typeof(EmployeeException))]
        Employee GetEmployee(Employee empid);

    }

    [DataContract]
    public class EmployeeException
    {
        [DataMember]
        public int Id { set; get; }

        [DataMember(Order = 1, Name = "Exception")]
        public string ExceptionMessage
        { set; get; }
    }

    [DataContract]
    public class Employee
    {
        [DataMember(IsRequired = true)]
        public string Id { set; get; }

        [DataMember(IsRequired=true, EmitDefaultValue = true)]
        public String Name { set; get; }

        [DataMember(Name = "Mobile No", Order = 2)]
        public string PhoneNo { set; get; }

    }

  
    public class EmployeeService : IOffice
    {
        EmployeeCollection coll = new EmployeeCollection();

        public Offer Joined()
        {
            return new Offer() { JoinedDate = DateTime.Now, IsPermenant = true, offer = new OfferLetter() { Message = "We are pleased to announce that offering a Job in our company.",SalaryPackage=550000} };
        }

        public bool EmpExists(Employee emp)
        {
            if (! string.IsNullOrEmpty(emp.Id))
            {                                 
                return coll.Any(x=>x.Id==emp.Id);              
            }
            else
            {
                throw new FaultException(new FaultReason("Employee not working in company"));
            }
        }

        public Employee GetEmployee(Employee empid)
        {
            if (!string.IsNullOrEmpty(empid.Id))
            {               
                return coll.Where(x => x.Id == empid.Id).FirstOrDefault();
            }
            else
            {
                EmployeeException ex = new EmployeeException() { ExceptionMessage="Employee  "+empid.Id+" is not working in our company"};
                throw new FaultException<EmployeeException>(ex,new FaultReason("Employee id is not present"));
            }
        }
    }

  public class EmployeeCollection:ObservableCollection<Employee>
    {
        public EmployeeCollection()
        {
            Add(new Employee() {Id="FG1",Name="Rajesh",PhoneNo="8056220725" });
            Add(new Employee() {Id="FG2",Name="Krish",PhoneNo="4535443223" });
            Add(new Employee() {Id="FG3",Name="Raju",PhoneNo="5635434334"});          
        }
    }

********************************************************************************
Invoking the service from the Client:
********************************************************************************
Create a Console Application and add a Service Reference the corresponding address.

Program.cs
*********
static void Main(string[] args)
        {
            try
            {
                OfficeClient client = new OfficeClient();
                Offer person1 = new Offer();
                OfferLetter letter=new OfferLetter();
                DateTime joineddate= client.Joined(out person1.IsExperience, out person1.IsPermenant, out letter);

                Console.WriteLine("Joined in Company");
                Console.WriteLine("");
                Console.WriteLine("Joined Date :{0},/nOfferLetter : {1},\nSalary : {2},\nIsExperience : {3},\nIsPermanentEmployee : {4}", joineddate, letter.Message, letter.SalaryPackage, person1.IsExperience, person1.IsPermenant);
                Console.WriteLine("");
                bool exists=client.EmployeeExists(new Employee() { Id="FG8" });
                Console.WriteLine("FG8  Employee Exists : " + exists);
                Console.WriteLine("");
                Employee emp = client.GetEmployee(new Employee() { Id = "FG1" });
                Console.WriteLine("Employee Details : id = {0}, Name = {1}, Mobile Number = {2}", emp.Id, emp.Name, emp.MobileNo);
                Console.Read();
            }           
            catch (FaultException<EmployeeException> ex)
            {
                Console.WriteLine(ex.Detail.Id);
            }
            catch (FaultException ex)
            {
                Console.WriteLine(ex.Reason);
            }
        }
**************************************************************************
Output:
**************************************************************************


From the article you can learn some of the basics of WCF.