使用 MvvmCross 和 Xamarin 加载 .xib

Loading a .xib using MvvmCross and Xamarin

我现在正在理发。我一直在尝试从插件中加载一个非常简单的视图,但不断出现各种错误。我最后一次尝试出现以下错误:

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

这是.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>

里面真的没有多少东西。我只是添加了一个 ViewController 而已。 这是文件的所有者和 .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;
            }
        }
    }
}

我在 FirstView.xib 上放置了一个按钮并将其绑定到 FirstViewModel.cs 中的 ICommand。 FirstView 显示正确。当我单击按钮时,出现上面的错误。 这是 FirstViewModel.cs 代码,其中 ICommand 绑定到 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>();
        }
    }
}

我一整天都在做这个,但仍然没有取得进一步的进展。这一切都伴随着VS2017的crashes/hangs和设计师整天放弃我。任何帮助表示赞赏。

好的,问题是这样的。我拿错了.xib。 Apple -> Empty User Interface 和 Apple -> View 之间似乎有区别

教训是:不要使用空用户界面,而要使用视图控制器。后者创建一个工作正常的 .cs 和 .xib 文件。