Xamarin Android MvvmCross 构造函数问题中的特定 Ioc
Xamarin Android specific Ioc in MvvmCross constructor issue
tldr:有什么方法可以让我在注册类型时选择使用哪个构造函数?
我已按照 Inversion of control 中概述的说明创建平台特定的控制反转。
In your core project, you can declare an interface and you can use
that interface in your classes there ...
In each UI project, you can then declare the platform-specific
implementation ...
You can then register these implementations in each of the
platform-specific Setup files - e.g. you could override
MvxSetup.InitializeFirstChance
所以在我的核心项目中我定义了如下接口
public interface IJob
{
void AddAction(Action action);
}
在我的Android项目中我实现了如下接口
public class CustomJob: Job, IJob
{
private Action _action;
public CustomJob(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public CustomJob() : base(new Params
(Jobs.Priority.MID
).RequireNetwork().Persist())
{
//_action = action;
}
public override void OnAdded()
{
}
protected override void OnCancel(int p0, Throwable p1)
{
}
public override void OnRun()
{
_action.Invoke();
}
protected override RetryConstraint ShouldReRunOnThrowable(Throwable p0, int p1, int p2)
{
return RetryConstraint.Cancel;
}
public void AddAction(Action action)
{
_action = action;
}
}
并且我在 Setup.cs
中覆盖了 InitializeFirstChance
的实现
protected override void InitializeFirstChance()
{
Mvx.IoCProvider.RegisterType<IJob, CustomJob>();
base.InitializeFirstChance();
}
但是,我在运行时得到以下堆栈跟踪
{MvvmCross.Exceptions.MvxIoCResolveException: Failed to resolve
parameter for parameter javaReference of type IntPtr when creating
CustomJob. You may pass it as an argument at
MvvmCross.IoC.MvxIoCContainer.GetIoCParameterValues (System.Type type,
System.Reflection.ConstructorInfo firstConstructor,
System.Collections.Generic.IDictionary2[TKey,TValue] arguments)
[0x0005a] in
C:\projects\mvvmcross\MvvmCross\IoC\MvxIoCContainer.cs:648 at
MvvmCross.IoC.MvxIoCContainer.IoCConstruct (System.Type type,
System.Collections.Generic.IDictionary
2[TKey,TValue] arguments)
[0x00031] in
C:\projects\mvvmcross\MvvmCross\IoC\MvxIoCContainer.cs:413
有什么方法可以让我在注册类型时选择使用哪个构造函数?谢谢。
据我所知,您无法选择 Mvx 的 IoC 引擎将选择哪个构造函数。
所以一种方法是制作另一个 class 来实现你的 IJob
,它在内部引用了 Job
,并且在 class 的实现中你传递了Job
实施的行动。
public class JobHandler : IJob
{
private Job _customJob;
public JobHandler()
{
this._customJob = new CustomJob();
}
public void AddAction(Action action)
{
this._customJob.AddAction(action);
}
}
如果您愿意,可以做一个继承 IJob
的 IJobHandler
,这样您就可以将该接口用于 JobHandler
实现,而 IJob
用于您实际的 Job
另一种方法是将存根参数添加到无参数构造函数,这样它比另一个构造函数需要更多的参数,并由 IoC 引擎选择(我不完全确定它会起作用,但我认为是这样)。但是有点乱
另一种方法是你告诉 IoC 引擎你想如何构建你的 IJob
:
Mvx.IoCProvider.RegisterType<IJob>(() => new CustomJob());
高
tldr:有什么方法可以让我在注册类型时选择使用哪个构造函数?
我已按照 Inversion of control 中概述的说明创建平台特定的控制反转。
In your core project, you can declare an interface and you can use that interface in your classes there ...
In each UI project, you can then declare the platform-specific implementation ...
You can then register these implementations in each of the platform-specific Setup files - e.g. you could override MvxSetup.InitializeFirstChance
所以在我的核心项目中我定义了如下接口
public interface IJob
{
void AddAction(Action action);
}
在我的Android项目中我实现了如下接口
public class CustomJob: Job, IJob
{
private Action _action;
public CustomJob(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public CustomJob() : base(new Params
(Jobs.Priority.MID
).RequireNetwork().Persist())
{
//_action = action;
}
public override void OnAdded()
{
}
protected override void OnCancel(int p0, Throwable p1)
{
}
public override void OnRun()
{
_action.Invoke();
}
protected override RetryConstraint ShouldReRunOnThrowable(Throwable p0, int p1, int p2)
{
return RetryConstraint.Cancel;
}
public void AddAction(Action action)
{
_action = action;
}
}
并且我在 Setup.cs
中覆盖了InitializeFirstChance
的实现
protected override void InitializeFirstChance()
{
Mvx.IoCProvider.RegisterType<IJob, CustomJob>();
base.InitializeFirstChance();
}
但是,我在运行时得到以下堆栈跟踪
{MvvmCross.Exceptions.MvxIoCResolveException: Failed to resolve parameter for parameter javaReference of type IntPtr when creating CustomJob. You may pass it as an argument at MvvmCross.IoC.MvxIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor, System.Collections.Generic.IDictionary
2[TKey,TValue] arguments) [0x0005a] in C:\projects\mvvmcross\MvvmCross\IoC\MvxIoCContainer.cs:648 at MvvmCross.IoC.MvxIoCContainer.IoCConstruct (System.Type type, System.Collections.Generic.IDictionary
2[TKey,TValue] arguments) [0x00031] in C:\projects\mvvmcross\MvvmCross\IoC\MvxIoCContainer.cs:413
有什么方法可以让我在注册类型时选择使用哪个构造函数?谢谢。
据我所知,您无法选择 Mvx 的 IoC 引擎将选择哪个构造函数。
所以一种方法是制作另一个 class 来实现你的 IJob
,它在内部引用了 Job
,并且在 class 的实现中你传递了Job
实施的行动。
public class JobHandler : IJob
{
private Job _customJob;
public JobHandler()
{
this._customJob = new CustomJob();
}
public void AddAction(Action action)
{
this._customJob.AddAction(action);
}
}
如果您愿意,可以做一个继承 IJob
的 IJobHandler
,这样您就可以将该接口用于 JobHandler
实现,而 IJob
用于您实际的 Job
另一种方法是将存根参数添加到无参数构造函数,这样它比另一个构造函数需要更多的参数,并由 IoC 引擎选择(我不完全确定它会起作用,但我认为是这样)。但是有点乱
另一种方法是你告诉 IoC 引擎你想如何构建你的 IJob
:
Mvx.IoCProvider.RegisterType<IJob>(() => new CustomJob());
高