Xamarin: 'System.Resources.MissingManifestResourceException' on WinPhone - xamarin

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!

Related

Xamarin Forms IOS App fails on Database.Migrate()

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 .

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!!

MVVM-light There is already a factory registered for INavigationService

I'm trying to adjust my WP8 project from self made MVVM implementation to MVVM Light.
The application compiles without errors, but when I open my MainPage.xaml in Expression Blend, I will get this error:
Class project.Services.INavigationService is already registered. App.xaml
My ViewModelLocator.cs:
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
}
else
{
if (!SimpleIoc.Default.IsRegistered<INavigationService>())
{
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
}
}
SimpleIoc.Default.Register<MainPage>();
SimpleIoc.Default.Register<SettingsEditViewModel>();
}
As you can see from my code comment, I've already tried the fix supposed here, but I'm still getting this error in Blend. There is no other place left where I could register the INavigationService, so what could be the problem?
Any ideas? :)
I had the same issue, and this seems to be a Visual Studio issue in combination with XAML-Designer, Static Factories/Locators and Design-Time creation of objects. However: The solutions were the following:
Register without a factory (not recommended)
SimpleIoc.Default.Register<INavigationService>();
Or if you want to use a factory, unregister before registering the factory
SimpleIoc.Default.Unregister<INavigationService>();
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
Prevent the ViewModelLocator from being created more than once by the designer/Blend by making the constructor static
static ViewModelLocator() { ... }
The error is cumbersome but could happen in this scenario: You create objects during design-time (the ViewModelLocator within App.xaml probably) and whenever you change something in your Code, the Compiler is triggered and the ViewModelLocator gets re-created without ever unregistering the services. Therefore it will complain that in the factory has already been registered. In theory, when registering classes without factories multiple times, there should be an error as-well.
Might already be solved by now, but I think you can just solve the issue in your example case by not using the factory method override.
SimpleIoc.Default.Register<INavigationService, NavigationService>();
If you do need a factory method, then you can mix this line in with your factory method approach using the design mode check like you have been.
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<INavigationService, NavigationService>();
}
else
{
SimpleIoc.Default.Register<INavigationService>(CreateNavigationService);
}
This seems like a super old question, but after hours of googling, this is the only question that's similar to my problem, so answering here for other people.
Make sure that you have IsDataSource on your ViewModelLocator:
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
That solved my problem and my design data popped up straightaway.
Weirdly, after putting that on, all my "INavigationService is already registered" errors goes away!
Not sure why, but I only get this error when my xaml designer window is open. When I compile with it closed, the error goes away and the project is built and runs successfully.

Monodroid emulator retrieving location problem

I am having difficulty getting my Monodroid application retrieve a location when I run it in an emulator. My code looks something like this:
LocationManager locationsManager = (LocationManager)threadSurfaceView.Context.GetSystemService(Context.LocationService);
location = locationsManager.GetLastKnownLocation(Android.Content.Context.LocationService);
However, this always seems to return null. Do I have to configure the emulator in some way so that it has a locaion service and/or location?
I also tried adding a LocationListener:
locationListener = new MyLocationListener();
locationsManager.RequestLocationUpdates(LocationManager.GpsProvider, 120000, 0, locationListener);
But the problem I had here was to create the MyLocationListener class. I implemented the four public methods (OnLocationChanged(Location) and so on), but Visual Studio complained about a missing Android.Runtime.IJavaObject method - does anyone have a simple example of a class that implements ILocationListener?
Thanks for any help. Martin
I got a kindly link to a sample at
https://github.com/gshackles/Sample-Projects/blob/master/MonoDroid/MonoDroidSamples/MonoDroidSamples/DemoActivities/LocationDemo/LocationActivity.cs
Which works fine on my Nexus S but still will not work on the emulator - so (perhaps) the issue is not a code issue.
I liked the technique in this sample of getting the "Activity" to implement ILocationListener as well as Activity - a neat solution to getting data back out of the "listener" function which had been giving me headaches when it was wrapped into another class.
I downloaded the bundle of samples and if you do the same then you will need to grab an mp3 file - rename it volbeat.mp3 and add it to the "raw" folder within "Resources" as it is missing.
partial answer as I am working on the same problem.
Your listener class that implements ILocationListener needs to be declared like
public class myLocationListener : Java.Lang.Object, ILOcationListener
{
//plus the public functions you identified
}
My attempt looks like this:
Android.Locations.Location iAmHere;
LocationManager myLoc = (LocationManager)GetSystemservice(Context.LocationService);
iAmHere = myLoc.GetLastKnownLocation(Android.Content.Context.LocationService);
but iAmHere is null on the emulator even when I have used TelNet to push a geo fix location through

Resources