Monday, August 31, 2009

test post from email

In Silverlight and WPF, when working with UserControls and Behaviors, you may find yourself in situations where you need to call a method or access a property that is stored in the parent. This parent can be anything - it could be a container or it could be another UserControl. Accessing what you want in those locations is not as straightforward as you may imagine it to be.

To describe this scenario a bit more, let's look at the following diagram where I am simplifying to a world where the parents and children are just Window, UserControl, or Behavior types:

In this short tutorial, I will describe a few things:

  1. How to access the contents of your application's root Page or Window regardless of where you are.

  2. How to access the contents of a parent user control such as the Yellow user control calling something in the Blue user control.

  3. How Behaviors are a bit different.

The syntax for both WPF and Silverlight can be a little different, so if there are any differences, I will provide both versions for the scenarios I list above.

Accessing the Root
The most common type of cross-usercontrol communication you would engage in is one where you are trying access the root of your application. The root of your application in Silverlight is of type UserControl, and the root of your application in WPF is of type Window. This distinction is important because it results in a varying syntax.

The syntax for accessing the root UserControl in Silverlight is:

UserControl rootPage = Application.Current.RootVisual as UserControl;

The syntax for accessing the root Window in WPF is:

Window rootWindow = Application.Current.MainWindow as Window;

The above code will hook into your root and provide you with access to any types that are available to UserControl and Window.

So now, you get access to the root element, but you probably don't want this particular approach. The problem is that casting to both UserControl and Window is very generic, and it contains no references to anything you may have done. Any elements you added in XAML or any properties and methods you added in your code-behind file are hidden from view.

The reason is actually pretty simple. The root of your application is not actually UserControl or Window. It is something derived directly from UserControl or Window. To fix this, you need to cast the returned value to the actual type of your root element. By default, in Expression Blend, the type of your root usercontrol is MainPage in Silverlight:

 

In WPF, the type of your root window is MainWindow:

This means, you will need to make just some minor tweaks to your code. For Silverlight, your code now becomes:

MainPage rootPage = Application.Current.RootVisual as MainPage;

For WPF, your code is:

MainWindow rootWindow = Application.Current.MainWindow as MainWindow;

While MainPage and MainWindow are new types, they are directly derived from UserControl and Window respectively. This means that any properties or methods you would expect to see either in UserControl or Window are still accessible to you. In your own projects, if you are not using Expression Blend, be sure to set the type of the root element appropriately because you cannot assume that MainWindow and MainPage will actually exist.

Thursday, October 16, 2008

Use class with addEvenListener

We used delegate or Proxy in actionscript 2.0 when we need to parse parameter to a function click or other event with button.

Now in Actionscipt 3.0 how can we do with this issues?
Ans:We will assign a new class to addEventListener and parse parameter into class method.

the Example like this.
1. Create new as file and save its name buttonEvent.as
2. Write this code
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class buttonEvent extends Sprite
{
var val:String;
public function buttonEvent()
{


}

public var setParam(param:String)
{
val=param;
}
public function clickFn(m:MouseEvent):void
{
//the code When click on this button
trace(val);//Hello All Flash BadAss!
}
public function overFn(m:MouseEvent):void
{
//the code When Roll Over on this button
}
public function outFn(m:MouseEvent):void
{
//the code When Roll Out
}
}
}


3. on main time line add tis code
import buttonEvent;
var bt:buttonEvent = new buttonEvent();
var myBt:Button = new Button();
myBt.addEventListener(MouseEvent.CLICK,bt.clickFn);
myBt.addEventListener(MouseEvent.MOUSE_OVER,bt.overFn);
myBt.addEventListener(MouseEvent.MOUSE_OUT
,bt.outFn);
//and then assign any value that we need to use with the button
bt.setParam("Hello All Flash BadAss!");

Good luck!

Hudsadin keox

Saturday, September 13, 2008

How to call stage from class!

This topic will talk shortly about the stage class. When we create our new class and need to access to stage class (Main timeline) example "stage.stageWidth"we will see the error "cannot access to null property" or some thing else -*-.I use some easy tip to access to stage class .

code!
package
{
import flash.display.Stage;
public class flashBadass
{
private var __stage:Stage;
public functon flashBadass(st:Stage)
{
__stage=st;
trace(__stage.stageWidth);///easyly but for newbies you will headache with this problem.
}
}
}

On timeline when you create new instace of this class you must pass stage to the method.
import flashBadass;
var flBadass:flashBadass = new flashBadass(stage);

I dont know if you had other way to do please tell me.*-*

Hudsadin keox