Xamarin MediaPlugin MediaFile null but image is written to path - xamarin

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?

Related

Error when changing the style to dark from the system

I have an error, when changing the style to dark from the system.
error in MainActivity.cs
base.OnCreate(savedInstanceState);
error text:
System.NotSupportedException: 'Unable to find the default constructor on type Xamarin.Forms.Platform.Android.ShellItemRenderer.Please provide the missing constructor.'
The function:
protected override void OnCreate(Bundle savedInstanceState)
{
CrossFingerprint.SetCurrentActivityResolver(() => this);
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState); **// in this line**
Window.DecorView.LayoutDirection = LayoutDirection.Rtl;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsGoogleMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}

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.

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

Can not resolve reference Plugin.Permissions

I get this error when use of Plugin.Permissions in xamarin.forms:
Can not resolve reference: Plugin.Permissions, referenced by MyProject. Please add a NuGet package or assembly reference for Plugin.Permissions, or remove the reference to MyProject. MyProject.Android
but I added plugin.permission in all Projects (Forms, android, ios)
I added this method in MainActivity:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
and added after base.OnCreate(savedInstanceState) below line:
protected override void OnCreate(Bundle savedInstanceState)
{
var intent = new Intent(this, typeof(MainService));
StartService(intent);
layout = FindViewById<LinearLayout>(Resource.Layout.AlertActivity);
Log.Debug("StartService", "DemoService started");
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
//this line added
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this,
savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
Now, everything works fine!
thanks for your helps!

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

Resources