我正在尝试在Xamarin Studio中创建一个简单的Hello World测试应用程序。我创建了一个没有故事板的空白单视图iPhone应用程序。
我拖着&在View上放一个ImageView并连接它的插座。就这么简单。
当我在模拟器中运行时,它工作正常但在设备上调试时会抛出NSUnknownKeyException。
我检查了通常的嫌疑人:
我仍然得到同样的例外。以下是代码和错误信息。我在这里错过了一个非常明显的事情,或者某种程度上Xcode和Xamarin Studio之间的同步失败了吗?
我感谢任何帮助。
错误日志
MonoTouch.Foundation.MonoTouchException:抛出Objective-C异常。 名称:NSUnknownKeyException原因:[setValue:forUndefinedKey:]:此类不是键值 符合编码的关键imgTest。 at at(包装 托管到原生)MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)在MonoTouch.UIKit.UIWindow.MakeKeyAndVisible() [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:130 在TestProject.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication应用程序,MonoTouch.Foundation.NSDictionary 选项)[0x00038] in /Users/gorken/Projects/TestProject/TestProject/AppDelegate.cs:31 at at(包装器托管到原生) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string [],intptr,intptr)在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 在TestProject.Application.Main(System.String [] args)[0x00008]中 /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>
答案 0 :(得分:0)
快速谷歌后,我发现清理和重建您的解决方案有助于消除此错误(https://bugzilla.xamarin.com/show_bug.cgi?id=15002)。 要清理和重建您的解决方案,请在Xamarin工作室中,转到屏幕顶部的“构建”,然后单击“全部清除”。之后,单击“全部重建”。
如果这对您不起作用,请查看“主界面”设置中的info.plist。错误可能会引发此异常。
此异常的另一个原因可能是Interface Builder中不再存在控件的插座。再次检查设计器文件(如果一切正常,可以使用Xcode进行额外检查)。
如果这有助于您,请告诉我。祝你好运!
爱和问候, 的Björn