Wednesday, 6 March 2013

Shapes and Custom Drawing in WPF/Silverlight




Shapes and Custom Drawing in WPF/Silverlight


In this article I will tell you how to create shape and create custom drawing using path property.

There are three common properties for shape listed below.

1. Fill      :- This property is used to fill shape with color.
2. Stroke :- This property describes border color for the shape.
3. StokeThickness :- This property set shape border thickness.

Now, we will discuss various shapes into details.

1.Line

The Line element of XAML draws a line between two points.
There are four attributes to draw a line (X1,Y1,X2,Y2) represents start point and end point for X and Y co-ordinates of line.


<Line Stroke="#000fff" StrokeThickness="2" X1="100"  Y1="100"
                X2="300" Y2="100"/


2. Rectangle

The Rectangle element of XAML draws a rectangle




 
      
   <Rectangle Width="300" Height="100" Fill="Black" Stroke="red" StrokeThickness="5">
        </Rectangle>


        <Rectangle Width="120" Height="100" RadiusX="40" RadiusY="40"
  Stroke="Green" StrokeThickness="5" Fill="Black">
        </Rectangle>

      
In the above figure, first is simple rectangle and second figure indicates rounded rectangle. For drawing rounded rectangle we have to set two more properties  RadiusX and RadiusY for round corner.



Ellipse

This shape create ellipse to circle in specified width and height.
 


<Ellipse Height="100" Width="300" StrokeThickness="0.4" Opacity="0.5" Stroke="Black"  Fill="Gold"/>



Polyline

This shape is used draw a series of connected strait line.



  
<Polyline Points="0,50 20,0 40,50 60,0 80,50 100,0 120,50 -4,50"
                      Stroke="Black"
                      StrokeThickness="10"
                      Canvas.Left="75"
                      Canvas.Top="50" />

Polygon

This shape is used for draw a polygon, which is a connected series of lines that form a closed shape.
Following example shows how to creates different types of shapes using polygon

Example  :


  <Polygon Points="100,0 75,75 100,100 125,75"
                         Stroke="Black" StrokeThickness="2" Fill="AliceBlue"/>
        <Polygon Points="100,100 125,125 100,200 75,125"
                         Stroke="Yellow" StrokeThickness="2" Fill="Black"/>
        <Polygon Points="100,100 125,75 200,100 125,125"
                         Stroke="Red" StrokeThickness="2" Fill="Azure"/>
        <Polygon Points="100,100 75,125 0,100 75,75"
                         Stroke="Blue" StrokeThickness="2" Fill="BlanchedAlmond"/>




Custom shape using Path

Now I will tell you how to create custom shapes using Path and Data property of Path element.

Path class is used to draw Curves and complex shapes. it describes using Geometry object. There are a various types of Geometry objects describes shapes are LineGeometry, RectangleGeometry, and EllipseGeometry.

Example 1: Draw Triangle



<Path Data="M50.0000011012083,0.5L99.5000021617096,99.5L0.500000040707015,99.5z" Stroke="Black"
     StrokeThickness="4" Fill="Yellow"/>


Example 2: Draw Curve





<Path Data="M25,0.5C38.5309753417969,0.5,49.5,9.23044681549072,49.5,20" Stroke="Black"
     StrokeThickness="5" Fill="Yellow" Margin="0,33,0,0" />



In the above example we gave path data from Microsoft expression blend.



Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------







Tuesday, 19 February 2013

Properties in Silverlight


Properties in Silverlight

Hi friends,

In this article i am explaingin properties in silverlight.

There are two ways to reference properties in XAML: in line with the element as
you would any XML attribute and as a nested subelement. Which you should
choose depends on what you need to represent. Simple values are typically represented
with inline properties, whereas complex values are typically represented with
element properties.


1. INLINE PROPERTIES

The use of an inline property requires a type converter that will convert the string representation—
for example, the "Black" in Background="Black"—into a correct
underlying .NET type (in this case, a SolidColorBrush).

Ex
<Grid x:Name="LayoutRoot" Background="Black" />



2.ELEMENT PROPERTIES

Another way to specify properties is to use the expanded property element syntax.
While this can generally be used for any property, it’s typically required only when you
need to specify something more complex than the inline syntax will easily allow. The
syntax for element properties is <Type.PropertyName>value</Type.PropertyName>,


<Grid x:Name="LayoutRoot">
<Grid.Background>
Black
</Grid.Background>
</Grid>


3.Dependency properties

Storing a dependency property differs in that the location
of its backing value depends upon its current state. The way that location is determined
is called value precedence.
VALUE PRECEDENCE
Dependency properties obtain their value from a variety of inputs. What follows is the
order the Silverlight property system uses when assigning the runtime values of
dependency properties, with the highest precedence listed first:


4 .Attached properties
Attached properties are a specialized type of dependency property that is immediately
recognizable in markup due to the TypeName.AttachedPropertyName syntax. For example,
Canvas.Left is an attached property defined by the Canvas type. What makes
attached properties interesting is that they’re not defined by the type you use them
with; instead, they’re defined by another type in a potentially different class hierarchy.


PROPERTY PATHS
Before we wrap up our discussion of properties, there’s one concept left to understand:
property paths. Property paths provide a way to reference properties of objects in
XAML both when you have a name for an element and when you need to indirectly
refer to an element by its position in the tree



Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

Monday, 21 January 2013

Tricky Asp.net interview Question

 Hi friends,

In the last month I had given one interview in one of the large MNC in Pune.
In that interview, one interviewer asked me one questions about Try Catch block in asp.net.
I liked that questions and really it’s challenging question.

Scenario 1 :-

Please find following Code and trace the output


Try{
Response,redirect("1.aspx");
}

Catch{
Response,redirect("2.aspx");
}

finally{
Response,redirect("3.aspx");
}

Answer:

The Output of this one is that it will redirect the page to 3.aspx as on try block this will throw a redirect error but after catch block the control will go to the finally block which will redirect the page to 3.aspx..





Scenario 2

Trace the output



Try
{
Server.Transfer("1.aspx");
}

Catch
{
Server.Transfer("2.aspx");
}

finally
{
Server.Transfer("3.aspx");
}

Answer:

In the Second case we are using Server.Transfer which will directly transfer the current page to show the contents of the page to be redirected keeping the url address as the same.. IN this case it will move through all three blocks and would show the contents of 1.aspx, 2.aspx and 3.aspx from the main page... ie., if we are transferring from main.aspx then it will show the contents of 1.aspx plus the content of 2.aspx and the content of 3.aspx in main.aspx..





Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------


Wednesday, 9 January 2013

Advantages for consuming RESTful services



1.     What is Rest

REST service is based on HTTP protocol. Each method invocation is a http get,post,delete or put request. Since it is HTTP protocol based so anything that can talk http can consume your service without much effort i.e. javascript, C#, Java, Whatever.
Also REST call results can be cached like normal http pages (by intermediate proxies or client machine) if you send the right caching parameters with the response.
However it is also more oriented towards 'resources' while normal WCF service is oriented towards RPC style communication.
Normal WCF supports callbacks and whole lot of other things that REST doesn't support but obviously it comes with cost of platform compatibility and complexity.

2.     What is Advantages of consuming RESTful services

  1. ·         REST is powerful, intuitive, and relatively easy to implement.
  2. ·         Light weight – not lot of extra xml markup as in SOAP.
  3. ·         Unlike SOAP it provides much simpler result that can be human readable.
  4. ·         It is very easy to build no extra toolkits are required.
  5. ·         Improve scalability of the application through caching and session state.
  6. ·         Custom URIs using URI templates
  7. ·         Consistency with design of the World Wide Web
  8. ·         Lightweight - not a lot of extra xml markup
  9. ·         Human Readable Results
  10. ·         Easy to build - no toolkits required
  11. ·         Each operation in a service is uniquely identified.
  12. ·         Each operation has their, own unique URI and can be accessed via this URL all across the web. 
  13. ·         Each URL is an object representation.
  14. ·         The Object can retreived using HTTP GET.
  15. ·         The Object can be deleted using a POST or PUT, use DELETE to modify the object
  16. ·         It is firewall friendly and it is fairly simple and straight forward.



3.     More About RESTful services

  1.  The Restful web services are nothing but a web programming model to expose WCF Service.
  2.  REST can have more impact on data transfer reduction in combination with JSON or other serialization techniques.
  3. REST stands for Representation State Transfer. Representation State Transfer term is invented by one of the original author of the HTTP protocol, Roy Fielding in his 2000 doctoral dissertation.  Visit the wiki  for more about REST.



4.     KEY CONCEPTS OF REST

  1. Resources: Resources are nothing but entities in a business domain for ex.  Items, Vendors, Customer etc…
  2. URIs: All resources are referenced using URIs (Universal Resource Identifiers)
  3. Representations: It consist of 2 things Data that reflects current or desired state, meta   data that provides description of data values. For ex. HTML, XML, JSON, Text files, zip, Images, Mpegs etc…


5.     Example


                [OperationContract(Name = " AddTwoNumber ")]
        [WebInvoke(UriTemplate = "/", Method = "POST")]
         int AddTwoNumber(int i, int j);

       

        [OperationContract(Name = " SubTwoNumber")]
        [WebGet(UriTemplate = "/")]
        int SubTwoNumber(int i, int j);


Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------