Saturday, March 31, 2007

How To Create A Full Trust XBAP Application In WPF.

I recently faced some problem while creating XBAP application in WPF like trying to develop application which is contain IO file accessing from the browser application. As you may know, WPF Web Browser applications run in a sandbox, which allows them to be installed without any security prompts. However, there are cases, such as intranet applications, where allows an in-browser application to do more makes sense. which I thought I'd share to this with you people.

The Following Steps helps to produce Full trust XBAP Application.

Step 1:

Change the TargetZone of the appliction to custom. This can be done manually by hacking the .proj file or can be done in Visual Studio by going to Project Properties, going to the Security tab, and changing the dropdown to (custom).

Step 2:

Add the Unrestricted="true" attribute to the manifest of the appliction, which is located in the Properties folder. The manifest should look something like this.

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true"/>

Step 3:

Deploy the application using the Visual Studio Publish Wizard.

Step 4:

If you want to deploy this from the web, the certificate used to sign the manifest will have to be added to the Trusted Publishers store within Internet Explorer. By default, VS generates a .pfx key when you create a Web Browser application. Of course, real deployments, a company issued certificate would be more appropriate to use. Getting a cert into the store can be done by double clicking the cert or by using the certmgr tool with the appropriate commandline switches.

For some more Idea You may try with this ...

Enjoy !!!!! If You couldn't get ,please leave the comment about what you want exactly and what type of exception you are getting. It will try to give solution for your problem ..

Friday, March 30, 2007

Routed Events

Routed Events(This is another intresting concept in WPF)

Raising and handling events with the WPF is based on the new XAML markup structures, and how events are routed through a tree of elements. When a XAML page contains nested controls, there are interactive complexities that never before existed in the typical .NET UI environment.‘Nested’ controls create what is known as a Logical Tree and a Visual Tree.

Application developers have been required in times past to‘Walk-the-Tree’ by raising events and/or calling methods for each logical ‘node’ within a nested hierarchy.Events in the WPF are called Routed because there is a brand new chronological process by which to handle these events. This ordered, or routed, list of events will fire in a controlled fashion, providing full view of event the most complex Visual Trees. With the advent of Routed Events in the WPF.

The complications involved with capturing a series of hierarchical events are encapsulated and simplified. Routed Events are a pipeline of event handlers that are automatically fired for a control when any of its nested controls’ events fire. This means that it performs as a self-aware component, knowing when its members are being interacted with, and providing handler logic to respond to user or system related interactions.

A FrameworkElement could possible contain other sibling and child elements, which forms a tree of elements. In the WPF, the parent element can provide information to child elements, providing usage, customization, and visibility to the potentially smaller, nested objects. "Control Composition" is a term used to describe the creation or design concepts used when creating controls and providing thought to the handling of information within the tree. It leads developers to creating better structure for controls to control and direct the logical flow of events for the tree and its members.

Types of RoutedEvents
  1. Tunneling
  2. Bubbling
  3. Direct
A Bubbling event is the first place we will start, as it provides event handling from the originating element of a Visual Tree to the root of the tree. Tunneling event, on the other hand, fire in the opposite direction and are used to hook into the pre-condition of the controls within the visual tree from the top of the visual tree down. Tunneling and Bubbling event models assist in the complicated logic associated to events raised from / by controls within other controls. Developers can hook into an action before or after it occurs in context to its parent and child controls. Direct Event is typical .Net event handler concept.

Lets we will talk about RoutedEvent in detailed manner.

In WPF application contain many elements. You can see these elements in elements tree.(Such as visual tree). The routed event model enables you to use the element tree . This routed event will route along a route after its raised .It can travel in one of two directions. Event Listeners invoke the handlers from the root element to all successive child elements along the source element , this is called tunneling . And Event Listeners invoke the handlers from the source element to all successive parent elements along the Root element , this is called Bubbling Event.

Syntax For Routed Event

public static readonly RoutedEvent eventNameEvent;

Here event name followed by the word Event(Ex: TaskEvent)

The RoutedEvent instance obtained from registration is retained as a public static readonly field member of the class EventManager.ResgisterRoutedEvent.

Initializing Routed Event.


Note : Don't contain Event word with eventName(It should be like : Task)



Add and Remove Implementations(Initializing the Actual Event (language-specific))
we should define the actual event (ex : Task) , here the value argument is the event handler.

public event RoutedEventHandler Task
AddHandler(TaskEvent, value);

RemoveHandler(TaskEvent, value);

Use Of RoutedEvent

Group of controls together that should all interact. The parent element is a common listener for the routed event, and uses the same event handler whenever any of the controls raises a particular event.

Example :

In this example no need to write event handler for all the buttons , by using RoutedEvent can place a common Event Handler on the Parent of two buttons. (Here in windows).

Attaching and Implementing an Event Handler for a Routed Event

Attaching a handler for a routed event in an application that is created in code is also straightforward. Routed events almost always have background implementations of add and remove logic that allow the handlers to be added by a language-specific event syntaxRouted event handlers can also be attached through a helper method AddHandler.

Example of the helper method

Button b2 = new Button();
b2.Click += new RoutedEventHandler(myEvent);

void myEvent(object sender, RoutedEventArgs e)

Sample For Creating Custom RoutedEvent


class MyButton :Button
public static readonly RoutedEvent TaskEvent = EventManager.RegisterRoutedEvent("Task", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButton));
// Provide CLR accessors for the event
public event RoutedEventHandler Task
add { AddHandler(TaskEvent, value); }
remove { RemoveHandler(TaskEvent, value); }
// This method raises the Task event
void RaiseTaskEvent()
RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButton.TaskEvent);
//Raise the event when the MyButton is clicked
protected override void OnClick()


public partial class Window1 : System.Windows.Window
public Window1()
// Creating Our Own Control for custom Routed Event
MyButton m=new MyButton();
m.Content ="Click Me";
m.Width =200;
m.Height =30;
this.Content = m;
m.Task += new RoutedEventHandler(m_Task);
void m_Task(object sender, RoutedEventArgs e)
MessageBox.Show("Hai Iam The Routed Event For Your Button");
// dont want to route others
e.Handled = true;
//throw new Exception("The method or operation is not implemented.");

Note : Here Task event will raise when you click on the mybutton. and it will show message box with message.

Key Points

For events that should be routed events

  • Declare a public static readonly field to hold your RoutedEvent object
  • Declare CLR accessors for these routed events
  • Declare a protected virtual void On( EventArgs e) method for subclasses to use. Call this method from your “class handler” -- EventManager.RegisterClassHandler(typeof(YourComponent), YourClassHandler).
  • Routed events using RoutingStrategy.Tunnel should have names that begin with “Preview”.
  • Many bubbling events don’t have corresponding tunnelling events, but almost all tunnelling events should have corresponding bubbling events.
  • A tunnelling event should be raised before its corresponding bubbling event. If the tunnelling event was marked as handled, the bubbling event should be marked as handled before the event is raised.

Still If You Cant Get, Try with this ....


Thursday, March 22, 2007

Layouts In WPF

Windows Presentation Foundation has dynamic layout (also known as automatic layout)
panel themselves are responsible for sizing ans positioning elements based on different layout models.

Type of Layout :(every layout derived from Panel Class)

  1. DockPanel
  2. Grid
  3. StackPanel
  4. UniformGrid
  5. WrapPanel

Panel defines a property names Children used to store the child elements. Children object can be UICollection. Like image,shape,textblock and control objects.


Its used to arrange the controls vertically or horizontal stack.


<Button> </Button>
<TextBlock> < /TextBlock>


StackPanel s=new StackPanel();
Button b=new Button();
TextBlock t=new TextBlock();

Orientation Property

We can specify the children orientation using Orientation property of StackPanel. s.Orientation=Orientation.Horizontal;

Vertical and Horizontal Alignment

s.HorizontalAlignment=HorizontalAlignment.Horizontal (or) s.VerticalAlignment=VerticalAlignment.Vertical


WrapPanel is similar to the StackPanel but in the WrapPanel Child elements can wrap to the next column or row automatically if there is not enough room. WrapPanel is used to add unknown number of controls like Non-Detail view in WindowsExplorer. And also we can set ItemHeight and ItemWidth properties that we can use to enforce uniform height or width, Orientation Property is same as StackPanel.

WrapPanel s=new WrapPanel();
Button b=new Button();
TextBlock t=new TextBlock();


use of the dockpanel is automates the positioning of elements against the inside edges of their parents. We can create dockpanel in C# Like Follows.

DockPanel d=new DockPanel();

And we can set dockpanel object into the window content property.

now window layout begin with dockpanel and then we can add other type panel or control like that . Adding children in dock panel is same syntax as with other panels.


But with that we need to specify on which side of the DockPanel you want control docked.
Example if you want dock the control on right side of panel you need to specify like this .

SetDock is a static method of DockPanel Class. Argument should be control and dock style.

Dock Enumeration


Note :
SetDock Method call may come before or after adding the control into the panel.

Another Method for setting Dock for the control

Using attached property concept like follows.

The last control is not docked at all but occupies the leftover interior space. This behavior is by default true setting of the DockPanel property LastChildFill. If we set LastChildFill=false means the last control also docked. And leftover space is unfilled .


its used to hosts the children in a grid of rows and columns. It has property named ShowGridLines. Its used to display the grid lines. GridLenth() is used to specify the Grid Height and Width. Constructor will take two arguments – one is value and another one is GridUnitType.


GridLenth glen=new GridLength(100,GridUnitType.Pixel);
Type Of GriUnit :


Grid g=new Grid();
RowDefinition rdef=new RowDefinition();
RowDefinition rdef1=new RowDefinition();
ColumnDefinition cdef=new ColumnDefinition();
ColumnDefinition cdef1=new ColumnDefinition();
Adding Control into the Grid.
Adding RowSpan and ColumnSpan


its similar to the Grid . Except that all the rows and columns are equal Height and Width.