Collapsed Grid visibility from class in windows phone 8.1 - windows-phone-8.1

I am currently working with windows phone 8.1 [RT] application , In my application I have hide Grid Visibility from class.
For that I have create one public method on cs page
public void HideCancelButton()
{
grdCancle.Visibility = Visibility.Collapsed;
bdrCancel.Visibility = Visibility.Collapsed;
Debug.WriteLine("hide button");
//UpdateLayout();
}
and calld that method in following manner in helperClass.cs
MainPage mypage = new MainPage();
mypage.HideCancelButton();
it will debug "hide button" but doesn't hide grid
I have also use
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() =>{});

It won't hide the grid because you are not referencing the MainPage that is currently showing.
You should get the reference to the main page wherever you are calling the HideCancelButton method.
In your case the easiest solution will be doing something like this (considering you are not calling the method from the MainPage class itself.
Frame rootFrame = Window.Current.Content as Frame;
MainPage mainPage = rootFrame.Content as MainPage;
if(mainPage != null)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(
Windows.UI.Core.CoreDispatcherPriority.Normal,
() => { mainPage.HideCancelButton(); }
);
}

Related

Making JavaFX Alerts/Dialogs Modal within Swing Application

So once again we are in the process of converting our existing Java application that was using entirely Swing to using JavaFX. However, the application will not be using JavaFX entirely. This seems to be causing some issues with Alerts/Dialogs and modality. We are currently using Java 8u40.
The main application is basically in a JFrame that has a Menu. The main content pane is JDesktopPane and clicking a MenuItem opens new JInternalFrames within the DeskopPane. Screens we are converting to JavaFX are basically JFXPanels within a JInternalFrame at the moment. Any Alerts/Dialogs that are opened from the JFXPanels are modal to the panel itself, but not to the JInternalFrame, DeskopPane, Menu, etc.
I read in the DialogPane documentation that they are planning to introduce some lightweight dialogs and even possibly InternalFrames in future releases of JavaFX, so maybe we'll just have to wait it out a little longer for this functionality. But, ideally when opening a new Alert/Dialog it would be modal to the entire Application.
EDIT:
Currently doing the following for modal dialogs:
((Stage)getDialogPane().getScene().getWindow()).setAlwaysOnTop(true);
This makes the dialog always appear on top, however the dialog also remains on top of other applications even if our main application is minimized. It also does not block input to any Swing components in the frame.
You can use the following work-around which creates an invisible JDialog when the Alert is shown and disposes the JDialog when the Alert is closed. This approach extends the modality to the whole application, including the Swing part.
// create Alert
Alert alert = new Alert(AlertType.INFORMATION, "Hello");
// create invisible JDialog and "show" it
JDialog dialog = new JDialog();
dialog.setModal(true);
dialog.setUndecorated(true);
dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
SwingUtilities.invokeLater(() -> dialog.setVisible(true));
// show Alert
alert.showAndWait();
// close JDialog after Alert is closed
dialog.dispose();
I don't think i understand your question completely. But here is my guess - You are trying to make an alert window from some JFXPanel that will be modal (i.e. user will not be able to click in your application until she closes that alert window) to your entire application which is written partially using swing components.
If your application would be written in purely JavaFX then you would do something like (Assuming you have created a button somewhere in your JFXPanel)
button.setOnAction(evt -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.initModality(Modality.APPLICATION_MODAL);
// This will not work in your code
alert.initOwner(button.getScene().getWindow());
alert.show();
});
but since initOwner requires a javafx.stage.window object passing a swing component won't work in your code. As of Java 8u40 i don't think there is a right way(i.e. not hacks) to set ownership of Alert objects to swing component. Not surprisingly such questions has already been asked here and not answered as of writing this.
For your requirements you can use JOptionPane.showMessageDialog method and its look alike as workaround.
button.setOnAction(evt -> {
JOptionPane.showMessageDialog(desktopPane,"My message");
});
These dialog boxes are modal by default so no work is necessary. You can call these from any event handler methods of JavaFX components.
I've done a little workaround with a small interface which is implemented in my JavaFXFrame:
public interface DialogParent {
void setOnFocusGained(EventHandler<FocusEvent> focusHandler);
void setOnCloseRequest(EventHandler<WindowEvent> closeHandler);
}
And my JavaFXFrame implementation
public class JavaFXFrame implements DialogParent {
private JFrame frame;
private EventHandler<ch.irix.sumadmin.util.FocusEvent> focusGainedHandler;
private EventHandler<javafx.stage.WindowEvent> windowClosingHandler;
public void JavaFXFrame() {
final JFXPanel fxPanel = new JFXPanel();
frame = new JFrame();
frame.add(fxPanel);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
tryClosing(this);
}
});
frame.addWindowFocusListener(new WindowAdapter() {
#Override
public void windowGainedFocus(WindowEvent e) {
if (focusGainedHandler != null) {
focusGainedHandler.handle(new FocusEvent());
}
}
});
}
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
private void tryClosing(WindowListener listener) {
javafx.stage.WindowEvent windowEvent = new javafx.stage.WindowEvent(null, javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST);
if (windowClosingHandler != null) {
windowClosingHandler.handle(windowEvent);
}
if (!windowEvent.isConsumed()) {
frame.setVisible(false);
}
}
#Override
public void setOnFocusGained(EventHandler<ch.irix.sumadmin.util.FocusEvent> focusGainedHandler) {
this.focusGainedHandler = focusGainedHandler;
}
#Override
public void setOnCloseRequest(EventHandler<javafx.stage.WindowEvent> windowClosingHandler) {
this.windowClosingHandler = windowClosingHandler;
}
}
And showing an Alert:
public static void showAlert(Alert alert) {
DialogPane dialogPane = alert.getDialogPane();
final Stage stage = new Stage();
stage.setScene(dialogPane.getScene());
List<ButtonType> buttonTypes = dialogPane.getButtonTypes();
for (ButtonType buttonType : buttonTypes) {
ButtonBase button = (ButtonBase) dialogPane.lookupButton(buttonType);
button.setOnAction(evt -> {
dialogPane.setUserData(buttonType);
stage.close();
});
}
dialogParent.setOnFocusGained(event -> {
stage.toFront();
});
dialogParent.setOnCloseRequest(Event::consume);
stage.setOnCloseRequest(event -> {
dialogParent.setOnFocusGained(null);
dialogParent.setOnCloseRequest(null);
});
stage.show();
}
Hope this will help you

Listener on MenuBar in Vaadin

I want to add a ClickListener to an Item of a MenuBar in Vaadin.
I know about the normal situation, which i got working:
MenuBar menubar = new MenuBar();
menubar.addItem("Item", new MenuBar.Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
//Do sth. when item is clicked
}
});
In my application, I'm working with MVP, so the code which should run, is in an other class than the code which is defining the menubar.
Is there a way to add a listener to a specific item in the menubar ?
When you add an item to your MenuBar the function addItem(String,Command) actually returns a MenuItem which can be used later. You can do this :
MenuItem select = menuBar.addItem("Select", null);
And in another context you can add a listener on that MenuItem like this:
select.setCommand(new Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
System.out.println("You clicked on "+selectedItem.getText());
}
});

Hardware Back button is not working in (Webview ) windows phone 8

I am having trouble getting the hardware back button to do what I would like it to do for the Windows Phone 8. The app is strictly just webview, so as of now when a back (hardware) button is clicked it closes the app but i want to back just previous page. I put the the URL in constructor like below
namespace Masala
{
public partial class Entertainment : PhoneApplicationPage
{
public Entertainment()
{
InitializeComponent();
var targetUri = new Uri("http://mobile-masala.com");
WebBrowser.Navigate(targetUri);
}
}
}
Add following code in your page.xam.cs to handle back button press...
protected override void OnBackKeyPress(CancelEventArgs e)
{
if(WebBrowser.CanGoBack)// your code... check the web view that you can go back or it is the main page.
{
WebBrowser.GoBack();
e.Cancel = true;
}
}

Changing an Image.Source property in Windows Phone 8 app causes proximity event subscription to stop working

Very simple repro app - created a new Windows Phone 8 C# app from template in Visual Studio, added an Image to the content panel, then subscribed for NDEF proximity messages as shown:
// Constructor
public MainPage()
{
InitializeComponent();
ProximityDevice device = ProximityDevice.GetDefault();
if (device != null)
{
device.SubscribeForMessage("NDEF", handler);
}
}
private void handler(ProximityDevice sender, ProximityMessage message)
{
Debug.WriteLine("Received message");
Dispatcher.BeginInvoke(() =>
{
myImage.Source = new BitmapImage(new Uri("Assets/test.png", UriKind.Relative));
});
}
First event works fine (image source is changed successfully). However, after this event has fired once it no longer fires if I touch another NFC tag. If I remove the call to update the image source, it will fire on each touch.
I can't understand why there would be any interaction here. The phone I'm testing on is a Nokia Lumia 620.
Just a shot in the dark: try to keep reference to your ProximityDevice somewhere. Make it a member of the class and assign in constructor.
class MainPage : PhoneApplicationPage
{
private ProximityDevice device;
public MainPage()
{
InitializeComponent();
device = ProximityDevice.GetDefault();
if (device != null)
{
device.SubscribeForMessage("NDEF", handler);
}
}
...
}
As it correctly mentioned in comments, this is caused by the Garbage Collector, which collects the instance of the ProximityDevice. When new BitmapImage is created, memory consumption grows, and this triggers the GC process. That's why you don't have such issue when you don't create the image.

Switch to another View, but stay in the Navigation Controller

I have a navigation controller with details about a specific product, one of the details is a small image of the product. Now I want to add a button which must go to a view controller with a enlarged image of the product.
How can I make that the enlarged image is on the same navigation controller as the detail screen and as transition an flip of the screen but that the header and tab bar won't flip.
Added a overview image of the two views on the navigation controller.
You can use something like:
UIView.Transition(this.NavigationController.View, 1f,
UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.TransitionFlipFromRight,
delegate { this.NavigationController.PushViewController(this.ImageViewController, false); });
To have your image view flipped in when getting pushed on the navogation controller, instead of the default transition. The title property of the ImageViewController should be the same as the one from your details.
The idea of the UINavigationController is to handle multiple controllers, so you should not work against it by overlaying a view.
Add two views to the controller and animate their frame properties to get the effect you want.
See the example below.
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace delete20121203
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
var ctrl = new MyViewController ();
var nav = new UINavigationController (ctrl);
window.RootViewController = nav;
// make the window visible
window.MakeKeyAndVisible ();
return true;
}
}
public class MyViewController : UIViewController
{
UIButton _button1;
UIButton _button2;
UIView _view1;
UIView _view2;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Title = "Test";
_view1 = new UIView (View.Bounds);
_view1.AutoresizingMask = UIViewAutoresizing.All;
_view1.BackgroundColor = UIColor.White;
_button1 = UIButton.FromType (UIButtonType.RoundedRect);
_button1.Frame = new System.Drawing.RectangleF (10, 10, 150, 44);
_button1.SetTitle ("Click", UIControlState.Normal);
_button1.TouchUpInside += Button1Click;
_view2 = new UIView (View.Bounds);
_view2.AutoresizingMask = UIViewAutoresizing.All;
_view2.BackgroundColor = UIColor.LightGray;
RectangleF hideRect = _view2.Frame;
hideRect.X = hideRect.X + hideRect.Width;
_view2.Frame = hideRect;
_button2 = UIButton.FromType (UIButtonType.RoundedRect);
_button2.Frame = new System.Drawing.RectangleF (10, 10, 150, 44);
_button2.SetTitle ("Back", UIControlState.Normal);
_button2.TouchUpInside += Button2Click;
_view1.Add (_button1);
_view2.Add (_button2);
View.Add (_view1);
View.Add (_view2);
}
void Button1Click (object sender, EventArgs e)
{
UIView.Animate (.5f, 0, UIViewAnimationOptions.CurveEaseInOut, delegate {
_view2.Frame = View.Frame;
RectangleF hideRect = _view1.Frame;
hideRect.X = hideRect.X - hideRect.Width;
_view1.Frame = hideRect;
},
null);
}
void Button2Click (object sender, EventArgs e)
{
UIView.Animate (.5f, 0, UIViewAnimationOptions.CurveEaseInOut, delegate {
RectangleF hideRect = _view2.Frame;
hideRect.X = hideRect.X + hideRect.Width;
_view2.Frame = hideRect;
_view1.Frame = View.Frame;
},
null);
}
}
}