My problem is whenever i try to override OnBackButtonPressed visual studio just tells me that there are no method that can be override.
Here is the code
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity {
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults){
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override bool OnBackButtonPressed() {
return true;
}
}
}
whenever i remove "override" tag the error disapper but the evente is never triggered.
Any idea why cant override this method?
OnBackPressed is an Android method, you can override it in the file MainActivity.cs this way:
public override void OnBackPressed()
{
// your code over here
base.OnBackPressed();
}
Related
namespace FlexIT
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = false)]
public class MainActivity : AppCompatActivity
{
protected MainActivity(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
This is my code this say the error is The type 'FlexIT.MainActivity' must provide a public default constructor
You should remove the below codes:
protected MainActivity(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
So your MainActivity will like this:
namespace FlexIT
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = false)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
Does anybody face this problem? I'm trying to implement MVVM using MVVMCross in Xamarin.iOS project, but in ViewController only constructor was called, neither ViewDidLoad nor ViewWillAppear. What's wrong? Or may be I miss something? Thank you.
public partial class VideosViewController : MvxViewController<VideoListViewModel>
{
public VideosViewController() : base(nameof(VideosViewController), null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
}
public class VideoListViewModel : MvxViewModel
{
private readonly IVideoService _videoService;
public CancellationTokenSource TokenSource => new CancellationTokenSource();
public VideoListViewModel(IVideoService videoService)
{
_videoService = videoService;
}
public override async Task Initialize()
{
await base.Initialize();
}
}
I have to controller A and B.
From A I'm navigating to B in the B on button click I have open another ViewController using PresentModalViewController, which ask the user to enter some data. On the click of the submit button on that modally presented view I want to dismiss the modal and show the data on the B view that captured in modally presented view.
Here is what I have done:
I have InviteCollaboratorViewController from this I have modally presented AddNewCollaboratorDialogPopUpView and from there again I have modally presented another view named AddCollaboratorPopUpView.
AddCollaboratorPopUpView user will fill the details of and click on addCollaboratorButton, on this button click event I want to dismiss both the modally presented views, and want to reflects the changes in InviteCollaboratorViewController tableview.
InviteCollaboratorViewController
public partial class InviteCollaboratorViewController : UIViewController
{
public override void ViewDidLoad()
{
btnNewDialog.TouchUpInside += BtnNewDialog_TouchUpInside;
MyCollaboratorsTableView.Source = new MyCollaboratorsTableViewSource(this, _listOfMyCollaborators);
MyCollaboratorsTableView.RowHeight = UITableView.AutomaticDimension;
MyCollaboratorsTableView.ReloadData();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
MyCollaboratorsTableView.ReloadData();
}
public void BtnNewDialog_TouchUpInside(object sender, EventArgs e)
{
AddNewCollaboratorDialogPopUpView addNewCollaboratorDialogPop = this.Storyboard.InstantiateViewController("AddNewCollaboratorDialogPopUpView") as AddNewCollaboratorDialogPopUpView;
if (addNewCollaboratorDialogPop != null)
{
addNewCollaboratorDialogPop.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
addNewCollaboratorDialogPop.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
this.PresentViewController(addNewCollaboratorDialogPop, true, null);
}
}
}
AddNewCollaboratorDialogPopUpView
public partial class AddNewCollaboratorDialogPopUpView : UIViewController
{
public AddNewCollaboratorDialogPopUpView (IntPtr handle) : base (handle) { }
public override void ViewDidLoad()
{
base.ViewDidLoad();
SET_FONTS();
SET_UI_CONTROLS_STYLING();
createNewCollaboratorButton.TouchUpInside += CreateNewCollaboratorButton_TouchUpInside;
}
public void CreateNewCollaboratorButton_TouchUpInside(object sender, EventArgs e)
{
AddCollaboratorPopUpView addCollaboratorPopUp = this.Storyboard.InstantiateViewController("AddCollaboratorPopUpView") as AddCollaboratorPopUpView;
if (addCollaboratorPopUp != null)
{
addCollaboratorPopUp.ModalPresentationStyle = UIModalPresentationStyle.Popover;
addCollaboratorPopUp.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
this.PresentViewController(addCollaboratorPopUp, true, null);
}
}
}
AddCollaboratorPopUpView
public partial class AddCollaboratorPopUpView : UIViewController
{
public AddCollaboratorPopUpView (IntPtr handle) : base (handle) { }
public override void ViewDidLoad()
{
base.ViewDidLoad();
textFieldFirstName.ShouldEndEditing += TextFieldFirstName_ShouldEndEditing;
textFieldLastName.ShouldEndEditing += TextFieldLastName_ShouldEndEditing;
textFieldPhoneNumber.ShouldEndEditing += TextFieldPhoneNumber_ShouldEndEditing;
textFieldEmail.ShouldEndEditing += TextFieldEmail_ShouldEndEditing;
dismissPopupButton.TouchUpInside += DismissPopupButton_TouchUpInside;
addCollaboratorButton.TouchUpInside += AddCollaboratorButton_TouchUpInside;
}
public void DismissPopupButton_TouchUpInside(object sender, EventArgs e)
{
PresentingViewController?.PresentingViewController?.DismissModalViewController(false);
}
public void AddCollaboratorButton_TouchUpInside(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textFieldFirstName.Text) && !string.IsNullOrEmpty(textFieldLastName.Text)
&& !string.IsNullOrEmpty(textFieldPhoneNumber.Text) && !string.IsNullOrEmpty(textFieldEmail.Text))
{
//Now from here what I want is to call the InviteCollaboratorViewController's ViewWillAppear() because that Refresh's the list of
//users which reflect the added user on list instantly as popup get dismissed.
collaborator.FirstName = textFieldFirstName.Text;
collaborator.LastName = textFieldLastName.Text;
collaborator.Email = textFieldEmail.Text;
collaborator.PhoneNumber = textFieldPhoneNumber.Text;
GlobalConstants.listOfMyCollaborators.Add(collaborator);
PresentingViewController.PresentingViewController.DismissModalViewController(false);
}
else
{
Toast.MakeToast("Please fill all required fields before submitting.").Show();
}
}
}
Try code below, I have made few modifications. It might help you
InviteCollaboratorViewController.cs
public override void ViewDidLoad()
{
btnNewDialog.TouchUpInside += BtnNewDialog_TouchUpInside;
_listOfMyCollaborators.Add(GlobalConstants.Collaborators);
GlobalConstants.Collaborators=null;
MyCollaboratorsTableView.Source = new MyCollaboratorsTableViewSource(this, _listOfMyCollaborators);
MyCollaboratorsTableView.RowHeight = UITableView.AutomaticDimension;
MyCollaboratorsTableView.ReloadData();
}
AddCollaboratorPopUpView.cs
public void AddCollaboratorButton_TouchUpInside(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textFieldFirstName.Text) && !string.IsNullOrEmpty(textFieldLastName.Text)
&& !string.IsNullOrEmpty(textFieldPhoneNumber.Text) && !string.IsNullOrEmpty(textFieldEmail.Text))
{
var objCollab=new Collaborators();
objCollab.FirstName = textFieldFirstName.Text;
objCollab.LastName = textFieldLastName.Text;
objCollab.Email = textFieldEmail.Text;
objCollab.PhoneNumber = textFieldPhoneNumber.Text;
GlobalConstants.Collaborators=objCollab;
PresentingViewController.PresentingViewController.DismissModalViewController(false);
}
}
GlobalConstants.cs
Public class GlobalConstants
{
public static Collaborators myCollab {get;set;}
}
I am trying to implement a ContextMenu from a button in the titlebar, but it doesn't appear
to be working. I register the button, but when I click it, nothing
is happening. Any ideas? I'm using MonoDroid 1.2. Thanks.
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.main);
Button btnMenu =
(Button)FindViewById(Resource.Id.btn_menu_options);
RegisterForContextMenu(btnMenu);
}
public override void OnCreateContextMenu(IContextMenu menu, View v,
IContextMenuContextMenuInfo menuInfo)
{
base.OnCreateContextMenu(menu, v, menuInfo);
menu.SetHeaderTitle(Resource.String.menu_title);
menu.Add(Resource.String.menu_option1);
}
}
What do you mean under "button in the titlebar"? I have just created demo project and all works fine. Here is a code:
[Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.MyButton);
RegisterForContextMenu(button);
}
public override void OnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
{
base.OnCreateContextMenu(menu, v, menuInfo);
menu.SetHeaderTitle(Resource.String.MenuTitle);
menu.Add(Resource.String.Action1);
menu.Add(Resource.String.Action2);
}
public override bool OnContextItemSelected(IMenuItem item)
{
Toast.MakeText(this, item.TitleFormatted, ToastLength.Short).Show();
return true;
}
}
I believe the context menu is a long press. Just pressing the button normally won't activate it. You need to do a long press.
I've searched high and low and I can't seem to find a straight answer.
If I have a custom attribute/filter, will the OnActionExecuted method always be called? Even if there is an exception thrown?
At least with MVC 5, #tvanfosson's answer is no longer correct. This may apply to earlier versions as well.
OnActionExecuted is always called and has access to the thrown exception via filterContext.Exception.
Test case with exception in an action:
public class HomeController
: Controller
{
public ActionResult Index()
{
throw new Exception("Index");
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}
public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
Output:
Bar.OnActionExecuting
Foo.OnActionExecuting
Exception thrown: 'System.Exception' in WebApplication1.dll
Foo.OnActionExecuted
Has exception: True
Bar.OnActionExecuted
Has exception: True
Test case with exception in a filter
public class HomeController
: Controller
{
public ActionResult Index()
{
return new HttpStatusCodeResult(200);
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}
public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
throw new Exception("Foo");
base.OnActionExecuted(filterContext);
}
}
public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
Output:
Bar.OnActionExecuting
Foo.OnActionExecuting
Foo.OnActionExecuted
Has exception: False
Exception thrown: 'System.Exception' in WebApplication1.dll
Bar.OnActionExecuted
Has exception: True