Loading a .xib using MvvmCross and Xamarin

2019-08-02 07:36发布

问题:

I'm taring my hair out at the moment. I've been trying to get a very simple view to load from a plugin but keep getting varying errors. The last attempt I did gives me the following error:

'Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: -[UIViewController _loadViewFromNibNamed:bundle:] loaded the "PincodeView" nib but the view outlet was not set.'

Here's the .xib:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" launchScreen="NO">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="PincodeViewController">
            <connections>
                <outlet property="RootView" destination="7" id="name-outlet-7"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <viewController id="6">
            <layoutGuides>
                <viewControllerLayoutGuide type="top" id="4"/>
                <viewControllerLayoutGuide type="bottom" id="5"/>
            </layoutGuides>
            <view key="view" contentMode="scaleToFill" id="7">
                <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
            </view>
            <point key="canvasLocation" x="-407" y="-274"/>
        </viewController>
    </objects>
</document>

There really not much in there. I've simply added a ViewController and that's it. Here's the File's owner and the .designer.cs

using MvvmCross.iOS.Views;
using NN.Plugin.Utils.iOS.IOC;
using NN.Plugin.Utils.ViewModels;

namespace NN.Plugin.Utils.iOS
{
    public partial class PincodeViewController : MvxViewController<PincodeViewModel>
    {
        public PincodeViewController () : base ("PincodeView", null)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
        }
    }
}

// WARNING
//
// This file has been generated automatically by Visual Studio from the outlets and
// actions declared in your storyboard file.
// Manual changes to this file will not be maintained.
//
using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace NN.Plugin.Utils.iOS
{
    [Register ("PincodeViewController")]
    partial class PincodeViewController
    {
        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UIKit.UIView RootView { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (RootView != null) {
                RootView.Dispose ();
                RootView = null;
            }
        }
    }
}

I've put a button on the FirstView.xib and bound that to an ICommand in the FirstViewModel.cs. The FirstView is shown correctly. When I click the button I get the error above. Here's the FirstViewModel.cs code with the ICommand bound to the button in the FirstView.xib.

using MvvmCross.Core.ViewModels;
using NN.Plugin.Utils.ViewModels;
using System.Windows.Input;
using System;

namespace PluginsTester.Core.ViewModels
{
    public class FirstViewModel
        : MvxViewModel
    {
        public override void Start()
        {
            base.Start();

            // ShowViewModel<PincodeViewModel>();
        }

        string hello = "Hello MvvmCross";
        public string Hello
        {
            get { return hello; }
            set { SetProperty(ref hello, value); }
        }

        public ICommand PinClicked
        {
            get => new MvxCommand(() => OpenPin());
        }

        private void OpenPin()
        {
            ShowViewModel<PincodeViewModel>();
        }
    }
}

I've been at this the whole day and still haven't gotten one step further. All this accompanied with crashes/hangs of VS2017 and the designer giving up on me all day long. Any help appreciated.

回答1:

Ok, the problem was this. I took the wrong .xib. It seems there is a difference between the Apple -> Empty User Interface and the Apple -> View

The lesson is: Don't use the Empty User Interface but use the View Controller. The latter creates a .cs and a .xib file that work just fine.