Xamarin Forms Prism Can't access PCL classes from android project - xamarin.forms

I'm using Xamarin Forms Prism and I can't use my PCL classes in android project.
common example :
here is the Interface in PCL
namespace BlankApp.Helpers
{
public interface IToast
{
void MakeLongToast(string msg);
}
}
and here is the class in android project
namespace BlankApp2.Droid.DependencyService
{
public class ToastImp : IToast
{
}
}
it can not find IToast Interface reference !
there is a suggest from IDE with this msg : "Reference 'projectname-WebAppMAinModule' and use 'projectname.Helpers.IToast' "
screenshot
which does nothing actually !
I don't have these kind of problems in XamarinForms I face theme while using Prism . Do I forget something in referencing my PCL ?
I'm using
Prism.Forms (7.0.0.396)
Xamarin.Forms (2.5.0.122203)
also there are my project dependencies
here

You should be able to implement PCL interfaces and use its classes, etc. in your platform specific code (Android included).
Use the following steps:
Make sure your Android project is referencing your PCL.
Delete your Android obj & bin folders.
Recompile your PCL
After recompiling your PCL, recompile your Android project
See if it recognizes your interface now.
If the above doesn't work, then as one last option is:
Close the solution
Re-open the solution
Both of these typically work when your project won't recognize a newly created Interface.

Related

What does "public App() : this(null) { }" do when using Xamarin Forms with Prism

So I was jsut wondering why in the Prism Doc and VS template this line is included in the App Class.
public App() : this(null) { }
Since today I commented it out and the App still started without any issues in both iOs and Android.
Best Regards
Basecrusher
If you don't need/want an IPlatformInitializer, it's fine to pass null, and the default implementation does so.
I guess the code is in there to remind you that you could pass an IPlatformInitializer if you needed/wanted to.
"With Xamarin.Forms you may have read how you can add the Dependency attribute for an impelementing type in your Platform Specific code and then resolve it with the Xamarin.Forms DependencyService. This is considered a major Anti-Pattern that should be avoided when you are using a proper Dependency Injection container. It is for this reason that Prism has dropped all support for working with the DependencyService as of Prism 7.0. Starting with Prism 6.3 the IPlatformInitializer was introduced. This allows you to easily register types with Prism's container."
https://prismlibrary.com/docs/xamarin-forms/dependency-injection/platform-specific-services.html

Can't Resolve Platform Effect in Xamarin Forms component

I am having issues with Platform Effects in Xamarin Forms. These effects worked in a shared library format, and we are now migrating to .NET Standard 2.0 for reasons outside of the scope of this post.
Project Setup
VS 2017 15.8.0
Xamarin Forms 3.1.0.697729
Two .Net Standard 2.0 projects (one is for components, the other for the main UI)
Two Android projects (one that runs, the other is an Android .dll)
Two iOS projects (same deal)
Main Issue
None of my platform effects in the Android .dll work. They all resolve as null effect.
According to the documentation, my setup is correct. (Documentation here)
I found two different issues and have hit a brick wall.
When I follow the instructions exactly and place [assembly: ResolutionGroupName("MyGroupName")] on the platform renderer itself, I get exceptions with Xamarin's dependency resolver. There is no message, just an exception and stack trace that leads back to the constructor of the RoutingEffect.
When I use a slightly different pattern and register that on the namespace of an empty file in my .NET Standard 2.0 project, all effects return as null effect.
I've been Googling and digging through forums to no avail. Does anyone have an idea what could be the problem?
Side notes: All of the custom renderers work after using the static Init() trick, but not the effects. I have also tried making the typeof() statement reference both the Android version of the class and the .NET Standard 2.0 version of the class.
For the sake of completeness, here's what I have going.
NET Standard 2.0:
namespace MyBaseNamespace.Components.Effects
{
public class SearchBarStylingEffect : RoutingEffect
{
public SearchBarStylingEffect() : base("MyGroupName.SearchBarStylingEffect")
{
}
}
}
Android:
[assembly: ResolutionGroupName("MyGroupName")]
[assembly: ExportEffect(typeof(SearchBarStylingEffect), "SearchBarStylingEffect")]
namespace MyBaseNamespace.Components.Android.Renderers.Effects
{
public class SearchBarStylingEffect : PlatformEffect
{
…
}
}
(iOS is the same as Android, only the namespace is different)
I also posted this on Xamarin's forums, and Billy Liu from Xamarin asked a question that ultimately led me down the right path. I'll link to that here and also post what fixed it.
link: Xamarin Forms Post
Here's the resolution from that post:
In short, I had to do the following things to get this functional
(which it now is):
Ensure that the GroupNameResolution tag was on the first Effect ever initialized in the app.
Remove the empty file initializer from my .Net 2.0 project.
???
Profit.

What's the proper way of setting up MvvmCross 6.0 Xamarin.Forms UWP application code?

I have my UWP Application inherited from Base class, which inherits from MvxApplication<Setup, CoreApp>:
public sealed partial class App : WindowsApplication
{
public App()
{
InitializeComponent();
}
}
public class WindowsApplication : MvxApplication<Setup, CoreApp>
{
}
public class Setup : MvxWindowsSetup<CoreApp>
{
public override IEnumerable<Assembly> GetViewAssemblies()
{
// need to do this as otherwise I receive the message that corresponding view to view model is not found
var assemblies = base.GetViewAssemblies().ToList();
assemblies.Add(typeof(Forms.App).Assembly);
return assemblies;
}
}
However, when launching it, receiving the following error message:
The type MvxContentPagePresentationAttribute is not configured in the
presenter dictionary
As I understand, all that is not proper way to launch Xamarin.Forms MvvmCross application, as UWP App and Setup should be inherited from something like MvxFormsApplication and MvxFormsWindowsSetup<CoreApp, Forms.App> respectively (to have Xamarin.Forms app properly initialized).
But:
MvxFormsApplication is not generic and doesn't provide ability of passing Forms-generic setup.
even if I inherit the App from MvxFormsApplication and use this.RegisterSetupType<MvxFormsWindowsSetupInheritor>();, Visual Studio compiler never allows me to compile the project because of some weird error message (something like The name “WindowsApplication” does not exist in the namespace “…”) (this might be some issue of Visual Studio, but I have VS 15.7 version, which expects the code to work (again, MvvmCross declares they support UWP and XF)).
So, from my understanding, if there is Xamarin.Forms app, there must be also some way of passing actually Xamarin.Forms App class to the UWP App class initialization.
MvvmCross, again, stands for UWP and Xamarin.Forms support, but I can't see any clear example of the way to setup such type of application.
MvvmCross documentation as always is quite "modest". There are some instructions about setting up MvvmCross UWP app as well as setting up MvvmCross XF iOS/Android, but the only word about MvvmCross XF UWP is:
You are now free to place your custom renderers in a different
assembly. All you have to do to make it work is to add your assembly
to the Setup.ViewAssemblies collection.
(in official website docs)
(which is still sounds weird, as iOS and Android versions don't need that additional code, which makes me think that such (current) documentation isn't quite actualized)
and
UWP, WPF
Extend App from MvxApplication. ( App : MvxApplication { } )
from MvvmCross.Forms package readme.txt file, when all other platforms, again, expect inheritance for the app classes from MvxForms*-based ones.
MvvmCross guys, any thoughts on that?
When I set up a new Xamarin.Forms project, I always follow the Playground sample in the MvvmCross GitHub as this example evolves along with the API and is always up-to-date, as it is part of the MvvmCross solution, so any commits need to preserve its functionality. So if you want to see how everything should look in a minimal UWP + Xamarin.Forms project see the Playground.Forms.UI and Playground.Forms.Uwp projects in the linked folder.

MVVM Cross iOS Sqlite issue: Plugin not registered for type Cirrious.MvvmCross.Community.Plugins.Sqlite (Community Sqlite Plugin version 3.1.1)

I am using Xamarin Studio with MVVM Cross 3.5.1 and recently upgraded SQLite to the Community Plugin (version 3.1.1) and although it works fine in Android and Windows Phone 8.1 the iOS project can't use SQLite anymore.
The bootstraper code looks like this out-of-the-box:
using Cirrious.CrossCore.Plugins;
namespace Compass.Mobile.iOS.Bootstrap {
public class SqlitePluginBootstrap
: MvxPluginBootstrapAction<Cirrious.MvvmCross.Community.Plugins.Sqlite.PluginLoader>
{
}
}
I looked at Question 20143457 but the code it refers to is no longer applicable.
Has anybody figured out how to resolve this yet?
I would recommend using the SQLite-PCL for MvvmCross plugin: https://github.com/MvvmCross/MvvmCross-Plugins/tree/master/SQLite-PCL
It is much more up to date. The bootstrap to use for it is:
public class SqlitePluginBootstrap
: MvxPluginBootstrapAction<MvvmCross.Plugins.Sqlite.PluginLoader>
{}
This error can occur if you do not bootstrap a PCL+platform-specific
plugin in the iOS-specific way.
According to the docs
Note: on AoT platforms (i.e. MonoTouch/Xamarin.iOS) the bootstrap
class used is slightly different - to assist the AoT process, it
references classes from both the PCL and the platform specific
Assemblies for the plugin like:
public class ColorPluginBootstrap
: MvxLoaderPluginBootstrapAction<Color.PluginLoader, Color.Touch.Plugin>
{
}
instead of the standard
public class ColorPluginBootstrap
: MvxPluginBootstrapAction<Color.PluginLoader>
{
}

ExtensionContext error while creating Native Extension in Flex 3.6 SDK

I'm creating native extension with Flex 3.6. Coded native side then created Flex Library Project and then create .ane file. Finally imported .ane file to myFlex Project. Here is the problem I had. While I'm debugging app, "1046: Type was not found or was not a compile-time constant: ExtensionContext" error occurs. Attached the Library project .as class .
Thanks in Advance
package com.extension.samples
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.external.ExtensionContext;
public class NetworkConnectionANE extends EventDispatcher
{
public var _extContext : ExtensionContext;
public function NetworkConnectionANE(target:IEventDispatcher=null)
{
_extContext = ExtensionContext.createExtensionContext("com.extension.samples.NetworkConnectionANE", null);
super(target);
}
public function Connect(path:String):int
{
return _extContext.call("nativeFunc", path);
}
public function dispose():void
{
_extContext.dispose();
}
}
}
Edit:
I tried to use .swc file that created from library project in another Flex Desktop app, but the same error
Also tried with _extContext = ExtensionContext.createExtensionContext("com.extension.samples.NetworkConnectionANE","");
Edit: The problem about Flex SDK,no problem in SDK 4.6. Now the question is, How to use Extension in lib project in Flex 3.6 SDK ?
Right click on the project in flash builder goto properties of project and add air libraries in the flex library compiler.
I had the same problem but it was resolved in the following way:
File > New > Flex Library Project
Check the "Include Adobe AIR Libraries" option
As I know ExtensionContext.createExtensionContext() can be null in these cases:
The call is not in an .ane file. You cannot call this from a .swc or a .swf file. In other words it needs to be compiled to an .ane file before calling it.
You try to use the extension in a platform, that is not supported by the extension. For example you try to use an iOS extension on PC.
The ID for the extension does not exists. The extension ID must be the same, as the one you specify in the extension.xml like:
<extension xmlns="http://ns.adobe.com/air/extension/3.5">
<id>com.extension.samples.NetworkConnectionANE</id>
<versionNumber>0.0.1</versionNumber>
....
btw you dont need to set the second parameter of the ExtensionContext.createExtensionContext call, unless you want to specify OS-specific APIs (I havent even seen apps, that do this)

Resources