Xamarin.IOS MVVM模拟器上的空白屏幕

时间:2018-10-29 04:52:53

标签: ios xamarin mvvmcross

我创建了一个新的Classic Xamarin解决方案,其中包含一个.NET Standard项目,Xamarin.Android项目和Xamarin.IOS项目。我的ViewModels都是在Core项目中定义的,我已经为登录页面,主页页面和几个页面创建了IOS视图。我也在使用MVVMCross。

当我在模拟器中运行该应用程序时,看不到任何控件或对象,而是白屏。没有引发任何错误。我正在使用仅代码方式,并从项目中删除了所有情节提要,.xibs等。

AppStart:

public class AppStart : MvxAppStart
{
    private readonly IMvxNavigationService _navigationService;
    private readonly ILoginService _loginService;

    public AppStart(IMvxApplication application, IMvxNavigationService navigationService, ILoginService loginService) : base(application, navigationService)
    {
        _navigationService = navigationService;
        _loginService = loginService;
    }

    protected override Task NavigateToFirstViewModel(object hint = null)
    {
        if(_loginService.IsAuthenticated){
            return NavigationService.Navigate<HomeViewModel>();
        }
        else{
            return NavigationService.Navigate<LoginViewModel>();
        }
    }

}

LoginViewModel:

private readonly IMvxNavigationService _navigationService;
    private readonly ILoginService _loginService;
    private readonly IDialogService _dialogService;

    public LoginViewModel(IMvxNavigationService navigationService, ILoginService loginService)
    {
        _navigationService = navigationService;
        _loginService = loginService;

        Username = "TestUser";
        Password = "password";
        IsLoading = false;

        ShowHomeViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<HomeViewModel>());
        //ShowLocationViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<LocationViewModel>());
        ShowMoveViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<MoveViewModel>());
        ShowPullViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<PullViewModel>());
        ShowAuditViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<AuditViewModel>());
        ShowConfigureViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<ConfigureViewModel>());
        ShowLookupViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<LookupViewModel>());
        ShowLogoutViewModelCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<LogoutViewModel>());


    }

    //MVVM Commands
    public IMvxAsyncCommand ShowHomeViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowLocationViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowMoveViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowPullViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowAuditViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowConfigureViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowLookupViewModelCommand { get; private set; }
    public IMvxAsyncCommand ShowLogoutViewModelCommand { get; private set; }

    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))
        {
            _navigationService.Navigate<MainViewModel>();
        }
        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));
    }

设置(IOS项目):

public class Setup : MvxIosSetup<App>
{
    protected override void InitializeFirstChance()
    {
        base.InitializeFirstChance();
        Mvx.IoCProvider.RegisterType<IMvxJsonConverter, MvxJsonConverter>();
    }

    protected override void InitializeLastChance()
    {
        base.InitializeLastChance();

        var registry = Mvx.IoCProvider.Resolve<IMvxTargetBindingFactoryRegistry>();
        registry.RegisterFactory(new MvxCustomBindingFactory<UIViewController>("NetworkIndicator", targetBindingCreator: (viewController) => new NetworkIndicatorTargetBinding(viewController)));
    }

    protected override IMvxIocOptions CreateIocOptions()
    {
        return new MvxIocOptions
        {
            PropertyInjectorOptions = MvxPropertyInjectorOptions.MvxInject
        };
    }
}

AppDelegate:

public class AppDelegate : MvxApplicationDelegate<Setup, App>
{
    public override UIWindow Window { get; set; }

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        var result = base.FinishedLaunching(application, launchOptions);
        CustomizeAppearance();
        return result;
    }
    private void CustomizeAppearance()
    {
        UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes
        {
            TextColor = UIColor.Black,
            Font = UIFont.SystemFontOfSize(17f, UIFontWeight.Semibold)
        });
        UINavigationBar.Appearance.Translucent = false;
        UITabBar.Appearance.BarTintColor = AppColors.PrimaryColor.ToNativeColor();
        UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = AppColors.AccentColor.ToNativeColor()
        }, UIControlState.Selected);


    }
}

LoginView(IOS):

 [MvxRootPresentation]
public partial class LoginView : MvxViewController<LoginViewModel>
{
    public LoginView() : base("LoginView", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        var textUserName = new UITextField { Placeholder = "UserName", BorderStyle = UITextBorderStyle.RoundedRect };
        var textPassword = new UITextField { Placeholder = "Password", BorderStyle = UITextBorderStyle.RoundedRect };
        var loginButton = new UIButton(UIButtonType.RoundedRect);

        var set = this.CreateBindingSet<LoginView, LoginViewModel>();
        set.Bind(textUserName).To(vm => vm.Username);
        set.Bind(textPassword).To(vm => vm.Password);
        set.Bind(loginButton).To(vm => vm.LoginCommand);
        set.Apply();
    }


    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }
}

如何解决此空白屏幕问题并确保我的LoginView正确呈现?

0 个答案:

没有答案