NotlmplementedException in Xamarin.Forms (PCL) project - xamarin

I'm programming in Xamarin.Forms (PCL), and I have seen many post but none one works for me. I'm using the plugin PhoneCall.Forms.Plugin
I made a method to call with a button that contains the next code
try
{
var PhoneCallTask = CrossMessaging.Current.PhoneDialer;
if (PhoneCallTask.CanMakePhoneCall)
{
PhoneCallTask.MakePhoneCall("+528331607211", "Laura");
}
else
{
DisplayAlert("Llamada", "No se puede hacer llamada", "Ok");
}
}
It throws an error:
System.NotlmplementedException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.

Have you tried the Messaging plugin?
Like said by Jason here.

Try:
Device.OpenUri(new Uri("tel:528331607211"));

If you are using this plugin (I'm not sure if this is the specific one you're using or not), then you need to make the phone call using a Dependency service (This is explained in the plugin readme).
Make a method in your PCL project to call the dependancy service:
private void QCall(string number)
{
var notificator = DependencyService.Get<IPhoneCall>();
notificator.MakeQuickCall (number);
}
In each platform specific project, initialize the plugin. For Android, this will be done in the OnCreate method of your MainActivity, and for iOS in AppDelegate in the FinishedLaunching method. Add the following line to each after the initialization of Xamarin.Forms:
PhoneCallImplementation.Init();
Then when you call QCall() in your PCL project it will run the code necessary for the specific platform the user is on.

Related

How to import Activity of NfcFCardEmulation.EnableService from Xamarin common project, not Android project?

I'm developing an app using Xamarin's HCE feature.
The project structure is as follows.
hceSample
hceSample.Android
hceSample.iOS
I am implementing hce simulation code called hceService in hceSample, not hceSample.Android.
A function called Enable_Card exists in the hce service, and you want to use the NfcFCardEmulation.EnableService function in that function.
Activity and ComponentName are requested as parameters of the function.
The ComponentName area was handled easily, but I don't know how to get the Activity. Please advise.
This is the contents of enable_Card function of hceService.
private Activity activity = null;
private bool enable_Card(cardModel card)
{
try
{
sid = card.cardSN;
tag = "Felica";
emulation.EnableService(, componentName); //<- How to get Activity??
emulation.SetNfcid2ForService(componentName, sid);
return true;
}
catch
{
return false;
}
}
This is my first time asking a question on Stackoverflow.
I would appreciate it if you could point out any missing or incorrect parts.
I trying this
activity = Xamarin.Essentials.Platform.CurrentActivity; //<- this function is not found!
Added missing information!
The namespace of the Enable_Card function is located in hceSample.Service.
Are you using the NfcFCardEmulation.EnableService(Activity, ComponentName) Method, right?
The method is an android api from android sdk,you cannot use it directly in xamarin.form(yours is hceSample) project.
If you want to call the function in xamarin form project(hceSample) from native platform(hceSample.Android, or hceSample.iOS),you can use Xamarin.Forms DependencyService to achieve this.
The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.
For more information about DependencyService, you can check document Xamarin.Forms DependencyService. And there is a sample included in above document,which is helpful for you to understand DependencyService.
Note:
We recognize that hardware service is the right and ideal way to
implement in each OS project. However, I'm curious if there is a way
to code Android and iOS at the same time
Since the api you used is from android sdk, you can call it in native android or use DependencyService to call it on xamarin.form(yours is hceSample) project.
If you call it on xamarin.form(yours is hceSample) project, you also need to find the the corresponding function or interface in iOS.

How to send a form/notification to user from a DLL?

I have a DLL .
And want to send a form/notification to user( who would reference my dll) from this dll.
I just want to build a function inside one class in my dll such that ->, once a user runs his local program and references my dll -> and calls a function that is present in dll -> that particular function should show up a form/notification to user ( from some functionalities in dll )? Is there any other method for the same?
I tried converting the library to a Winform from class library type and that would change its properties into an executable and it would demand presence of the main function. But I am just building a library that can be referenced and when called by the user produces a form at their end?
I tried toast notifications but not .show() method doesnt work inside new ToastContentBuilder() while i call that in a fucntion from my library . Otherwise as an idenpendant console app , it worked fine
Is there a way I can have a project for toast notification in the same solution as that of my library and then call from a class in my main library -> the function present in this different project for toast notif and would produce the toast notification.
You can try the following steps to send a notification to the user from the dll.
First, please create a class library(Target Framework is .NET Core 3.1) called TestDll.
Second, you could install the nuget-package Microsoft.Toolkit.Uwp.Notifications in the class library.
Third, you can add a class and refer to the following code example.
namespace TestDll
{
public class Example
{
public void Setnotifications()
{
new ToastContentBuilder().AddArgument("action", "viewConversation")
.AddArgument("conversationId", 9813)
.AddText("Ev2 Compiler Library")
.AddText("Allow sending build Data to Microsoft")
.AddButton(new ToastButton()
.SetContent("Yes")
.AddArgument("action", "reply")
.SetBackgroundActivation())
.AddButton(new ToastButton()
.SetContent("No")
.AddArgument("action", "like")
.SetBackgroundActivation())
.SetToastScenario(ToastScenario.Reminder)
.Show();
}
}
}
Fourth, please rebuild the class lib and create a console app(Target Framework is .NET Core 3.1).
Fifth, we can add the project reference TestDll to the console app and write the following example in the main method.
static void Main(string[] args)
{
Example example = new Example();
example.Setnotifications();
}
Finally, we can see the notification in the right bottom of computer like the following:
Besides, the problem about 'toast notifications but not .show() method' maybe that you used .net standard framework, you can solve the problem by changing to the .net core framwork.

Using an Android library that extends AppCompatDialog

So I'm trying to create a plugin that uses PrettyDialog (https://github.com/mjn1369/PrettyDialog) using the latest NativeScript seed.
However I've run into the following error when compiling:
Error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
That happens using the following code, and calling show() (TypeScript):
export class PrettyAlert {
show() {
const alert = this.createAlert();
}
createAlert(width?: number) {
return new libs.mjn.prettydialog.PrettyDialog(app.android.context);
}
}
I've been looking into the error here (pure Android): You need to use a Theme.AppCompat theme (or descendant) with this activity
But none of the solutions have worked.
I figure it possible somehow, but I'm new a plugin building, and I'm sure there's some quirks I need to understand.
There are similar plugins - fancyalert / cfalert already if you are not very choosy about PrettyDialog.
NativeScript introduced support for AppCompatActivity from v5.x which seems just hit live. You should bypass this error if you upgrade to latest version.

Xamarin BluetoothLE Plugin not working

I created a new Xamarin.Forms app to test basic Bluetooth functionality. I downloaded this plugin into both the Android project and the shared project:
https://github.com/aritchie/bluetoothle
I wrote this function in the shared project and am calling it from the OnCreate of my launch activity in my Android project:
public static async Task BroadcastBluetooth()
{
// (I do not await this function when I call it)
try
{
await Task.Delay(5000); // just to make sure we give enough time for all initialization to complete
_server = CrossBleAdapter.Current.CreateGattServer();
// exception thrown on this line
await _server.Start(new AdvertisementData
{
LocalName = "TestServer",
ServiceUuids = new List<Guid>()
});
}
catch (Exception e)
{
}
}
It throws this exception:
{System.NullReferenceException: Object reference not set to an
instance of an object. at
Plugin.BluetoothLE.Server.GattServer.StartAdvertising
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00095] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:135
at Plugin.BluetoothLE.Server.GattServer.Start
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00011] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:70
at App2.App+d__7.MoveNext () [0x00097] in
C:\Projects\app2\App2\App2\App.xaml.cs:49 }
I'm only doing something really basic so I must be missing something? The exception is referencing a directory path of the plugin's developer's machine (C:\dev\acr...) so either this plugin is broken or I'm doing something really wrong?
I am using the same plugin in a current project of mine and it is working fine. So I doubt the issue is within the plugin.
Some thoughts about what could cause the issue:
Are you testing this code on a real device that is capable of performing BLE Advertisement?
Do you have set the permissions accordingly in your android project?
Does BLE Advertising work when you use the native android apis?
It also would be helpful, if you could attach a repository with which I can reproduce the issue.

MvvmLight's SimpleIoc breaking in Xamarin Forms 1.3 Android and iOS app, but not in Windows Phone

I am working on a Xamarin Forms app and all is going well with the Windows Phone building and running. However when I try and run the Android version, it build OK and then fails and I get an exception when calling the ServiceLocator to resolve ViewModel in the ViewModelLocator.
Breaks on the line in the ViewModelLocator
return ServiceLocator.Current.GetInstance<MainViewModel>();
with
System.Reflection.TargetInvocationException
Source "mscorlib" string
StackTrace "at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Bind…"
and hovering over the 'GetInstance' shows
Could not resolve type: global::Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<global::hms.BillSplitter.ViewModel.PCL.MainViewModel>
My ViewModel's only constructor looks like
public MainViewModel(INavigationService navigationService, ICountryTippingService countryTippingService, AppSettings appSettings)
{
_navigationService = navigationService;
_countryTippingService = countryTippingService;
ThisAppSettings = appSettings;
ThisBillDetail = new BillDetail();
ThisBillDetail.TotalOnBill = 0;
}
All dependencies are registered ahead of this in the ViewModelLocator e.g.
SimpleIoc.Default.Register(() => new HmsPublicCoreMobileServiceClient(HmsCommonSettingConstants.HmsPublicCoreServiceUrl, HmsCommonSettingConstants.HmsPublicCoreServiceAppKey));
var prefService = ServiceLocator.Current.GetInstance<IPreferenceService>();
SimpleIoc.Default.Register(() => (SettingsHelper.GetCurrentSettings(prefService)));
SimpleIoc.Default.Register<MainViewModel>();
and some platform specific ones in the MainActivity.cs (Android) and AppDelegate(iOS) e.g.
SimpleIoc.Default.Register(() => new PreferenceService(this));
What I don't get is that it work beautifully in Windows Phone? What is it about Android that's different? Has anyone used SimpleIoc in Xamarin 1.3+?
Should I use a factory to create he ViewModel?
Any help would be great and much appreciated. I am using all the latest versions for MVVMLight (5.1.0.1) and Xamarin (1.3.3).
I finally worked out what the problem was and it's pretty basic and nothing to do with MvvmLight and/or Xamarin Forms updates!
I made the mistake of Registering a concrete class in a factory and then attempted to GetInstance on an Interface. SimpleIoC was not able to reconcile it.
from the code above
SimpleIoc.Default.Register(() => (SettingsHelper.GetCurrentSettings(prefService)));
should have been
SimpleIoc.Default.Register<IPreferenceService>(() => (SettingsHelper.GetCurrentSettings(prefService)));
so that the line
var prefService = ServiceLocator.Current.GetInstance<IPreferenceService>();
would know what I was talking about.
Anyhow, if you get errors like this, you'll know what to look for!!

Resources