Sunday, April 29, 2007

What is the future of WPF and XAML over other technologies and how may I benefitted from using these two?.

User interfaces are an important part of most Windows applications. No matter how much software evolves, traditional menu-driven GUIs are here to stay for some more time. Similarly, the need to display video, run animations, use 2/3D graphics, and work with different document formats also cannot be superseded. And all of this must be possible whether the application is a stand-alone desktop client or is accessed through a Web browser.

So far, all of these aspects of the user interface have been provided in different ways on Windows. For example, a developer needs to use Windows Forms to build a Windows GUI, or HTML/ASPX/Applets/JavaScript etc. to build a web interface, Windows Media Player or software such as Adobe's Flash Player for displaying video etc. The challenge for developers is certainly clear: building a coherent user interface for different kinds of clients using diverse technologies isn't a simple job.

A primary goal of WPF (originally called Avalon) is to address this challenge! By offering a consistent platform for these entire user interface aspects, WPF makes life simpler for developers. By taking a more modern approach, including support for video, animation, 2/3D graphics, and various kinds of documents, WPF can let users work with information in new ways. And by providing a common foundation for desktop clients and browser clients, WPF makes it easier to build applications that address both.

Another challenge that has long faced the creators of user interfaces stems from the different roles required for building effective interfaces. Software developers are needed to create the logic behind the interface, and Designers are required to define the interface's look and feel. Yet older technologies such as Windows Forms are focused entirely on the developer. There's no truly effective way for developers and designers to collaborate. To address this issue, WPF relies on the eXtensible Application Markup Language (XAML). An XML-based language, XAML allows specifying a user interface declaratively rather than in code. This makes it much easier for user interface design tools like MS Expression Blend (originally branded as MS Expression Interactive Designer and code named as Sparkle) to generate and work with an interface specification based on the visual representation created by a designer. Designers will be able to use such tools to create the look of an interface and then have a XAML definition of that interface generated for them. The developer imports this definition into Visual Studio, then creates the logic the interface requires.

Developers can also build a XAML browser application (XBAP) to create a remote client that runs inside a Web browser. Built on the same foundation as a stand-alone WPF application, an XBAP allows presenting the same style of user interface within a downloadable browser application. The best part is that the same code can potentially be used for both kinds of applications, which means that developers no longer need different skill sets for desktop and browser clients. The downloaded XBAP from the Internet runs in a secure sandbox (like Java applets), and thus it limits what the downloaded application can do.

Burp, GUI is a complex but an important part of the modern applications. Through WPF, the .NET Framework 3.0 presents a more complete and consistent solution to the challenges these interfaces present. The goal is to let people who create user interfaces (both developers and designers) effectively collaborate and do their jobs more expeditiously.

From the above information You could come to the conclution for below question's.
  • What is WPF and XAML?
  • What is the future of WPF and XAML over other technologies?.
  • How may I benefitted from using these two?. How it differs from others?.
  • Will it be a consistent one( For this you might get more idea from video module which is available in my last post(What Is WPF And It's Futures?).

I Hope everyone might came to the conclusion about those questions. if you want more about this topic please feel free to ask and i will post ASAP as per your requirement.

Please leave a comment and I'll cover them in my next post.
Enjoy!!!!!!!!!!!!!!!!!!!!!!!

Thursday, April 26, 2007

What Is WPF And It's Futures?

Hi , here I am not going to explain about this topic.Directly WPF program manager going to explain about this topic. For understanding this just you need to spend 50 minutes in online because This is the video clip that shows demo about following futures. You could select topic directly from given combo box while playing demo application.

Demo application that shows off
  • Controls (rich functionality, rich content model, rich styling)
  • Interop with WinForms Controls
  • Layout (Panels)
  • Data Binding (DataTemplates)
  • Vector graphics
  • Text (this demo is commented out due to font licensing issues)
  • Reading (FlowDocument)
  • Save a FlowDocument to XPS
  • Media (Video)
  • 3d
  • Animation throughout

Here is the link and get idea and have a fun with WPF.

http://sessions.mix06.com/view.asp?sessionChoice=2001&disc=&pid=NGW030&yearChoice=2005

Still If you have a doubt please let me know i will try to give....

Enjoy!!!! !!!!!!!!!!!!!! Have a nice day ...

Wednesday, April 25, 2007

DataBinding In WPF

Today I will discuss about DataBinding that I have faced in my experience. please leave a comment and I'll cover them in my next post.

DataBinding is the technique of connecting controls and elements to the data. In the past, a programmer would write code both to initialize control from the variable and to set the variable into controls . In modern programing environments , the programmer defines a binding the control and variable , the binding automatically performs both the jobs.

Very often a data binding can replace an event handler, and simplifying the code. DataBinding are considered to have Source and Target. Here source is Data and Target is the Control and may be two controls will participate.

The bound property of the source need not be a Dependency property , but target property must be derived from the dependency object.

How can we bind data using {Binding} concept?
we can bind data between two elements or from resource collection. The following format is for bind with resource collections.

<!-- Resource Area-->
<Window.Resources>
<system:String x:Key="myvar">Hi , This is Binding </system:String>
</Window.Resources>

<!-- Bind With resource -->
//Method 1 (this method will bind whole object)
<TextBlock TextContent="{StaticResource myvar}" />

//Method 2 (this will bind particular property of object). it will display lenth property value of string .
<TextBlock Content="{Binding Source={StaticResource myvar}, Path=Length}" />

For Bind With Another Elements

<TextBlock Content="{StaticBinding Path=Length, ElementName=MyBox}" />
<TextBox Name="MyBox" />

Here textblock getting changes its content while typing the content on textbox.


Binding Modes

You can specify the mode property is separated from the path using comma.


Type of Mode

  1. OneWay -From the picture label get the value from source textbox. But it wont affect textbox value while the change happens in the label.

  2. TwoWay - From the picture Label get the from the Source textbox and also textbox will get the reflection while the change happens in the label.

  3. OneTime - From the picture Label initialized from the source textbox but does not track changes .

  4. OneWayToSource -if target property is not packed by a dependency property while source is . In that case we can use this mode.

DataContext


DataContext is inherited through the element tree , so if you set it for one element it is also applies to all the children of that element. For example if we set the binding in stackpanel. We can bound properties of all the control under stackpanel.

Thursday, April 19, 2007

Freezable Object

A Freezable is a special type of object that has two states: unfrozen and frozen. When unfrozen, a Freezable appears to behave like any other object. When frozen, a Freezable can no longer be modified.

Freezable having Changed event to notify any changes in the object . Freezable can improve performance because it wont take more time to spend resources on changes. A Frozen Freezable can also be shared across threads. But unfrozen cant.Although the Freezable class has many applications, most Freezable objects in Windows Presentation Foundation (WPF) are related to the graphics sub-system.

Examples of types that inherit from Freezable include the Brush, Transform, and Geometry classes. Because they contain unmanaged resources, the system must monitor these objects for modifications, and then update their corresponding unmanaged resources when there is a change to the original object. Even if you don't actually modify a graphics system object, the system must still spend some of its resources monitoring the object, in case you do change it.

For example, suppose you create a SolidColorBrush brush and use it to paint the background of a button.

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;

while painting the button on the screen , the SolidColorBrush doesn't actually do the painting , the Graphics System generates fast. low-level objects for the button and the brush, and it is those objects that actually appear on the screen. If you were to modify the brush, those low-level objects would have to be regenerated. The freezable class is what gives a brush the ability to find its corresponding generated, low-level objects and to update them when it changes. When this ability is enabled, the brush is said to be "unfrozen."

A freezable's Freeze method enables you to disable this self-updating ability. You can use this method to make the brush become "frozen," or unmodifiable.Here we are checking the object can freeze or not. Then if can means we can freeze using Freeze() method.

SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}

Not every Freezable object can be frozen. To avoid throwing an InvalidOperationException, check the value of the Freezable object's CanFreeze property to determine whether it can be frozen before attempting to freeze it.

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;
// Changes the button's background to red.
myBrush.Color = Colors.Red;

This sample first set the yellow color to Button , then will get change into the Red. after when get next page refresh. But it happening immediately after rendering the button.

Freezing a Freezable

To make a Freezable unmodifiable, you call its Freeze method. When you freeze an object that contains freezable objects, those objects are frozen as well. For example, if you freeze a PathGeometry, the figures and segments it contains would be frozen too. A Freezable can't be frozen if any of the following are true.

It has animated or data bound properties. It has properties set by a dynamic resource. (See the Resources Overview for more information about dynamic resources.) It contains Freezable sub-objects that can't be frozen.

Freezing from Markup

To freeze a Freezable object declared in markup, you use the PresentationOptions:Freeze attribute. In the following example, a SolidColorBrush is declared as a page resource and frozen. It is then used to set the background of a button.

XAML

<Page xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:PresentationOptions=http://schemas.microsoft.com/winfx/2006/xaml/presentation/options xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable="PresentationOptions">
<Page.Resources>
<!-- This resource is frozen. -->
<SolidColorBrush x:Key="MyBrush" PresentationOptions:Freeze="True" Color="Red" />
</Page.Resources>
<StackPanel>
<Button Content="A Button" Background="{StaticResource MyBrush}">
</Button>
</StackPanel>
</Page>

To use the Freeze attribute, you must map to the presentation options namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation/options PresentationOptions is the recommended prefix for mapping this namespace:

XAML
xmlns:PresentationOptions=http://schemas.microsoft.com/winfx/2006/xaml/presentation/options

Because not all XAML readers recognize this attribute, it's recommended that you use the mc:Ignorable Attribute to mark the Presentation:Freeze attribute as ignorable:

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

"Unfreezing" a Freezable

Once frozen, a Freezable can never be modified or unfrozen; however, you can create an unfrozen clone using the Clone or CloneCurrentValue method.

Creating Your Own Freezable Class

A class that derives from Freezable gains the following features.
  • Special states: a read-only (frozen) and a writable state.
  • Thread safety: a frozen Freezable can be shared across threads.
  • Detailed change notification: Unlike other DependencyObjects, Freezable objects provide change notifications when sub-property values change.
  • Easy cloning: the Freezable class has already implemented several methods that produce deep clones.

A Freezable is a type of DependencyObject, and therefore uses the dependency property system. Your class properties don't have to be dependency properties, but using dependency properties will reduce the amount of code you have to write, because the Freezable class was designed with dependency properties in mind. For more information about the dependency property system, see the Dependency Properties Overview which is posted by me.

Every Freezable subclass must override the CreateInstanceCore method. If your class uses dependency properties for all its data, you're finished.
If your class contains non-dependency property data members, you must also override the following methods:

  1. CloneCore
  2. CloneCurrentValueCore
  3. GetAsFrozenCoree
  4. GetCurrentValueAsFrozenCore
  5. FreezeCore

Enjoy!!!!!!!!!

Wednesday, April 18, 2007

Attached Properties

Attached properties are a special form of dependency property. a child element can store a value associated with a property defined on an ancestor element. This is commonly used in the interaction between elements and the WPF layout infrastructure, such as an element informing a DockPanel that it should be docked to a particular side of the panel.

Reason for Attached properties:

When dealing with out Layout system, we want to provide the ability for anybody to build new Panels. Inside of those panels you should be able to put normal elements:

<my:RobPanel>
<Button />
<Button />
</my:RobPanel>

Often, the new panel will need to define a property that the user can place on the children of that panel to control how the panel arranges the children.

<my:RobPanel>
<Button RotationsPerMinute="60" />
<Button RotationsPerMinute="3"/>
</my:RobPanel>

Unfortunately that won't work. RotationsPerMinute is a newly invented property. The Button class shipped before the RobPanel was ever built and the Button class designers forgot to include this important property!

So WPF has the concept of attached properties. One class defines the property. You attach it to instances of other objects.

<my:RobPanel>
<Button Name="b1" my:RobPanel.RotationsPerMinute="60" />
<Button Name="b2" my:RobPanel.RotationsPerMinute="3" />
</my:RobPanel>

In C#.
RobPanel.SetRotatationsPerMinute(b1, 30);
int currentRotationsPerMinute = RobPanel.GetRotationsPerMinute(b1);

So if you buy the need for this extensibility, then you need to ask why is Canvas.Top and Canvas.Left modeled as an attached property, but Height and Width are modeled as normal properties.

Top and Left are modeled as attached properties because they are only respected inside of a Canvas. If I set Top or Left on a Button inside a StackPanel or DockPanel, it would have no effect.

Lets we will see an example for attached properties !

In this example i am creating new control named as MyControl with derived from Button. In which i am creating one attached property named as MyAttack.The attached property registration is like dependency property registration and for setting value we need to implement method with signature of SetPropertyName. This method should set the value for attached property using Dependencyobject.SetValue(value) method.

Code For UserControl

public class MyControl : Button
{
public static readonly DependencyProperty MyAttackProperty = DependencyProperty.RegisterAttached("MyAttack", typeof(bool), typeof(MyControl),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyAttackInvalidated)));
public MyControl()
{
}
public static void SetMyAttack(DependencyObject dependencyObject, bool enabled)
{
dependencyObject.SetValue(MyAttackProperty, enabled);
}
private static void OnMyAttackInvalidated(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
Button b = dependencyObject as Button;
if (b != null)
{
if ((bool)e.NewValue)
{
MyControl m = new MyControl();
m.CallClick();
}
}
}
public void CallClick()
{
MessageBox.Show("Clicked");
}
}

For Accessing Attached Property :

We can simply call the created attached property using classname.propertyname from any control attributes area. Like follow's.

<Window x:Class="AttachedProp.Window1" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=
http://schemas.microsoft.com/winfx/2006/xaml xmlns:mn="clr-namespace:AttachedProp" Title="AttachedProp" Height="300" Width="300" >
<Grid>
<Button mn:MyControl.MyAttack="true" >Click</Button>
</Grid>
</Window>

Output of this example should display messagebox with "Clicked" message after loading the button in window.

Enjoy!!!!!!

Dependency Properties

I have faced some interesting concept in WPF ,That one is Dependency Properties ,Its really sound cool.But it takes full day for me to get the complete idea. First I have tried to get the idea from some books.but I couldn't , Then I have visited some sites about DP. There i got some partial idea. Then again Book with couple of samples, Finally I got..

Its really interesting concepts in WPF.

The concept of dependency properties(what i have understood):


WPF contain window Element Tree. It consists of all the visual objects created by the program .For example iam creating 3 buttons and put them into the Grid ,then Grid into the Window. Here Window is the parent node ,grid is child node of window and buttons are child of Grid.

Visual Tree (Not a Logical Tree because Logical Tree contain all the elements like GridRowDefenitions , GridColDefenitions and Run object associated with text Block,etc..). Obviously this is not a complete visual tree because in visual tree may include visual objects that the program does not explicitly create. (E
x. Border,Background,etc..)


Here we have to take FontSize as a Dependency Property.

In WPF, if I try to change the FontSize of the window , It will change all the child elements fontsize automatically until the child element fontsize changed by explicitly, If we changed the fontsize of child element by explicitly it won't affect the original fontsize of that particular child element. But rest of the elements will get affect. This is called Dependency Properties.The interesting aspects is, In this visual tree Grid don't have a font size property even though the buttons font size will get changes,

Using this concepts we can create our own dependency properties for our custom controls. But our control class must be inherited by FrameWorkElement or UIElement class. Here fontsize isn't the only property that works like this. Allow-Drop ,IsEnabled,IsVisible,etc.. These are all UIElement class properties.


If you got! enjoy and leave your comments here!!!!!!!!!!!!!!!!!!!