I am developing an application in which I need to use platform specific services. I referred so many stuff in the internet and couldn't find a way to get this to work. Even the MvvmCross documentation has not been updated I guess.
My platform specific service is to pop up dialogs in each platform in their native appearances.
Since I am new to MvvmCross I couldn't think of a way to make this work. Any help would be highly appreciated.
Note: MvvmCross version is 6.4.2
IPlatformSpecificDialogs.cs in Core/Common Project
public interface IPlatformSpecificDialogs
{
void Alert(string message, string title, string okButtonText);
}
IOSDialogs.cs in iOS Project
public class IOSDialogs : IPlatformSpecificDialogs
{
public IOSDialogs()
{
}
public void Alert(string message, string title, string okButtonText)
{
Console.WriteLine("worked");
}
}
Setup.cs in iOS Project
public class Setup : MvxIosSetup<App>
{
public Setup() : base()
{
}
protected override IMvxApplication CreateApp()
{
return App.Instance;
}
protected override void InitializeLastChance()
{
base.InitializeLastChance();
Mvx.IoCProvider.RegisterSingleton<IPlatformSpecificDialogs>(() => new IOSDialogs());
}
}
MyView.cs in iOS Project
[MvxFromStoryboard("Main")]
[MvxModalPresentation(WrapInNavigationController = false)]
public partial class MyView : BaseView<MyViewModel>
{
public MyView(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
protected override void BindControllers()
{
var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(AlertButton).To(vm => vm.AlertCommand);
set.Apply();
}
}
MyViewModel.cs in Core/Common Project
public class MyViewModel : BaseViewModel
{
private readonly IPlatformSpecificDialogs _platformSpecificDialogs;
public MyViewModel(IPlatformSpecificDialogs platformSpecificDialogs) : base()
{
_platformSpecificDialogs = platformSpecificDialogs;
}
public override Task Initialize()
{
return base.Initialize();
}
public IMvxCommand AlertCommand => new MvxCommand(PopupAlert);
private void PopupAlert()
{
_platformSpecificDialogs.Alert("Title","Messege", "OK");
}
}
Exception I get when I navigate to MyView
This is the Inner Exception Messege
Failed to resolve parameter for parameter platformSpecificDialogs of type IPlatformSpecificDialogs when creating MyProject.Core.ViewModels.MyViewModel. You may pass it as an argument.
This is the Inner Exception StackTrace
at MvvmCross.IoC.MvxIoCContainer.TryResolveParameter (System.Type type, System.Reflection.ParameterInfo parameterInfo, System.Object& parameterValue) [0x00020] in D:\a\1\s\MvvmCross\IoC\MvxIoCContainer.cs:686 at MvvmCross.IoC.MvxIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo selectedConstructor, System.Collections.Generic.IDictionary'2[TKey,TValue] arguments) [0x0003a] in D:\a\1\s\MvvmCross\IoC\MvxIoCContainer.cs:646 at MvvmCross.IoC.MvxIoCContainer.IoCConstruct (System.Type type, System.Collections.Generic.IDictionary'2[TKey,TValue] arguments) [0x0002b] in D:\a\1\s\MvvmCross\IoC\MvxIoCContainer.cs:416 at MvvmCross.IoC.MvxIoCContainer.IoCConstruct (System.Type type) [0x00000] in D:\a\1\s\MvvmCross\IoC\MvxIoCContainer.cs:363 at MvvmCross.IoC.MvxIoCProvider.IoCConstruct (System.Type type) [0x00000] in D:\a\1\s\MvvmCross\IoC\MvxIoCProvider.cs:149 at MvvmCross.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.ViewModels.IMvxBundle parameterValues, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00000] in D:\a\1\s\MvvmCross\ViewModels\MvxDefaultViewModelLocator.cs:56