Xamarin iOS UIButton How To Click button programmatically? - c#

I'm attempting to fake the act of clicking a button, by programmatically calling it within my ViewWillAppear() function.
The onclick function is defined in my ViewDidLoad(), and you can see I am trying to use a Perform Selector to manually call the button.
The button does not appear to be running. Any ideas?
public override void ViewDidLoad()
{
base.ViewDidLoad();
idButtonScanLoad.TouchUpInside += async (sender, ea) =>
{
System.Console.WriteLine("Scan button pressed");
};
}
[Export("TouchUpInsideEvent:")]
private void TouchUpInsideEvent(NSObject sender)
{
Console.WriteLine("yay!");
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.PerformSelector(new ObjCRuntime.Selector("TouchUpInsideEvent:"), this as NSObject, 10f);
}

Here is the answer, you should call it from your ViewDidLoad method.
myButton.SendActionForControlEvents (UIControlEvent.TouchUpInside);

If you want to create a button's click event, you can use the this
public override void ViewDidLoad()
{
Mybutton.AddTarget(ButtonEventHandler, UIControlEvent.TouchUpInside);
}
public void ButtonEventHandler(object sender, EventArgs e)
{
if(sender==Mybutton)
{
//do stuff here
}
}

Related

ScrollChange event equivalent on iOS

I want to hide a button when the user is scrolling through the page so a specific area of the page is not blocked by the button, after the user finish scrolling I will show the button again after a delay of 1000-1500ms. My idea so far is that I will subscribe to the scroll changing event, hide the button, wait for specific delay, show the button again. I managed to implement it for Android platform using effect like this:
public class AndroidScrollingEffect : Xamarin.Forms.Platform.Android.PlatformEffect
{
private bool _isAttached;
public static void Initialize() { }
protected override void OnAttached()
{
if (!_isAttached)
{
Control.ScrollChange += Control_ScrollChange;
_isAttached = true;
}
}
private void Control_ScrollChange(object sender, global::Android.Views.View.ScrollChangeEventArgs e)
{
var command = ScrollingEffect.GetCommand(Element);
command?.Execute(null);
}
protected override void OnDetached()
{
if (_isAttached)
{
Control.ScrollChange -= Control_ScrollChange;
_isAttached = false;
}
}
}
The implementation is working just fine, for android platform. Now, I want to do the same thing for iOS. So, is there an equivalent event for ScrollChange on iOS?
P.S. I want to use effect so I don't write code in the code behind of the page and still able to bind to a specific command in the view model.
Forms ScrollView is rendered as UIScrollView on iOS. So you can try the code below to construct your custom effect:
public class iOSScrollingEffect : PlatformEffect
{
UIScrollView nativeControl;
private bool _isAttached;
protected override void OnAttached()
{
nativeControl = Control as UIScrollView;
if (nativeControl != null && !_isAttached)
{
nativeControl.Scrolled += NativeControl_Scrolled;
_isAttached = true;
}
}
private void NativeControl_Scrolled(object sender, EventArgs e)
{
...
}
protected override void OnDetached()
{
if (_isAttached)
{
nativeControl.Scrolled -= NativeControl_Scrolled;
_isAttached = false;
}
}
}

xamarin android button click is not working

I am new in xamarin. so just trying to make basic application. but button click is not working i.e. when click from application code is not executed
MainActivity.cs
StartActivity(typeof(LoginActivity));
LoginActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Login);
Button button = FindViewById<Button>(Resource.Id.loginButton);
if (button != null)
{
button.Click += (sender, e) =>
{
save();
};
}
}
public void save()
{
string ab = "asbs";
Console.WriteLine("Working");
}
Declare the following method:
public void save(Object sender, System.EventArgs e)
{
// Add code to run when the button is clicked here
}
To attach the method to the click event, include the following in your code:
if (BtnSave != null)
{
BtnSave.Click += save;
}
Try with this
// set onclick listener here, by deleting some process
button.Click += delegate {
loginButtonClick();
};
public void loginButtonClick()
{
Toast.MakeText(this, "Proceed to Login ", ToastLength.Long).Show();
}

UserControl OnMouseWheel fires but other events do not

I have some basic override code in the same class to catch mouse events. I can get OnMouseWheel to fire, but other click events do to not fire with the same code.
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
Debug.WriteLine("mouse down"); //does not work
}
protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
{
Debug.WriteLine("mouse wheel"); //works
}
Does OnMouseWheel need focus to fire? That has been what I have been trying to troubleshoot so far.
protected override void OnMouseClick ( MouseEventArgs e )
{
Debug.WriteLine("Mouse click"); //works
}
private void panel1_MouseClick ( object sender, MouseEventArgs e )
{
OnMouseClick(e);
}
In your actual design window make sure you attached the event to the Control. If you need to do it programatically, then
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);
Note:
Yes I know the control names aren't the exact same and you aren't using MetroControls, but it's the same idea.
You need to call the base method implementation of OnMouse events so the base class can react to the events:
protected override void OnMouseDown(MouseEventArgs e)
{
Debug.WriteLine("OnMouseDown");
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Debug.WriteLine("OnMouseUp");
base.OnMouseUp(e);
}
protected override void OnMouseWheel(MouseEventArgs e)
{
Debug.WriteLine("OnMouseWheel");
base.OnMouseWheel(e);
}

Button Click On App Start

So I have a function inside of a button that I want to trigger as soon as my application starts, and I am doing it by this:
public partial class App : Application
{
private void App_OnStartup(object sender, StartupEventArgs e)
{
//get button reference
Button btn = ((MainWindow)Current.MainWindow).btnJSON;
//programmatically simulate button click
ButtonAutomationPeer peer =
new ButtonAutomationPeer(btn);
IInvokeProvider invokeProv =
peer.GetPattern(PatternInterface.Invoke)
as IInvokeProvider;
invokeProv.Invoke();
}
}
However, this leads be to a nullReferenceExeption. How do I achieve this, i.e. invoking the fucntion on App stratup or simulate the button click?
Here is the button signature:
public void btnJSON_Click(object sender, RoutedEventArgs e)
{
...
}
Thanks.
Instead of trying to invoke the button click, just call the method that the button click invokes.
public partial class App : Application
{
private void App_OnStartup(object sender, StartupEventArgs e)
{
((MainWindow)Current.MainWindow).DoStuff();
}
}
In your MainWindow.xaml.cs:
public void DoStuff()
{
// TODO: Do Stuff
}
Just have your button click handler call that method.
public void btnJSON_Click(object sender, RoutedEventArgs e)
{
DoStuff();
}
Don't call the button click if all you want is to run some code. If your button's click handler looks like:
public void btnJSON_Click(object sender, RoutedEventArgs e)
{
DoSomeInterestingStuff();
}
Then just call that method from your startup method:
private void App_OnStartup(object sender, StartupEventArgs e)
{
DoSomeInterestingStuff();
// other stuff to do here
}

I want to capture mouse click event which was clicked outside of the user control?

I have a user control in window c# and i am adding that control into another project. I want to capture mouse click event which was clicked outside of the user control. How can i enable this feature in it?
You'll have to assign these,but this should work
bool ValidClick = false;
private void form_Click(object sender, EventArgs e)
{
if(!ValidClick)
ValidClick = true;
method();
}
private void control_Click(object sender, EventArgs e)
{
ValidClick = false;
method();
}
public void method()
{
if(ValidClick)
{
//Do method
}
}

Categories

Resources