- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发我的第一个Xamarin应用程序。我试图显示我的第一页,但在base.ViewDidLoad();上出现以下错误ViewDidLoad()中的行
已抛出MvvmCross.Platform.Exceptions.MvxException
Failed to construct and initialize ViewModel for type iManage.ViewModels.LoginViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information
public partial class LoginView : MvxViewController<LoginViewModel>
{
public LoginView() : base("LoginView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
txtUser.Layer.CornerRadius = 15;
txtPassword.Layer.CornerRadius = 15;
btnLogin.Layer.CornerRadius = 20;
var set = this.CreateBindingSet<LoginView, LoginViewModel>();
set.Bind(txtUser).To(vm => vm.Username);
set.Bind(txtPassword).To(vm => vm.Password);
set.Bind(btnLogin).To(vm => vm.LoginCommand);
//set.Bind(btnLogin).To(vm => vm.AttemptLogin());
set.Apply();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
public class LoginViewModel : BaseViewModel
{
private readonly ILoginService _loginService;
private readonly IDialogService _dialogService;
public LoginViewModel(ILoginService loginService, IDialogService dialogService)
{
_loginService = loginService;
_dialogService = dialogService;
Username = "TestUser";
Password = "YouCantSeeMe";
IsLoading = false;
}
private string _username;
public string Username
{
get
{
return _username;
}
set
{
SetProperty(ref _username, value);
RaisePropertyChanged(() => Username);
}
}
private string _password;
public string Password
{
get
{
return _password;
}
set
{
SetProperty(ref _password, value);
RaisePropertyChanged(() => Password);
}
}
private bool _isLoading;
public bool IsLoading
{
get
{
return _isLoading;
}
set
{
SetProperty(ref _isLoading, value);
}
}
private IMvxCommand _loginCommand;
public virtual IMvxCommand LoginCommand
{
get
{
_loginCommand = _loginCommand ?? new MvxCommand(AttemptLogin, CanExecuteLogin);
return _loginCommand;
}
}
private void AttemptLogin()
{
if (_loginService.Login(Username, Password))
{
ShowViewModel<DashboardEmpViewModel>();
}
else
{
_dialogService.Alert("We were unable to log you in!", "Login Failed", "OK");
}
}
private bool CanExecuteLogin()
{
return (!string.IsNullOrEmpty(Username) || !string.IsNullOrWhiteSpace(Username))
&& (!string.IsNullOrEmpty(Password) || !string.IsNullOrWhiteSpace(Password));
}
}
public class LoginService : ILoginService
{
/// <summary>Initializes a new instance of the <see cref="LoginService"/> class.</summary>
public LoginService() // e.g. LoginService(IMyApiClient client)
{
// this constructor would most likely contain some form of API Client that performs
// the message creation, sending and deals with the response from a remote API
}
/// <summary>
/// Gets a value indicating whether the user is authenticated.
/// </summary>
public bool IsAuthenticated { get; private set; }
/// <summary>Gets the error message.</summary>
/// <value>The error message.</value>
public string ErrorMessage { get; private set; }
/// <summary>
/// Attempts to log the user in using stored credentials if present
/// </summary>
/// <returns> <see langword="true"/> if the login is successful, false otherwise </returns>
public bool Login()
{
// get the stored username from previous sessions
// var username = Settings.UserName;
// var username = _settingsService.GetValue<string>(Constants.UserNameKey);
// force return of false just for demo purposes
IsAuthenticated = false;
return IsAuthenticated;
}
/// <summary>The login method to retrieve OAuth2 access tokens from an API. </summary>
/// <param name="userName">The user Name (email address) </param>
/// <param name="password">The users <paramref name="password"/>. </param>
/// <param name="scope">The required scopes. </param>
/// <returns>The <see cref="bool"/>. </returns>
public bool Login(string userName, string password, string scope)
{
try
{
//IsAuthenticated = _apiClient.ExchangeUserCredentialsForTokens(userName, password, scope);
return IsAuthenticated;
}
catch (ArgumentException argex)
{
ErrorMessage = argex.Message;
IsAuthenticated = false;
return IsAuthenticated;
}
}
/// <summary>
/// Logins the specified user name.
/// </summary>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The users password.</param>
/// <returns></returns>
public bool Login(string userName, string password)
{
// this simply returns true to mock a real login service call
return true;
}
}
public class Setup : MvxIosSetup
{
public Setup(IMvxApplicationDelegate applicationDelegate, IMvxIosViewPresenter presenter) : base(applicationDelegate, presenter)
{
}
protected override IMvxApplication CreateApp()
{
return new App();
}
}
MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type iManage.ViewModels.LoginViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information ---> MvvmCross.Platform.Exceptions.MvxException: Problem creating viewModel of type LoginViewModel ---> MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to resolve parameter for parameter dialogService of type IDialogService when creating iManage.ViewModels.LoginViewModel at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00066] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x0002c] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Platform.Mvx.IocConstruct (System.Type t) [0x00006] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00000] in :0 --- End of inner exception stack trace --- at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00029] in :0 at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00035] in :0 --- End of inner exception stack trace --- at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00068] in :0 at MvvmCross.iOS.Views.MvxViewControllerExtensionMethods.LoadViewModel (MvvmCross.iOS.Views.IMvxIosView iosView) [0x0005f] in <6f99728979034e579bc72f6d53e5bc35>:0 at MvvmCross.Core.Views.MvxViewExtensionMethods.OnViewCreate (MvvmCross.Core.Views.IMvxView view, System.Func`1[TResult] viewModelLoader) [0x00012] in :0 at MvvmCross.iOS.Views.MvxViewControllerExtensionMethods.OnViewCreate (MvvmCross.iOS.Views.IMvxIosView iosView) [0x00001] in <6f99728979034e579bc72f6d53e5bc35>:0 at MvvmCross.iOS.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00007] in <6f99728979034e579bc72f6d53e5bc35>:0 at at (wrapper delegate-invoke) :invoke_void_object_EventArgs (object,System.EventArgs) at MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise (System.EventHandler eventHandler, System.Object sender) [0x00003] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Platform.iOS.Views.MvxEventSourceViewController.ViewDidLoad () [0x00006] in <4467c42ffcc4478e847227b8e4af47fe>:0 at MvvmCross.iOS.Views.MvxViewController.ViewDidLoad () [0x00001] in <6f99728979034e579bc72f6d53e5bc35>:0 at iManage.iOS.Views.LoginView.ViewDidLoad () [0x00001] in /Users/pankajsachdeva/Projects/iManage/iOS/Views/LoginView.cs:18 at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at iManage.iOS.Application.Main (System.String[] args) [0x00001] in /Users/pankajsachdeva/Projects/iManage/iOS/Main.cs:17
最佳答案
您必须在IoC容器中注册服务的实现,以便可以在需要时对其进行解析。通常,可以在两个地方完成此操作:
对于App.cs
方法中Initialize
中与平台无关的服务:
public class App : MvxApplication
{
public override void Initialize()
{
...
Mvx.ConstructAndRegisterSingleton<IDialogService,DialogService>();
...
}
}
App
类已经包含一个块,该块会自动使用“Service”后缀注册所有类型)
Setup.cs
方法中平台的
InitializeFirstChance
中特定于平台的服务,例如对于Windows,它将为:
public class Setup : MvxWindowsSetup
{
protected override void InitializeFirstChance()
{
Mvx.ConstructAndRegisterSingleton<IDialogService,DialogService>();
base.InitializeFirstChance();
}
}
DialogService
将取决于平台,因此第二种解决方案将更合适。
LazyConstructAndRegisterSingleton
来确保仅在首次实际需要时才创建实例。您还可以使用
RegisterType
来注册类型并在每次需要时创建一个新实例。
关于c# - 无法构造和初始化viewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139347/
我是 Spring 新手,这就是我想要做的事情: 我正在使用一个基于 Maven 的库,它有自己的 Spring 上下文和 Autowiring 字段。 它的bean配置文件是src/test/res
我在我的测试脚本中有以下列表初始化: newSequenceCore=["ls", "ns", "*", "cm", "*", "ov", "ov", "ov", "ov", "kd"] (代表要在控
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Class construction with initial values 当我查看 http://en.
我得到了成员变量“objectCount”的限定错误。编译器还返回“ISO C++ 禁止非常量静态成员的类内初始化”。这是主类: #include #include "Tree.h" using n
我有如下所示的a.h class A { public: void doSomething()=0; }; 然后我有如下所示的b.h #include "a.h" class b: publi
我需要解析 Firebase DataSnapshot (一个 JSON 对象)转换成一个数据类,其属性包括 enum 和 list。所以我更喜欢通过传递 DataSnapshot 来手动解析它进入二
我使用 JQuery 一段时间了,我总是使用以下代码来初始化我的 javascript: $(document).ready( function() { // Initalisation logic
这里是 Objective-C 菜鸟。 为什么会这样: NSString *myString = [NSString alloc]; [myString initWithFormat:@"%f", s
我无法让核心数据支持的 NSArrayController 在我的代码中正常工作。下面是我的代码: pageArrayController = [[NSArrayController alloc] i
我对这一切都很陌生,并且无法将其安装到我的后端代码中。它去哪里?在我的页脚下面有我所有的 JS? 比如,这是什么意思: Popup initialization code should be exec
这可能是一个简单的问题,但是嘿,我是初学者。 所以我创建了一个程序来计算一些东西,它目前正在控制台中运行。我决定向其中添加一个用户界面,因此我使用 NetBeans IDE 中的内置功能创建了一个 J
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
据我所知, dependentObservable 在声明时会进行计算。但如果某些值尚不存在怎么办? 例如: var viewModel ={}; var dependentObservable1 =
我正在阅读 POODR 这本书,它使用旧语法进行默认值初始化。我想用新语法实现相同的功能。 class Gear attr_reader :chainring, :cog, :wheel de
我按照 polymer 教程的说明进行操作: https://www.polymer-project.org/3.0/start/install-3-0 (我跳过了可选部分) 但是,在我执行命令“po
很抱歉问到一个非常新手的Kotlin问题,但是我正在努力理解与构造函数和初始化有关的一些东西。 我有这个类和构造函数: class TestCaseBuilder constructor(
假设我们有一个包含 30 列和 30 行的网格。 生命游戏规则简而言之: 一个小区有八个相邻小区 当一个细胞拥有三个存活的相邻细胞时,该细胞就会存活 如果一个细胞恰好有两个或三个活的相邻细胞,那么它就
我是 MQTT 和 Android 开放附件“AOA” 的新手。在阅读教程时,我意识到,在尝试写入 ByteArrayOutputStream 类型的变量之前,应该写入 0 或 0x00首先到该变量。
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我是一名优秀的程序员,十分优秀!