I'm developing Xamarin Forms app and I need to handle an event when physical button is pressed (on Android). What I've found out is that I can use DispatchKeyEvent inside an activity in Xamarin.Android project. However I don't know specifically how to do it.
In the shared code I have defined a ContentPage and I'm trying to override the DispatchKeyEvent inside a custom page renderer in Xamarin.Android. However, I get an error about missing constructor (I suppose it is related to layout, which is defined in the shared code, but I don't know why Xamarin.Android renderer doesn't use it).
How to solve this problem? Maybe there is some easier way to get info about key being pressed?
In the code behind for your page, you can override OnBackButtonPressed.
protected override bool OnBackButtonPressed()
{
return base.OnBackButtonPressed();
}
Related
I am trying to override the OnBackPressed() method in a pcl project. I do not know where i must ovveride it. In the portable project or in the android? I want to navigate to another page when i click the back button. Any suggestions?
You need to override OnBackButtonPressed in your PCL project on your custom Page classes.
protected override bool OnBackButtonPressed()
{
// call your custom navigation code here
// returning true because you are handling the navigation
return true;
}
Note that this event is raised when the hardware back button is pressed but will not be raised on iOS.
I'm currently doing some cross-platform mobile development through Visual Studio using Xamarin (so in C#) and am about to start the iOS portion. I've never done iOS development before and thought I could get myself acquainted with their "Hello, iOS" Tutorials. Unfortunately, things have not been going smoothly. I constantly get NSInvalidArgumentExceptions from my TouchUpInside actions:
Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSInvalidArgumentException Reason:
-[ViewController TranslateButton_TouchUpInside:]:
unrecognized selector sent to instance 0x7b6200d0
I can occasionally remedy it for a moment by literally remaking the Buttons, but it breaks pretty much right afterwards. The actual error itself occurs in my Main.cs file:
using UIKit;
namespace CheckinIOS
{
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate"); //this line is where it breaks
}
}
}
In case it is any helpful, I am trying to deploy to iPhone 5S simulator running iOS 9.3 (but it breaks on iPhone 6 simulator as well). I could also post more of my code if necessary, but I copypasted all the C# from Xamarin's tutorial, and did the same thing as them for Main.storyboard.
I have spent a while looking for people with the same problem as me, but their solutions either did not work, or they got the error for slightly different reasons. Any assistance is appreciated.
EDIT: Here is my implementation of TranslateButton_TouchUpInside:
TranslateButton.TouchUpInside += (object sender, EventArgs e) =>
{
// Convert the phone number with text to a number
// using PhoneTranslator.cs
translatedNumber = PhoneTranslator.ToNumber(PhoneNumberText.Text);
// Dismiss the keyboard if text field was tapped
PhoneNumberText.ResignFirstResponder();
if (translatedNumber == "")
{
CallButton.SetTitle("Call", UIControlState.Normal);
CallButton.Enabled = false;
}
else
{
CallButton.SetTitle("Call " + translatedNumber, UIControlState.Normal);
CallButton.Enabled = true;
}
};
The iOS Runtime is looking for a method called (in Obj-C land) TranslateButton_TouchUpInside: in your ViewController class. However there is no method exported to Obj-C with that name. A first guess is that you added an event to the button in the storyboard that perhaps had that name, but you either deleted that method or never implemented it.
Try opening your storyboard in iOS Designer and removing any event from the Properties->Events tab when your button is selected on the canvas. Also I assume your button has the name TranslateButton in the Properties->Widget pane when the button is selected on the canvas.
There are a couple ways to attach events to controls in Xamarin iOS. One, and the preferred way, is to create an event in iOS Designer for the control. If you do this, a partial method stub will be in the .designer.cs file with an Export attribute that exports the method name to the Obj-C runtime. You will then need to implement this method, using the same signature (without the Export Attribute), in your main .cs file for the ViewController. This is called, in Obj-C land, an action.
The other way is to do as is shown in your code snippet. In this case you ONLY need to give the control a name in the Properties->Widget pane that you can then use in code to subscribe to the TouchUpInside event. This is called, in Obj-C land, an outlet.
My guess is that you did both but without ever implementing the TranslateButton_TouchUpInside: method in your ViewController. Note that this is the Obj-C name used in the Export attribute of the method stub created in the .designer.cs file when you add an event to a control.
But it is hard to say without seeing the storyboard and both the main ViewController.cs file and the ViewController.designer.cs file
i currently implement an uwp application and i would like to use a secondary tile. I succeeded to create this tile but i don't find any documentation explaining how to open the view of my application when i click on this tile. Thanks in advance for your help.
I find a solution to this problem. In fact, i use the same strategy as windows 8.
We just need to use the OnLaunched event handler (in App.Xaml.cs) to discover if the app is launched from a SecondaryTile or the primary one
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
bool CalledFromSecondaryTile = false;
if (!string.IsNullOrEmpty(e.Arguments))
{
CalledFromSecondaryTile = true;
}
I have a WPF application without an application.xaml, since I need to do the Main() method by myself. Therefore I neither have an ApplicationDefinition nor an application resource. I currently attach the resource dictionary to the application at the application startup
Of course the WPF Designer complains now about missing resources.
So I want to get rid of two problems:
- I don't want to attach the resource dictionary manually at startup
- I want to get the resources also work at design time in the WPF designer
Is there any help for this problem?
Thanks
Martin
There is a possibility to use the Main() method for yourself
Just delete the following property from the xamlcode of Application.xaml:
StartupUri="MainWindow.xaml"
Then add the following in the code-behind of the Application.xaml:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// My code goes here, but nothing ever happens.
base.OnStartup(e);
}
}
Now you can still use the Application.xaml for your resources and also have your specific startup procedure.
I'm using Monotouch to develop an app for the iPhone.
In my iPhone only app, I have an InputAccessoryView appear whenever the user selects a textfield. The accessory view provides buttons which aid the user (undo/redo etc).
It works fantastically on the simulator and on iPhone devices.
However, out of the blue, the Input Accessory View is not appearing on the iPad. I've made no changes to the code regarding the views; I've even rolled back to a version that I know displayed the accessory view correctly.
I was wondering if anyone else has come across this behaviour before / would know why this is happening?
EDIT
I've seen this accross all of my iphone projects running on the ipad. I made a fresh project which only contains 1 view, a UITextField and override the Input Accessory View and I'm still seeing nothing.
Code I'm using to test the override of the input view is:
public override UIView InputAccessoryView
{
get
{
UIView view = new UIView(new RectangleF(0,0,320,30));
view.BackgroundColor = UIColor.Blue;
return view;
}
}
Nothing too complex, on the iPhone just returns a blue bar above the keyboard.
I've reinstalled Mono, MonoDevelop, Monotouch and the iOS SDK multiple times to no avail. Apps that I've downloaded from the store still show the Input Accessory View, so I'm beginning to wonder if it's an issue with my Monotouch/iOS SDK combo? I'm using Monotouch 3.1.3 personal and 4.1 iOS SDK - version 2.6 of Mono. I'm going to try updating to version 2.8 of Mono.
The thing I don't understand is why it would work previously then all of a sudden just stop working?
When I'm deploying the code on the iPad, I'm selecting "Rebuild all", then uploading to the device. It doesn't matter if I pick either Release/Debug as a build, both yield the same result.
EDIT 2
If I subclass a UITextField and override the InputAccessoryView within that subclass, then the view appears on the iPad. This means that the InputAccessoryView which is being overriden in the View class isn't being assigned to the Textfield on the ipad.
EDIT 3
It would appear this is fixed in iOS 4.2 with Monotouch 3.2 !
My first suggestion is to reset the device in iTunes. You may have done so already, but just in case you did not, please do it first (since it's quick and easy to do).
If that didn't help: To identify if it is a problem with Monotouch and/or the iOS SDK could you repeat what you did above using only Cocoa Touch?
I.e., start a View-based app in XCode, add one UITextField in Interface Builder and connect it to the controller. Here is a corresponding Objective-C code to do it:
Instance variables in your controller (add to the #interface section in the <controllername>.h file):
IBOutlet UITextField *text;
UIView *accessory;
Hook up the UITextField in Interface Builder with the text outlet and then add the following implementation of viewDidLoad to the controller (the #implementation section in the <controllername>.m file):
- (void)viewDidLoad {
accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[accessory setBackgroundColor:[UIColor blueColor]];
[text setInputAccessoryView:accessory];
[super viewDidLoad];
}
If this displays the blue bar correctly, the problem is with the setup of Monotouch. If it does not, it is an issue with your device.
I had the same issue where a UITextView contained within a UIView would show the accessory bar on the simulator but not on the iPhone or iPad.
I'm doing some strange things with BeginFirstResponder to allow tapping anywhere in the larger parent area (containing a label and the text field) and activating the contained field and suspected this was involved. I added code in the parent UIView to override the InputAccessoryView and have it return the the value from the child UITextView and this fixed.
I suspect that Monotouch has an issue routing the event to the wrong object and my fix resolved it.
pseudo code:
public class ParentView : UIView
{
UILabel Label;
MyTextView Text;
/*Magic line here that fixed the problem */
public override UIView InputAccessoryView {get{return Text.InputAccessoryView;}}
}
public class MyTextView : UITextView
{
UIView Accessory;
public override UIView InputAccessoryView {get{return Accessory;}}
}
How are you showing your accessory view?
Can you try keeping a reference to the view?
Try this, and let me know if this fails:
public class Foo {
static UIView track;
}
And then when you create your view, stick it in that global:
Foo.track = new MyAccessoryView ()