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();
}
Related
I have a WebView in a page and I'm playing a youtube video. When I navigate away from the page to the previous page on the stack, the youtube video in the web view keeps playing the youtube video. Anyone know how to prevent this and kill the WebView when the user navigates away from the page?
EDIT:
Here is how I'm navigating back
private void HtmlViewerPage_BackRequested(object sender, BackRequestedEventArgs e)
{
if (Viewer.CanGoBack)
{
Viewer.GoBack();
e.Handled = true;
return;
}
{
GoBack will navigate the frame stack. It then allows the youtube video to continue to play in the background.
EDIT: I created a sample project that demonstrates my issue.
https://github.com/themimcompany/WebViewIssue
You've registered BackRequested event in MainPage and WebViewPage, but you have not unregistered it when you navigate to other pages. This will cause your issue. When you click back button on WebViewPage, the BackRequested handler will be triggered multi times.
You could remove it in OnNavigatingFrom handler on MainPage.xaml.cs and WebViewPage.xaml.cs.
Please see the following code:
public sealed partial class WebViewPage : Page
{
public WebViewPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += WebViewPage_BackRequested;
var site = (e.Parameter as Dictionary<string, string>)["site"];
WebViewer.Navigate(new Uri(site));
}
private void WebViewPage_BackRequested(object sender, BackRequestedEventArgs e)
{
//always try to go back within the WebView first, then try the frame!
if (this.Frame.CanGoBack)
{
if (WebViewer.CanGoBack)
{
WebViewer.GoBack();
e.Handled = true;
}
else
{
WebViewer.NavigateToString("<html>Unloaded.</html>");
WebViewer.NavigateToString("");
var source = WebViewer.Source; // is cleared to null
Frame.GoBack();
e.Handled = true;
}
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
SystemNavigationManager.GetForCurrentView().BackRequested -= WebViewPage_BackRequested;
}
}
//MainPage.xaml.cs
private void AppBackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
return;
// If we can go back and the event has not already been handled, do so.
if (rootFrame.CanGoBack && e.Handled == false)
{
e.Handled = true;
rootFrame.GoBack();
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += AppBackRequested;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
SystemNavigationManager.GetForCurrentView().BackRequested -= AppBackRequested;
}
How about overriding the OnNavigatingFrom method and stop the webview?
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
}
Inside the method try setting the WebView to null, or setting the Source to null or something else
This will stop it:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
// this will stop any video from playing back.
WebView.NavigateToString("<html>Unloaded.</html>");
base.OnNavigatingFrom(e);
}
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;
}
}
}
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
}
}
I'm enlisting to the Gtk.Dialog close event from another dialog like this:
AddTask dialog = new AddTask (task); //AddTask inherits from Gtk.Dialog
dialog.Close += HandleDialogClose;
dialog.Show();
void HandleDialogClose (object sender, EventArgs e)
{
Refresh(); //Does some stuff in the calling dialog
}
When I close the dialog (the one I created above), the HandleDialogClose event is never triggered. Any idea why?
Here is some sample code, and I solved it now myself. I was enlisting to the Close() event, when I should have been enlisting the Destroyed() event. The code below now works (the commented code is what was not working).
using System;
using Gtk;
namespace test
{
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
Button button1 = new Button();
button1.Label = "Open About";
this.Add(button1);
button1.Show();
button1.Clicked += HandleButton1Clicked;
}
void HandleButton1Clicked (object sender, EventArgs e)
{
About dialog = new About();
//dialog.Close += HandleAboutClose;
dialog.Destroyed += HandleAboutClose;
dialog.Show();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
void HandleAboutClose (object sender, EventArgs e)
{
Console.WriteLine("About Closed");
}
}
}
I needed to use
dialog.Destroyed += HandleAboutClose;
instead of
dialog.Close += HandleAboutClose;
I have a ascx page suppose page1.ascx in that I have a button click event handler
btnSave.Click +=
delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
and I have another ascx page suppose page2.ascx in that I have a button click
protected void btnEdit_Click(object sender, EventArgs e)
{
// want to call
btnSave.Click +=delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
}
I want to call btnSave click delegate(page1.ascx) on btnEdit(page2.ascx). Is it possible if yes then how?
If I understood what you mean, one way would be as below:
public class Control1 : UserControl {
public delegate void ButtonClickHandler(object sender, EventArgs e);
public ButtonClickHandler ButtonClickEvent {get;set;}
public void Save(object sender, EventArgs e) {
//do something
if (ButtonClickEvent != null) {ButtonClickEvent(sender, e);}
//do something
}
}
public class Control2 : UserControl {
protected override void OnLoad(EventArgs e) {
control1.ButtonClickEvent += YourMethod;
}
protected void YourMethod(object sender, EventArgs e) { // do something here ... }
}
Another way is to declare your button's event in the first control as property and assign your method in the 2nd control.
I'd recommend moving the code out of the code-behind and into a separate class, so that it is accessible to both .ascx files.