我正在尝试在 Xamarin Studio 中创建一个简单的 Hello World 测试应用程序。我创建了一个没有 Story Board 的空白单 View iPhone 应用程序。
我将一个 ImageView 拖放到 View 上并为其连接一个 socket 。就这么简单。
当我在模拟器中运行时,它工作正常,但在设备上调试时抛出 NSUnknownKeyException。
我检查了常见的嫌疑人:
- 检查带有感叹号的未定义 socket
- 检查了 designer.cs 文件
- 检查 .h 和 .m 文件
- 清理并重建所有
- 从部署选项中清除主界面设置
- 设置自定义类定义
- 使用-f选项编译
- 更新到最新的 Xcode、Monotouch 和 Xamarin Studio(Xamarin Studio 4.2.3、Xcode 5.1、7.2.0.2(入门版))
- 将 iPhone 5s 更新到 IOS 7.1
我仍然遇到同样的异常。以下是代码和错误信息。我是否遗漏了一件非常明显的事情,或者 Xcode 和 Xamarin Studio 之间的同步以某种方式失败了?
感谢任何帮助。
错误日志
MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSUnknownKeyException Reason: [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imgTest. at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr) at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:130 at TestProject.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00038] in /Users/gorken/Projects/TestProject/TestProject/AppDelegate.cs:31 at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 at TestProject.Application.Main (System.String[] args) [0x00008] in /Users/gorken/Projects/TestProject/TestProject/Main.cs:16
TestProjectViewController.cs
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace TestProject
{
public partial class TestProjectViewController : UIViewController
{
public TestProjectViewController () : base ("TestProjectViewController", null)
{
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
}
}
}
TestProjectViewController.designer.cs
using MonoTouch.Foundation;
using System.CodeDom.Compiler;
namespace TestProject
{
[Register ("TestProjectViewController")]
partial class TestProjectViewController
{
[Outlet]
MonoTouch.UIKit.UIImageView imgTest { get; set; }
void ReleaseDesignerOutlets ()
{
if (imgTest != null) {
imgTest.Dispose ();
imgTest = null;
}
}
}
}
AppDelegate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace TestProject
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
TestProjectViewController viewController;
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
viewController = new TestProjectViewController ();
window.RootViewController = viewController;
window.MakeKeyAndVisible ();
return true;
}
}
}
TestProjectViewController.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface TestProjectViewController : UIViewController {
}
@property (retain, nonatomic) IBOutlet UIImageView *imgTest;
@end
TestProjectViewController.m
#import "TestProjectViewController.h"
@implementation TestProjectViewController
- (void)dealloc {
[_imgTest release];
[super dealloc];
}
@end
xib文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TestProjectViewController">
<connections>
<outlet property="imgTest" destination="PLQ-yP-dMH" id="f4J-3q-Qoq"/>
<outlet property="view" destination="6" id="7"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="6">
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="PLQ-yP-dMH">
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</imageView>
</subviews>
<color key="backgroundColor" white="0.75" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
</view>
</objects>
</document>
最佳答案
快速谷歌后,我发现清理和重建解决方案有助于清除此错误 ( https://bugzilla.xamarin.com/show_bug.cgi?id=15002 )。 要清理和重建解决方案,请在 Xamarin Studio 中转到屏幕顶部的“生成”,然后单击“全部清理”。之后,单击“全部重建”。
如果这对您不起作用,请查看“主界面”设置中的 info.plist。那里的错误可能会引发此异常。
此异常的另一个原因可能是当界面生成器中不再存在的控件存在导出时。再次检查设计器文件(如果一切正常,可以使用 Xcode 进行额外检查)。
如果这对您有帮助,请告诉我。祝你好运!
爱与问候, 比约恩
关于c# - 应用程序在模拟器中运行良好,但在设备上抛出 NSUnknownKeyException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708323/