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

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));
}
}

Related

EventBus Xamaring. Custom method of activity not found using reflection

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.

Xamarin MediaPlugin MediaFile null but image is written to path

I am having a strange issue where by the below code returns null, but if I look in the folder, the image jpg has saved to file
public partial class TaskPage : ContentPage
{
private async void TaskPageTaskAnswer_Clicked(object sender, EventArgs e)
{
MediaFile mediaFile = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { PhotoSize = PhotoSize.MaxWidthHeight, MaxWidthHeight = 1024 });
}
permissions checking is just this at the moment after various attempts
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Forms.Init(this, savedInstanceState);
FormsMaterial.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Also I am having trouble getting the Permissions plugin to return after it has requested permissions. They may be related issues?
I have followed all the steps but there seems to be contrasting info regarding the setup. Some places say to have a MainApplication.cs in the android project, yet the sample for this doesn't have that?

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

Dependency Injection too late

I am using JavaFX 2 in conjunction with the Spring Framework, however the injection happens to late. My Controller is instanciated by the FXML-Loader, the Spring injection for member variables of this Controller works, but it works only too late, this means that in (1) injection yet didn't happen and in (2) injection did happen:
public class MainController extends AbstractController
{
#Autowired
public StatusBarController statusbarController;
// Implementing Initializable Interface no longer required according to
// http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm:
private void initialize() {
BorderPane borderPane = (BorderPane)getView();
borderPane.setBottom(statusbarController.getView()); // (1) null exception!
}
// Linked to a button in the view
public void sayHello() {
BorderPane borderPane = (BorderPane)getView();
borderPane.setBottom(statusbarController.getView()); // (2) works!
}
}
Any way to let Spring inject statusbarController in an earlier state? I can't let the user have to click a button to load my GUI ;-)
My AppFactory is this:
#Configuration
public class AppFactory
{
#Bean
public MainController mainController() throws IOException
{
return (MainController) loadController("/main.fxml");
}
protected Object loadController(String url) throws IOException
{
InputStream fxmlStream = null;
try
{
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = new FXMLLoader();
Node view = (Node) loader.load(fxmlStream);
AbstractController controller = (AbstractController) loader.getController();
controller.setView(view);
return controller;
}
finally
{
if (fxmlStream != null)
{
fxmlStream.close();
}
}
}
}
You should set a ControllerFactory on the FXMLLoader so that you are in charge of creating the controller instance.

Resources