使用MvvmCross和Xamarin加载.xib

时间:2017-07-12 08:49:45

标签: ios xamarin mvvmcross

此刻我正在剪头发。我一直试图从插件中加载一个非常简单的视图,但不断出现变化的错误。我做的最后一次尝试给了我以下错误:

  

'Foundation.MonoTouchException:抛出Objective-C异常。名称:   NSInternalInconsistencyException原因: - [UIViewController   _loadViewFromNibNamed:bundle:]加载了“PincodeView”笔尖,但未设置视图插座。'

这是.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的崩溃/挂起,设计师终日放弃了我。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

好的,问题是这个。我选错了.xib。苹果似乎有区别 - &gt;空用户界面和Apple - &gt;查看

Select Project

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