EventBus Xamaring. Custom method of activity not found using reflection - xamarin

I've added EventBus as a binding library in my Xamarin Android project.
I can compile without errors but when I'm trying to register, I get the following error:
Here is the code for the activity
[Activity(Icon = "#mipmap/ic_launcher"]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Forms.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
LoadApplication(new App());
EventBus.Default.Register(this);
}
protected override void OnDestroy()
{
base.OnDestroy();
EventBus.Default.Unregister(this);
}
[Subscribe]
public void onEvent(DeviceConnectionEvent my_event)
{
throw new NotImplementedException();
}
protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
{
if (requestCode == 1)
{
wifiManagerInstance.onNetworkDataReceived();
}
base.OnActivityResult(requestCode, resultVal, data);
}
}
It looks like my method is not being detected when eventbus uses reflection
methods = findState.clazz.getDeclaredMethods();
Other methods of the activity are detected as can be seen in the following picture.
Is there anything I can do?
Thanks in advance.

Since I don't have a binding library for EventBus, I couldn't test it, but
you could check to see if your package is imported correctly when you use [Subscribe] attribute.
Here you should use org.greenrobot.eventbus.Subscribe; rather than com.google.common.eventbus.Subscribe;
I think you could also use MessagingCenter to do what you want.

Related

CS1729 'Application' does not contain a constructor that takes 1 arguments

I created a OAuth2Service class in my Xamarin Forms Android Project which inherits a IOAuth2Service interface.I need this to be available in my class library so i can perform google authentication in my Xamarin forms Application. But I keep getting the error message mentioned above despite the fact that i setup the application class to accept an argument of type IOAuth2Service. Here's my "App.Xaml.cs" which contains the Application class. What should i do?
public partial class Application : Xamarin.Forms.Application
{
public Application(IOAuth2Service oAuth2Service)
{
InitializeComponent();
MainPage = new NavigationPage(new LoginPage(oAuth2Service));
}
protected override void OnStart()
{
AppCenter.Start("android=ced94d85-cb85-4ec2-8411-96864a7ec23e;" +
"uwp={Your UWP App secret here};" +
"ios={Your iOS App secret here}",
typeof(Analytics), typeof(Crashes));
}
}
The IOAuth2Service is actually a Interface and not a class. I don't think you can create an instance object from an interface. And i'm correct cause after making the changes you suggested. I get the error:"Cannot create an instance of the abstract class or interface 'IOAuth2Service'"
Why not the instantiate the OAuth2Service class in the Constructor of Applicationlike following code? Put OAuth2Service class in PCL.
public Application()
{
InitializeComponent();
OAuth2Service oAuth2Service=new OAuth2Service ();
MainPage = new NavigationPage(new LoginPage(oAuth2Service));
}
If IOAuth2Service in the xxxxx.Droid or xxxxx.IOS folder. You need transfer IOAuth2Service to the PCL. In Android, You can instantiate it in OnCreate method of MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
OAuth2Service oAuth2Service = new OAuth2Service ();
LoadApplication(new App(oAuth2Service));
}
In IOS, You can instantiate it in FinishedLaunching method of AppDelegate.cs
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
OAuth2Service oAuth2Service = new OAuth2Service ();
LoadApplication(new App(oAuth2Service));
return base.FinishedLaunching(app, options);
}
Here is Application class.
public partial class Application : Xamarin.Forms.Application
{
public Application(OAuth2Service oAuth2Service)
{
InitializeComponent();
MainPage = new NavigationPage(new LoginPage(oAuth2Service));
}
}

Xamarin.Forms maps on MVVMCross

I want to use maps with MVVMCross. In iOS everything is OK, but in Android I don't have Bundle in the OnCreate() method, so I don't know where I should initialize Xamarin.FormsMaps.Init(this, bundle);
My MainApplication.cs looks like this:
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
public MainApplication(IntPtr handle, JniHandleOwnership transer)
: base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
//A great place to initialize Xamarin.Insights and Dependency Services!
}
public override void OnTerminate()
{
base.OnTerminate();
UnregisterActivityLifecycleCallbacks(this);
}
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
}
public void OnActivityDestroyed(Activity activity)
{
}
public void OnActivityPaused(Activity activity)
{
}
public void OnActivityResumed(Activity activity)
{
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
}
public void OnActivityStopped(Activity activity)
{
}
}
I don't know if I have to create another view or something like that.
Any thoughs?
I think, You can override the onCreate Method that will accept the instance details.
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
RegisterActivityLifecycleCallbacks(this);
global::Xamarin.FormsMaps.Init (this, bundle);
}

App does not contain a definition for UiParent

I've got an Azure AD B2C set up, and have configured external identity providers. I'm trying to follow this tutorial and have gotten stuck right here MainActivity.cs (Android project in my Xamarin solution):
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
LoadApplication(new App());
App.UiParent = new UIParent(Xamarin.Forms.Forms.Context as Activity);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
}
I'm getting red squiggled on App.UiParent, saying "UiParent" is not on App.
I've added the 1.1 release of Microsoft Authentication Library / Microsoft.Identity.Client, and even futzed around with the dev releases and got the same result.
As a bonus, I'm also getting Forms.Context is obsolete.
You are missing the public variable in your Application subclass:
public class App : Application
{
public static IAuthenticate AuthenticationProvider { get; private set; }
public static UIParent UiParent = null;
public App()
{
~~~~
Re: App.cs#L12

Error inflating Mvx.MvxLinearLayout after updating to MvvmCross 4.2.2 from 3.5.1

I just updated my project from MvvmCross 3.5.1 stable to 4.2.2. After fixing some other runtime exceptions that popped up after the update, I'm stuck with this one.
I am inflating a layout in an MvxFragment:
_rootView = this.BindingInflate(Resource.Layout.my_layout, null);
This throws a Java.Lang.ClassNotFoundException for Mvx.MvxLinearLayout. With the messages:
Binary XML file line #1: Error inflating class Mvx.MvxLinearLayout
Didn't find class \"Mvx.MvxLinearLayout\" on path: DexPathList[[zip file \"/data/app/com.myapp…
I have already installed the MvvmCross.Binding nuget package.
I the following base activity (which worked fine on 3.5.1):
MvxActionBarActivity
/// <summary>
/// Mvx support for the native ActionBarActivity
/// </summary>
public abstract class MvxActionBarActivity
: MvxActionBarEventSourceActivity
, IMvxAndroidView
{
protected MvxActionBarActivity()
{
BindingContext = new MvxAndroidBindingContext(this, this);
this.AddEventListeners();
}
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
public IMvxViewModel ViewModel
{
get { return DataContext as IMvxViewModel; }
set
{
DataContext = value;
OnViewModelSet();
}
}
public void MvxInternalStartActivityForResult(Intent intent, int requestCode)
{
base.StartActivityForResult(intent, requestCode);
}
public IMvxBindingContext BindingContext { get; set; }
public override void SetContentView(int layoutResId)
{
var view = this.BindingInflate(layoutResId, null);
SetContentView(view);
}
protected virtual void OnViewModelSet()
{
}
MvxActionBarEventSourceActivity
public class MvxActionBarEventSourceActivity : AppCompatActivity
, IMvxEventSourceActivity
{
protected override void OnCreate(Bundle bundle)
{
CreateWillBeCalled.Raise(this, bundle);
base.OnCreate(bundle);
CreateCalled.Raise(this, bundle);
}
protected override void OnDestroy()
{
DestroyCalled.Raise(this);
base.OnDestroy();
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
NewIntentCalled.Raise(this, intent);
}
protected override void OnResume()
{
base.OnResume();
ResumeCalled.Raise(this);
}
protected override void OnPause()
{
PauseCalled.Raise(this);
base.OnPause();
}
protected override void OnStart()
{
base.OnStart();
StartCalled.Raise(this);
}
protected override void OnRestart()
{
base.OnRestart();
RestartCalled.Raise(this);
}
protected override void OnStop()
{
StopCalled.Raise(this);
base.OnStop();
}
public override void StartActivityForResult(Intent intent, int requestCode)
{
StartActivityForResultCalled.Raise(this, new MvxStartActivityForResultParameters(intent, requestCode));
base.StartActivityForResult(intent, requestCode);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
ActivityResultCalled.Raise(this, new MvxActivityResultParameters(requestCode, resultCode, data));
base.OnActivityResult(requestCode, resultCode, data);
}
protected override void OnSaveInstanceState(Bundle outState)
{
SaveInstanceStateCalled.Raise(this, outState);
base.OnSaveInstanceState(outState);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeCalled.Raise(this);
}
base.Dispose(disposing);
}
public event EventHandler DisposeCalled;
public event EventHandler<MvxValueEventArgs<Bundle>> CreateWillBeCalled;
public event EventHandler<MvxValueEventArgs<Bundle>> CreateCalled;
public event EventHandler DestroyCalled;
public event EventHandler<MvxValueEventArgs<Intent>> NewIntentCalled;
public event EventHandler ResumeCalled;
public event EventHandler PauseCalled;
public event EventHandler StartCalled;
public event EventHandler RestartCalled;
public event EventHandler StopCalled;
public event EventHandler<MvxValueEventArgs<Bundle>> SaveInstanceStateCalled;
public event EventHandler<MvxValueEventArgs<MvxStartActivityForResultParameters>> StartActivityForResultCalled;
public event EventHandler<MvxValueEventArgs<MvxActivityResultParameters>> ActivityResultCalled;
}
Switching my base activity to MvxAppCompatActivity fixed the issue.

MVC Override OnException Error: No suitable method found to override

I'm trying to override OnException in Global.asax to handle error and writing log. I'm not sure which part is wrong, I keep on getting the error "MyApp.MvcApplication.OnException(System.Web.Mvc.ExceptionContext)': no suitable method found to override" whenever I rebuild my solution.
this is the code I have in Application_Start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
And this is the code I have in OnException()
protected override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (!string.IsNullOrEmpty(ex.Message) ||
!string.IsNullOrEmpty(ex.Source.ToString()) ||
!string.IsNullOrEmpty(ex.StackTrace))
{
WriteLog(ex.Message.ToString(), ex.StackTrace.ToString(), ex.Source.ToString(), "0");
context.Result = new ViewResult
{
ViewName = String.Format("~/ErrorPage/ErrorPage?message={0}&stack={1}&source={2}", HttpUtility.UrlEncode(ex.Message), HttpUtility.UrlEncode(ex.StackTrace), HttpUtility.UrlEncode(ex.Source))
};
}
context.ExceptionHandled = true;
}
The WriteLog() function is tested working in other application, I don't think there's any problem in it and I even tried:
protected override void OnException(ExceptionContext context) {
Exception ex = context.Exception;
context.Result = new ViewResult
{
ViewName = "~/Shared/Error.cshtml";
};
context.ExceptionHandled = true;
}
But nothing work. The error just remain there.
How could such problem occur and how do I fix it? I read many tutorial about this, I don't think I'm spelling OnException() wrongly.
Please help. Thanks
There isn't any OnException inside Global.asax
You have two ways:
Create your own HandleErrorAttribute and register in FilterConfig.cs
public class HandleExceptionsAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
(...)
}
}
FilterConfig.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleExceptionsAttribute());
(...)
}
Or, if you have a BaseController where all controllers are inherited from, override the OnException method.
PS: I would go for the filter one.
There is no method OnException in the global asax, that method belongs to the controllers.to handle errors in the global asax use the method Application_Error(object sender, EventArgs e)

Resources