Close popup on hardwarebutton backpressed in windows phone 8.1 application - windows-phone-8.1

I have a popup that comes on a click event of button..
But I am unable to close the popup..I want to close popup on hardwarebutton_backpress event..
Can anyone plzz tell me how to do so..
I am using this code to show popup
Public void showpopup_click()
{
Popup p =new Popup();
p.child= new Mypopup();
p.Isopen=true;
}
Here Mypopup is my user control..
I am using visual studio and developing a windows phone 8.1 app..

Another approach, in my opinion better, is using the IsLightDismissEnabled property where you don't need to check whether a popup is already open or not, or if you have multiple open popups, everything is handled automatically.
var popup = new Popup();
popup.IsLightDismissEnabled = true;
popup.IsOpen = true;

You have to subscribe to the Windows.Phone.UI.Input.HardwareButtons.BackPressed event to handle the back button with custom logic. If you store the Popup in a field, you can easily set IsOpen property to false.
Like this:
private Popup _popup;
public void showpopup_click()
{
_popup = new Popup();
_popup.Child = new Mypopup();
_popup.IsOpen = true;
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs args)
{
_popup.IsOpen = false;
args.Handled = true;
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}
In the event handler, you need to set the args.Handled property to true to prevent the application close on the back button pressed. You also need to unsubcrise from the event to avoid memory leak.

Related

Set object's `visible` to `true` when a `tap` event is performed then set it back to `false`

I have an object as MovieClip and I have a button as Button on my flash timeline.
When the button is tapped, I want to set the object.visible to true then when the button is not tapped, I want to set it back to false.
How can I do that?
I have tried this code, but it won't works as I want. I only can show the object but cannot hide it back.
button1.addEventListener(TouchEvent.TOUCH_TAP, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
stage.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I think this code could work.
button1.addEventListener(TouchEvent.TOUCH_BEGIN, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
button1.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
button1.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I changed
1: TouchEvent.TOUCH_TAP to TouchEvent.TOUCH_BEGIN
2: stage.addEventListener to button1.addEventListener
Before saying anything about your problem, let's take a look on the definitions of the TouchEvent.TOUCH_BEGIN, TouchEvent.TOUCH_END and TouchEvent.TOUCH_TAP events :
The TouchEvent.TOUCH_BEGIN is :
Dispatched when the user first contacts a touch-enabled device ...
The TouchEvent.TOUCH_END is :
Dispatched when the user removes contact with a touch-enabled device ...
The TouchEvent.TOUCH_TAP is :
Dispatched when the user lifts the point of contact over the same InteractiveObject instance on which the contact was initiated on a touch-enabled device ...
And with some tests, we can see that the TouchEvent.TOUCH_END event is, in almost cases, fired before the TouchEvent.TOUCH_TAP one (by 1 or 2 milliseconds), so we can understand the we are able to detect if the user has already removed contact with the device (TouchEvent.TOUCH_END is fired) then if that was on the same InteractiveObject object on which the contact was initiated (TouchEvent.TOUCH_TAP is fired).
And that's why your code is not working.
Now, let's see your problem : you want to show a MovieClip just when your user tap a button and hide it when he releases that button but only for a very short time (the time of a tap ~= 300 milliseconds).
In this case, I recommend you to use a TouchEvent.TOUCH_BEGIN event listener with a timeout to hide that object even if your user didn't release the button.
For that, take this example :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
btn.addEventListener(TouchEvent.TOUCH_BEGIN, on_touchBegin);
function on_touchBegin(e:TouchEvent): void
{
obj.visible = true;
hide_obj();
}
function hide_obj(): void
{
// you can use a Timer object instead of setTimeout()
var timeout:int = setTimeout(function(){
clearTimeout(timeout);
obj.visible = false;
}, 300);
}
Hope that can help.

Missing Event type MouseUp

In the following scenario a Event from type MouseUp is missing.
The base to reproduce is this code (using rap 3.0)
#Override
protected void createContents(Composite parent) {
GridLayout gl = new GridLayout(1, false);
parent.setLayout(gl);
GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
Composite above = new Composite(parent, SWT.BORDER);
above.setLayout(gl);
above.setLayoutData(gd);
Button buttonAbove = new Button(above, SWT.TOGGLE);
buttonAbove.setText("above");
Button button = new Button(parent, SWT.TOGGLE);
button.setText("start");
addListener(button);
Composite bottom = new Composite(parent, SWT.BORDER);
bottom.setLayout(gl);
bottom.setLayoutData(gd);
Button buttonBottom = new Button(bottom, SWT.TOGGLE);
buttonBottom.setText("bottom");
}
private void addListener(Button button) {
Listener listener = new Listener() {
#Override
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseDown:
System.out.println("MouseDown");
break;
case SWT.MouseUp:
System.out.println("MouseUp");
break;
}
}
};
button.addListener(SWT.MouseDown, listener);
button.addListener(SWT.MouseUp, listener);
}
go with the cursor over the button start
press the mouse button and keep it pressed
drag the cursor to the right, over the button bottom or to the composite buttom and release the MouseButton --> the Event from type SWT.MouseUp is fired
go with the cursor over the button start again
press the mouse button and keep it pressed
drag the cursor over the button above or over the composite above and release the mouse button --> the Event is not fired
I found this problem in a more complex way while doing drag'n'drop of a Button and this seem to be the root of the problem.
In a SWT Application a MouseUp Event is always thrown.
Is this a bug in RAP?

Flex topLevelApplication and SuperTabNavigator mouse event handler

I am using the flex SuperTabNavigator and want on closing the tab check if the control button was pressed. I tried:
public static var CONTROL_PRESSED:Boolean = false;
public function init():void {
var clickListener:Function = function _clickListener(event:MouseEvent):void{
trace(event.ctrlKey);
if(event.ctrlKey){
CONTROL_PRESSED = true;
}else{
CONTROL_PRESSED = false;
}
};
FlexGlobals.topLevelApplication.addEventListener(MouseEvent.CLICK, clickListener);
}
The problem with this is that the mouse click is called everywhere in the application except on the tab. I also tried the same code but addEventListener(MouseEvent.CLICK, clickListener); to add the listener to the SuperTabNavigator and it did not work at all. Is there another way to catch the mouse click?
This is because the SuperTabNavigator has a private mouse click handler function that hides the MouseEvent:
private function closeClickHandler(event:MouseEvent):void {
if(this.enabled) {
dispatchEvent(new Event(CLOSE_TAB_EVENT));
}
event.stopImmediatePropagation();
event.stopPropagation();
}
You'll need to modify the SuperTab class in the SuperTabNavigator source to dispatch some CustomEvent with the data you want instead of a plain new Event(CLOSE_TAB_EVENT).
Then change the onCloseTabClicked function in SuperTabBar to know about your CustomEvent. Then you can pass that to your application code (by adding it to the SuperTabEvent, perhaps).

WP8 Deleting Images from Map (Error when navigating back to page)

I have a Map that has several Images placed on it (inside the page_loaded event)
MapLayer pinLayer = new MapLayer();
MapOverlay pin50 = new MapOverlay();
pin50.GeoCoordinate = new GeoCoordinate(49.42563670946435, -0.44644108276367537);
Canvas myCanvas50 = new Canvas();
// The image is defined globally
pin50.PositionOrigin = new Point(0.5, 0.5);
image50.Source = new BitmapImage(new Uri("Images/Destroyer.png", UriKind.Relative));
image50.Opacity = 1;
Point point50 = new Point(1.0, 1.0);
Canvas.SetLeft(image50, point50.X);
Canvas.SetTop(image50, point50.Y);
myCanvas50.Children.Add(image50); // <=== ERROR OCCURS HERE
// Making an event handler for the image so that we can 'tap' on it
image50.DoubleTap += image50_DoubleTap;
pin50.Content = myCanvas50;
pinLayer50.Add(pin50);
map_J.Layers.Add(pinLayer);
It works fine for all of the Images, however when I click on an Image it navigates to a new page and gives a description of the Image that was clicked on.
The problem is that when a user navigates back to this map (by intuitively pressing the hardware BACK BUTTON) the app crashes due to the error "Element is already the child of another element."
I'm guessing that the page is still loaded so when it is navigated back to it gets 're-loaded' so can't add the same Image to the same canvas etc.
I tried adding the following to clear all information from the map before navigating away from the page but it doesn't help:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
var toRemove = new List<MapLayer>();
foreach (MapLayer lyr in map_J.Layers)
{
toRemove.Add(lyr);
}
// now do the actual removal
foreach (var child in toRemove)
{
map_J.Layers.Remove(child);
}
}
Any ideas? Is there a better way to handle the reloading of the map/elements?
I would recommend clearing the map layers everytime the user leaves the page.
Add this code to the main tag in the XAML code of the view:
<phone:PhoneApplicationPage
...
Unloaded="PhoneApplicationPage_Unloaded" >
And this event code behind:
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
map_J.Layers.Clear();
}

Set Popup Window Specific Position?

Actually in my Application Login Page is Popup..
private var login:Login;
protected function loginPopUpHandler(event:MouseEvent):void
{
login = new Login();
PopUpManager.addPopUp(login,this,true);
PopUpManager.centerPopUp(login);
}
Hear Popup coming Center ....
But I want Popup Below the Button..
i.e... When we click the login Button Popup will open Below the Button..
Try:
var sym:Login = PopUpManager.createPopUp( this, Login) as Login;
sym.x = button.x;
sym.y = button.y+button.height;
First of all it depends what this is. If this for example is your Login button you can easily do like this:
protected function loginPopUpHandler(event:MouseEvent):void
{
login = new Login();
PopUpManager.addPopUp(login,this,true);
PopUpManager.centerPopUp(login);
login.x += loginButton.width / 2; // or any other number
login.y += 123; //whatever number. try tweaking it
}
If your want to use display object as FlexGlobals.topLevelApplication as DisplayObject then you probably want to get you login button from event.target and manipulate with its coordinates. Just try to experiment.
Also you maybe will have to use localToGlobal property when playing with coordinates.
Working for me:
protected function loginPopUpHandler(event:MouseEvent):void
{
login = new Login();
login.x = 10; // x value relative to "this"
login.y = 123; // y value relative to "this"
PopUpManager.addPopUp(login,this,true);
}