I'm trying to make dependency on UWP by this tutorial https://www.mvvmcross.com/documentation/platform/uwp/universal-windows-platform-uwp?scroll=621
but on this step in Setup.cs:
public Setup( Frame rootFrame ) : base( rootFrame ){}
I get an error
does anybody have an idea why??
Dependency on Android works well
Solution
App.xaml.cs
public partial class App
{
public App()
{
InitializeComponent();
}
}
public abstract class AWSService : MvxApplication<Setup, Core.App>
{ }
Setup.cs
public class Setup : MvxWindowsSetup<Core.App>
{
public Setup()
{ }
protected override IMvxApplication CreateApp()
{
Mvx.IoCProvider.RegisterType<IYourInterface, YourNativeClass>();
return new Core.App();
}
}
Related
The error I'm getting during app startup is the following:
Parent class vtable failed to initialize, due to: Could not load list of method overrides due to Method not found: void Shiny.IShinyStartup.ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection) assembly:/Users/merickson/Library/Developer/CoreSimulator/Devices/D19E269D-A3E5-46C2-BB9C-94A122EA02DC/data/Containers/Bundle/Application/F9A34529-8F64-4868-8D7F-389C77DB54BC/PrismSizeTest.iOS.app/Shiny.Prism.dll type:PrismStartup member:(null)
I'm not sure if I'm missing a NuGet, maybe wrong version of one or more NuGets, or am I initializing something wrong? Any help is appreciated.
I've been able to replicate this with a bare bones solution which I can provide if necessary. Here are the main files involved:
App.xaml.cs
public partial class App : PrismApplicationBase
{
public App(IPlatformInitializer initializer)
: base(initializer)
{
}
protected override async void OnInitialized()
{
InitializeComponent();
await NavigationService.NavigateAsync("NavigationPage/MainPage");
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IAppInfo, AppInfoImplementation>();
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
}
protected override IContainerExtension CreateContainerExtension() => ContainerLocator.Current;
}
AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ShinyHost.Init(new ApplePlatform(), new MyStartup());
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(new iOSInitializer()));
return base.FinishedLaunching(app, options);
}
}
public class iOSInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register any platform specific implementations
}
}
MyStartup.cs
public class MyStartup : PrismStartup
{
protected override void ConfigureServices(IServiceCollection services)
{
services.UseGps<GpsDelegate>();
}
}
NuGets installed:
There is nothing directly wrong with the code... however the latest versions of Shiny have breaking ABI changes that make Shiny.Prism and Shiny.Core incompatible which is the result of the issue shown here...
For more information you can track the issue at: https://github.com/dansiegel/Prism.Container.Extensions/issues/183
I am new to xUnit and with the help of Prism Template Studio I created new Prism xUnit test project, which by default created one PrismAppMock.cs file as below:
// This should implement your application class -- What does it means?
public class PrismAppMock : PrismApplicationBase
{
public PrismAppMock(IPlatformInitializer initializer)
: base(initializer)
{
}
protected override IContainerExtension CreateContainerExtension()
{
return null;
}
protected override void OnInitialized()
{
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
On running the test it is throwing null exception as container is not resolved.
I tried to use UnityContainerExtension but as it is abstract class I can't use it directly.
protected override IContainerExtension CreateContainerExtension()
{
return new UnityContainerExtension();
}
Can anybody suggest on how to initialise PrismAppMock for unit testing.
I'm new to Xamarin and Prism, so forgive me if I'm missing something obvious. I am following the example of this project in GitHub. However, I am getting the following error when running my Android project.
System.InvalidOperationException: The current type, MyApp.Abstractions.IFacebookManager, is an interface and cannot be constructed. Are you missing a type mapping?
The error is occurring in EntryPage.xaml.g.cs in the LoadFromXaml() method.
[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("Views\\EntryPage.xaml")]
public partial class EntryPage : global::Xamarin.Forms.ContentPage {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
private void InitializeComponent() {
// Exception thrown here!
global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(EntryPage));
}
}
This leads me to believe that I'm not using the IoC container properly, but I can't see what I'm doing differently from the example. The EntryPageViewModel's constructor takes IFacebookManager as a parameter and I understand that Unity should take care of this. I have a breakpoint on the constructor, but it's not being hit. This is a .NET Standard Xamarin solution. I would appreciate any help and guidance. Thank you!
Here's my App.xaml.cs
using MyApp.Abstractions;
using MyApp.Helpers;
using MyApp.Services;
using MyApp.ViewModels;
using MyApp.Views;
using Prism;
using Prism.Ioc;
using Prism.Unity;
using Xamarin.Forms;
namespace MyApp
{
public partial class App : PrismApplication
{
public App(IPlatformInitializer initializer = null) : base(initializer) { }
protected override void OnInitialized()
{
InitializeComponent();
ServiceResolver.Instance.Add<ICloudService, AzureCloudService>();
NavigationService.NavigateAsync("NavigationPage/EntryPage");
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<EntryPage, EntryPageViewModel>();
}
}
}
Here's my MainActivity.cs
using Android.App;
using Android.Content.PM;
using Android.OS;
using MyApp.Droid.Services;
using MyApp.Abstractions;
using Android.Content;
using Xamarin.Facebook;
using Xamarin.Forms;
using Prism;
using Prism.Ioc;
namespace MyApp.Droid
{
[Activity(Label = "MyApp", Icon = "#drawable/icon", Theme = "#style/MainTheme", Name = "com.mydomain.myapp.MainActivity", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
ICallbackManager fbCallbackManager;
AndroidLoginProvider loginProvider;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
global::Xamarin.Forms.Forms.Init(this, bundle);
DependencyService.Register<IFacebookManager, FacebookManager>();
LoadApplication(new App(new AndroidInitializer()));
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
var manager = DependencyService.Get<IFacebookManager>();
if (manager != null)
{
(manager as FacebookManager)._callbackManager.OnActivityResult(requestCode, (int)resultCode, data);
}
}
}
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
You are mixing the Xamarin Forms Dependency Service with the one provided with Prism.Unity
Instead of calling DependencyService.Register<IFacebookManager, FacebookManager>();
You need to register it with the AndroidInitializer .
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry container)
{
container.RegisterSingleton<IFacebookManager, FacebookManager>();
}
}
You can then always resolve a dependency manually by calling App.Container.Resolve inside OnActivityResult.
To complete the previous answer that helped me a lot to fix a similar issue:
with prism 7, you need to resolve a dependency via IContainerProvider as follow:
var container = (App.Current as Prism.Unity.PrismApplication).Container;
var manager = container.Resolve<IFacebookManager>();
In this case, Unity is used as the IoC "container"
When i try to run the project, i am getting this kind of error: "System.NullReferenceException: Object reference not set to an instance of an object"
pointing in this code:
sqliteconnection = DependencyService.Get().GetConnection();
This is my Class for DB actions:
namespace DevoApp.DevoAppFinal.Helpers
{
public class DatabaseHelper
{
static SQLiteConnection sqliteconnection;
public const string DbFileName = "Devotion.db";
public DatabaseHelper()
{
sqliteconnection = DependencyService.Get<ISQLite>().GetConnection();
sqliteconnection.CreateTable<Devotion>();
}
// Get All Contact data
public List<Devotion> GetAllDevotionsData()
{
return (from data in sqliteconnection.Table<Devotion>() select data).ToList();
}
//Get Specific Contact data
public Devotion GetDevotionData(int id)
{
return sqliteconnection.Table<Devotion>().FirstOrDefault(t => t.devotionalId == id);
}
// Delete all Contacts Data
public void DeleteAllDevotions()
{
sqliteconnection.DeleteAll<Devotion>();
}
// Delete Specific Contact
public void DeleteDevotion(int id)
{
sqliteconnection.Delete<Devotion>(id);
}
// Insert new Contact to DB
public void InsertDevotion(Devotion contact)
{
sqliteconnection.Insert(contact);
}
// Update Contact Data
public void UpdateDevotion(Devotion contact)
{
sqliteconnection.Update(contact);
}
}
}
When using the DependencyService, you have to implement the interface in each targeted platform project.
In this case, you should have the ISQLite interface implemented on the platforms you're targeting, i.e. iOS and Android.
To make Xamarin find it at runtime, you will have to register the implementation with the Dependency attribute above the namespace. Observe the following example based on a few assumptions of your project.
In your shared library you have declared the interface:
public interface ISQLite
{
// Members here
}
Nothing fancy going on there. Then for each platform, you want to run the app on, do something like this:
[assembly: Xamarin.Forms.Dependency (typeof (SQLiteImplementation_iOS))]
namespace DevoApp.DevoAppFinal.iOS
{
public class SQLiteImplementation_iOS : ISQLite
{
// ... Your code
}
}
From the error, it looks like you forgot to add the attribute
I am always getting the error
No parameterless constructor defined for this object.
when I execute the codes below.
It must be something to do with my ninject modules.
What seems to be the problem here?
Controller
public class AccountController : Controller
{
private readonly IUserService _service;
public AccountController(IUserService service)
{
_service = service;
}
}
Service Layer
public class UserService : ServiceBase<User>, IUserService
{
public UserService(IRepository repository) : base(repository)
{
}
}
public interface IUserService : IService<User>
{
}
Repository
public class UserRepository : RepositoryBase<User>, IUserRepository
{
}
public interface IUserRepository : IRepository<User>
{
}
Ninject ----
ServiceModule
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IUserService>().To<UserService>();
}
}
RepositoryModule
public class RepositoryModule : NinjectModule
{
public override void Load()
{
Bind<IUserRepository>().To<UserRepository>();
}
}
Setup
public static void SetupDependencyInjection()
{
// Create Ninject DI kernel
var kernel = CreateKernel();
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
private static IKernel CreateKernel()
{
var modules = new INinjectModule[]
{
new ServiceModule(),
new RepositoryModule()
};
return new StandardKernel(modules);
}
Have have registered:
Bind<IUserRepository>().To<UserRepository>();
But you are referring to the base IRepository interface in UserService:
public UserService(IRepository repository) : base(repository)
{
}
So Ninject won't know how to construct a generic IRepository. You need to Bind a implementation for IRepository. Or use IUserRepository in UserService:
public UserService(IUserRepository repository) : base(repository)
{
}