【问题标题】:MvvmCross Xamarin error cannot find view for ViewModel in IOSMvvmCross Xamarin 错误在 IOS 中找不到 ViewModel 的视图
【发布时间】:2020-02-02 16:21:18
【问题描述】:

我将 Mvvmcross 从 5.7 更新到 6.x。当我介绍 BaseViewModel 时,导航正在工作,但它不起作用。 这是我的 StartPageViewModel 的示例

public class StartPageViewModel : MvxViewModel
{
protected readonly IUserSettings _userSettings;
private readonly IMvxNavigationService _navigationService;

public StartPageViewModel(IUserSettings userSettings,
IMvxNavigationService navigationService)
{
_userSettings = userSettings;
_navigationService = navigationService;
}
public IUserSettings UserSettings
{
get
{
return _userSettings;
}
}
 ]private IMvxCommand _loginCommand;
    public IMvxCommand LoginCommand
    {
        get
        {
            return _loginCommand ?? (_loginCommand = new MvxCommand(() => 
         _navigationService.Navigate<LoginViewModel>()));
        }
    }

    public void NavigateToDashboardIfAlreadyLoggedIn()
    {

    }

    public override void Start()
    {
        base.Start();
        NavigateToDashboardIfAlreadyLoggedIn();
    }
}

LoginViewModel 示例

public class LoginViewModel : BaseValidationViewModel
{


    public LoginViewModel(IMvxLogProvider logProvider,
        IUserSettings userSettings,
        IAuthenticationManager authenticationManager,
        IEventLogger eventLogger,
        IMvxNavigationService navigationService)
     : base(userSettings, eventLogger, authenticationManager, navigationService)
    {

        _navigationService = navigationService;
    }
}

我想使用 BaseViewModel 导航到 LoginViewModel,但我的 viewmodel 看不到视图,即 LoginView。

BaseValidationViewModel 示例

public abstract class BaseValidationViewModel : BaseViewModel
{
    protected BaseValidationViewModel(IUserSettings userSettings,
    IEventLogger eventLogger,
    IAuthenticationManager authenticationManager,
    IMvxNavigationService navigationService)
    : base(userSettings, eventLogger, authenticationManager, navigationService)
{
   //Some Code
}
 //Some Code
public override void Start() 
{
 base.Start();
}
}

基础视图模型

public abstract class BaseViewModel : MvxViewModel
{
    protected readonly IUserSettings _userSettings;
    protected readonly IEventLogger _eventLogger;
    protected readonly IAuthenticationManager _authenticationManager;
    private readonly IMvxNavigationService _navigationService;


    protected BaseViewModel(IUserSettings userSettings,
        IEventLogger eventLogger,
        IAuthenticationManager authenticationManager,
        IMvxNavigationService navigationService)
    {
        _userSettings = userSettings;
        _eventLogger = eventLogger;
        _authenticationManager = authenticationManager;
        _navigationService = navigationService;
    }


    public override void Start()
    {
        base.Start();

    }

}

LoginView 示例

`[Register("LoginView")]
[MvxViewFor(typeof(LoginViewModel))]

 
public partial class LoginView : MvxViewController<LoginViewModel>
    
{
}`

【问题讨论】:

  • 所以当您导航到 LoginViewModel 时它不会崩溃而只是显示一个空白屏幕?你在使用故事板吗? MvxViewFor 不会做任何事情,除非你在 WPF 上,而你不是。
  • 嗨@Cheesebaron,我没有使用Storyboards,但该应用程序不会压碎或显示空白屏幕。但它仍然在 StartPageView.. 我还在 LoginViewModel 中设置了断点,它显示它可以导航但它不能显示 LoginView
  • 嗨@Cheesebaron,我也尝试在我的 LoginView 中使用 [MvxChildPresentation] 但仍然没有显示 LoginView 页面:[MvxChildPresentation] public partial class LoginView : MvxViewController { }

标签: c# visual-studio xamarin xamarin.ios mvvmcross


【解决方案1】:

我遇到了同样的问题,并通过在 InitializeViewLookup 中像这样将视图映射到视图模型来解决它

protected override IMvxViewsContainer InitializeViewLookup(IDictionary<Type, Type> viewModelViewLookup)
{
    viewModelViewLookup.Add(typeof(UserDatabaseUpgradeViewModel),typeof(UserDatabaseUpgradeView));
}

希望这也能解决您的问题。我正在使用 MvvmCross 6.4.x

【讨论】:

    猜你喜欢
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多