Xamarin Forms IOS App fails on Database.Migrate() - xamarin

Good day,
I have a Xamarin Forms App that is cross platform for Android and IOS. I have developed and tested it on android for a while now and everything runs and works fine, but when I try to run the IOS on an emulator, the app keeps failing on the Database.Migrate line.
Please see below:
private readonly string _databasePath;
public DataContext()
{
Database.Migrate();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={Singletons.ApplicationSingleton.Instance.DatabasePath}");
}
The error I am getting is the following:
System.TypeLoadException: 'Could not resolve type with token 010000f8 from typeref (expected class 'System.ComponentModel.DataAnnotations.Schema.ColumnAttribute' in assembly 'System.ComponentModel.Annotations, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')'
I have had a look at all the references and all is the same versions and also removed the Database.Migrate line, but then it fails as soon as I try to access data in the database.
Has someone ever experienced this? Any solutions would be much appreciated.

Try to replace Database.Migrate(); with Database.EnsureCreated();in class constructor.
The offcial sample works fine on my side .
Refer to the source code https://github.com/dotnet/EntityFramework.Docs/blob/2466943d0fb22f4566e289643890baedddf37478/samples/core/Xamarin/EFGetStarted/Services/BloggingContext.cs#L17 .

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.

NotlmplementedException in Xamarin.Forms (PCL) project

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.

Xamarin: 'System.Resources.MissingManifestResourceException' on WinPhone

I found an issue with localization in WinPhone app. I added couple of resources and implemented localization. It is working fine in simulator, but crashing on Phone with exception Exception thrown: 'System.Resources.MissingManifestResourceException' in mscorlib.ni.dll
Any help?
Thanks to a bit of googling and a helpful blog post I have managed to solve this issue that happened intermittently with Xamarin.Forms projects.
This is the blog post that led me to the solution:
http://blog.tpcware.com/2016/06/xamarin-forms-localization/
Basically, the way you access resources on Android and iOS with Xamarin.Forms and Windows Phone is different:
To make it short, we need to “automagically” use the ResourceLoader.GetString(…) method when running on Store apps, while continuing to use the regular ResourceManager.GetString(…) method on all other platform. And because in the Xamarin Forms solution we use a resource file of type RESX, we also have the automatically generated resource class.
The super clever idea contained in the above linked post is to “hack” the resource class injecting a derived class of ResourceManager with an overridden GetString(…) method into the resource class “resourceMan” property (for a more detailed explanation of this hack, you can read the post).
We need to create our own version of ResourceManager like so and swap it for the existing ResourceManager using reflection:
public class WinRTResourceManager : ResourceManager
{
readonly ResourceLoader _resourceLoader;
private WinRTResourceManager(string baseName, Assembly assembly) : base(baseName, assembly)
{
_resourceLoader = ResourceLoader.GetForViewIndependentUse(baseName);
}
public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
{
resxGeneratedApplicationResourcesClass
.GetRuntimeFields()
.First(m => m.Name == "resourceMan")
.SetValue(null, new WinRTResourceManager(
resxGeneratedApplicationResourcesClass.FullName,
resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
}
public override string GetString(string name, CultureInfo culture)
{
return _resourceLoader.GetString(name);
}
}
All that's then left to do is call this when the app starts for the first time:
WinRTResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(typeof(AppResources));
After making these changes everything should work fine now.
Of course this is absolutely a hack but I have notified the Xamarin.Forms team of the issue and they are looking into it so hopefully it will be solved soon!

Java Binding: The type `...' does not exist in the namespace `...'. Are you missing an assembly?

I am trying to create a Xamarin Java Binding to the Spotify Android SDK. The SDK is now separated into two parts, one for authentication and one for the player. The former java binding works, however, the seconds gives me an error.
The original question was asked on the Xamarin Forums.
Hi,
I am trying to create a binding project for the Spotify Android SDK.
The SDK is seperated into two .aar files. One for authentication and one for media playback (Player).
Firstly I tried having both .aar files in one Binding Project, but the Player.aar was ignored. However, moving it to its own seem to work.
Now, my issue is related to the Java Interface NativePlayerNotificationCallback which is generated to IPlayerNotificationCallback (hence the lack og Notification), but in the Player class it tried to implement: global::Com.Spotify.Android.Player.INativePlayerNotificationCallback.
I can find no other mention of INativePlayerNotificationCallback in the decompiled files. Only IPlayerNotificationCallback.
I understand that this is a bit difficult to imagine. Here are the java class files seen in JD-GUI:
The generated files are listed here:
Inside the file Com.Spotify.Sdk.Android.Player.IPlayerNotificationCallback.cs:
And the error message itself
Error CS0234: The type or namespace name INativePlayerNotificationCallback' does not exist in the namespaceCom.Spotify.Sdk.Android.Player'. Are you missing an assembly reference?
I would really appreciate any insight as to how I can get this to work. It looks to me like there are some inconsistencies in the naming of the interface, but I am not sure.
Thank you for helping out,
Fredrik
Should be fixed by adding metadata to Player binding project:
<metadata>
<attr path="/api/package[#name='com.spotify.sdk.android.player']/interface[#name='NativePlayerNotificationCallback']" name="visibility">public</attr>
</metadata>
and Player class extension (into the Additions directory):
using System.Collections;
using Java.Lang;
using Java.Util.Concurrent;
namespace Com.Spotify.Sdk.Android.Player
{
public partial class Player
{
public IList InvokeAll(ICollection tasks)
{
return null;
}
public IList InvokeAll(ICollection tasks, long timeout, TimeUnit unit)
{
return null;
}
public Object InvokeAny(ICollection tasks)
{
return null;
}
public Object InvokeAny(ICollection tasks, long timeout, TimeUnit unit)
{
return null;
}
}
}
You will probably need to implement these methods correctly by calling generic methods.
Also I had to add metadata to Auth library binding project (I found it in your old topics) and referenced Auth project from Player project as it uses some of the classes (maybe that's no necessary).

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