MvvmCross/Xamarin“这个类不是键值编码兼容的键" [英] MvvmCross/Xamarin "This class is not key value coding-compliant for the key"
问题描述
我知道这个问题已经被问了一百万次,但是我在尝试跟随 Stuart Lodge 的教程N+1 days of MvvmCross",N=11,CollectionView 时遇到了错误.我没有 100% 跟进,因为我有另一个项目正在观看教程时正在编辑,所以我确定我只是错过了一些东西.这是完整的错误消息:
I know this question has been asked a million times but I'm getting the error trying to follow along with Stuart Lodge's tutorials "N+1 days of MvvmCross", N=11, CollectionView. I'm not following along 100% as I've got another project that I'm editing while watching the tutorials so I'm sure I've just missed something. Here is the full error message:
Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSUnknownKeyException Reason: [<NSObject 0x796c2460>
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key MyLabel.
TestItemView 的自动生成分部类似乎有一个有效的出口
There appears to be a valid outlet for the auto-generate partial class for TestItemView
我不相信这是向xib添加控件然后将其删除的情况,因此存在搁浅的连接.当我删除 xib 并重新开始时,就会发生这种情况.我有一个FirstView:MvxCollectionViewController",对于单元格,我有一个TestItemView:MvxCollectionViewCell",它是KittenCollectionCell"的替代品.下面我将给出 FirstView、TestItemView 和 TestItemView.xib 的源代码.
I don't believe this is a case of adding a control to the xib, then removing it, so that there is a stranded connection. This happens when I delete the xib and start all over again. I have a "FirstView : MvxCollectionViewController" and for the cells I have a "TestItemView : MvxCollectionViewCell" which is a stand-in for the "KittenCollectionCell". I will give the source code for FirstView, TestItemView, and the TestItemView.xib below.
第一视图:
using System.Drawing;
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
using Foundation;
using MvvmCross.Binding.iOS.Views;
using UIKit;
namespace FirstDemo.Core.iOS.Views
{
[Register("FirstView")]
public class FirstView : MvxCollectionViewController
{
private bool _isInitialized = false;
public FirstView() : base(new UICollectionViewFlowLayout() {
ItemSize = new SizeF(240, 400),
ScrollDirection = UICollectionViewScrollDirection.Horizontal
})
{
_isInitialized = true;
ViewDidLoad();
}
public sealed override void ViewDidLoad()
{
if (!_isInitialized)
return;
base.ViewDidLoad();
//var source = new MvxStandardTableViewSource(TestItemsTableView, "TitleText Name;");
//TestItemsTableView.Source = source;
CollectionView.RegisterNibForCell(TestItemView.Nib, TestItemView.Key);
var source = new MvxCollectionViewSource(CollectionView, TestItemView.Key);
CollectionView.Source = source;
var set = this.CreateBindingSet<FirstView, Core.ViewModels.FirstViewModel>();
set.Bind(source).To(vm => vm.TestItemViewModels);
set.Apply();
CollectionView.ReloadData();
}
}
}
这是我为 TestItemView 手工编码的部分:
Here is my hand-coded partial for TestItemView:
using System;
using FirstDemo.Core.ViewModels;
using Foundation;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Binding.iOS.Views;
using UIKit;
namespace FirstDemo.Core.iOS
{
public partial class TestItemView : MvxCollectionViewCell
{
public static readonly UINib Nib = UINib.FromName("TestItemView", NSBundle.MainBundle);
public static readonly NSString Key = new NSString("TestItemView");
public TestItemView(IntPtr handle)
: base(string.Empty /* TODO - this isn't really needed - mvx bug */, handle)
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<TestItemView, TestItemViewModel>();
set.Bind(MyLabel).To(testItem => testItem.Name);
set.Apply();
});
}
public static TestItemView Create()
{
return (TestItemView)Nib.Instantiate(null, null)[0];
}
}
}
这是自动生成的部分:
// WARNING
//
// This file has been generated automatically by Xamarin 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 FirstDemo.Core.iOS
{
[Register ("TestItemView")]
partial class TestItemView
{
[Outlet]
[GeneratedCode ("iOS Designer", "1.0")]
UILabel MyLabel { get; set; }
[Outlet]
[GeneratedCode ("iOS Designer", "1.0")]
UIView TestItemInfo { get; set; }
void ReleaseDesignerOutlets ()
{
if (MyLabel != null) {
MyLabel.Dispose ();
MyLabel = null;
}
if (TestItemInfo != null) {
TestItemInfo.Dispose ();
TestItemInfo = null;
}
}
}
}
最后,臭名昭著的 TestItemView xib:
And finally, the notorious xib for TestItemView:
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES"><dependencies><plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/></dependencies><objects><placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TestItemView"><connections><outlet property="MyLabel" destination="28" id="name-outlet-28"/><outlet property="TestItemInfo" destination="1" id="name-outlet-1"/></connections></placeholder><placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/><view contentMode="scaleToFill" id="1"><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="custom" customColorSpace="calibratedWhite"/><subviews><label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Label" lineBreakMode="tailTruncation" minimumFontSize="10" id="28" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES"><rect key="frame" x="235" y="172" width="42" height="21"/><color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/><fontDescription key="fontDescription" type="system" pointSize="17"/><color key="textColor" cocoaTouchSystemColor="darkTextColor"/><nil key="highlightedColor"/></label></subviews><userDefinedRuntimeAttributes><userDefinedRuntimeAttribute keyPath="accessibilityIdentifier" type="string" value="MyTestItem"/></userDefinedRuntimeAttributes></view></objects></document>
如果有人有线索,我将不胜感激,因为我完全不知所措.
If anyone has a clue, I'd appreciate the help because I'm at a complete loss.
谢谢!
推荐答案
我不熟悉快速方法.但是在objective-c 中,您必须手动将出口对象从XIB 添加到代码中.当您的 XIB 视图链接到某个旧视图的地址时,将出现此错误.所以在 Objective-c 中,我们曾经执行以下步骤.
I am not familier with the swift approach. But in objective-c, you have to manually add outlet object from XIB to code. And when your XIB's view is linked to some older view's address this error will appear. So in objective-c we used to do the below steps.
- 打开你的XIB
- 搜索名为 MyLabel 的标签
- 验证连接(它链接到哪个对象)
- 如果需要,请移除插座链接并再次添加.
这篇关于MvvmCross/Xamarin“这个类不是键值编码兼容的键"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!