[英]How to use a Custom iOS View(located in a referenced Class Library) in an Interface Builder file?
I have two projects. 我有两个项目。 The first one is the iOS-App which is referencing a class library with the target framework
Xamarin.iOS
. 第一个是iOS-App,它使用目标框架
Xamarin.iOS
引用类库。 In that class library I implemented a CustomView
which inherits from UIView
. 在该类库中,我实现了从
UIView
继承的CustomView
。 I also registered it with the "Register"
attribute. 我还使用
"Register"
属性注册了它。
So in the interface designer I get the prefilled information to use this class as custom class after I dragged the UIView
to my ViewController
. 因此,在界面设计器中,将
UIView
拖动到ViewController
之后,我获得了预填充的信息以将该类用作自定义类。 But when I run the app I get the following information: 但是,当我运行该应用程序时,我得到以下信息:
"Unknown class MyCustomView in Interface Builder file."
If I move the my CustomView
from the class library project into my first project then it will run as expected. 如果我将
CustomView
从类库项目移到我的第一个项目,则它将按预期运行。
Does someone know how to use a CustomView
, which is located in a referenced class library, in an Interface Builder file? 有人知道如何在Interface Builder文件中使用位于引用的类库中的
CustomView
吗?
I don't think it's possible to Register
accross project boundaries. 我认为无法跨越项目边界进行
Register
。 In my tests, even when you use the exact same namespace, you still end up with an error. 在我的测试中,即使使用完全相同的名称空间,也仍然会出现错误。
What is possible, on the other hand, is to use a proxy class and make use of inheritance to bring the desired behaviour to the proxy. 什么是可能的,而另一方面,就是使用代理类,利用继承带来的期望的行为给代理。
Basically, it boils down to this: 基本上,可以归结为:
1) In your library, you define your Custom UIView
and implement any behaviour you want to share: 1)在您的库中,定义您的Custom
UIView
并实现您要共享的任何行为:
public class MyCustomUIView : UIView
{
public MyCustomUIView(IntPtr ptr) : base (ptr)
{
}
public override void AwakeFromNib()
{
// Do something beautiful here...
AddSubview(new UILabel(new CGRect(0,0,200,30)) { Text = "I live in the library"});
BackgroundColor = UIColor.Green;
}
}
Note that we don't Register
this class, because it won't work anyway. 请注意,我们不会
Register
此类,因为它将无法正常工作。
2) Now in your app project, open up the storyboard in interface designer or XCode and assign a completely new name to your placeholder UIView
. 2)现在,在您的应用程序项目中,在界面设计器或XCode中打开情节提要,并为占位符
UIView
分配一个全新的名称 。 In my case, I chose MyCustomUIViewProxy
. 就我而言,我选择了
MyCustomUIViewProxy
。 Xamarin studio will create two files for you: MyCustomUIViewProxy.cs and MyCustomUIViewProxy2.designer.cs. Xamarin Studio将为您创建两个文件:MyCustomUIViewProxy.cs和MyCustomUIViewProxy2.designer.cs。 Note that in the designer.cs file, Xamarin automatically
Register
ed your proxy class. 请注意,在designer.cs文件中,Xamarin自动
Register
您的代理类。
3) Now all we have to do is use inheritance, to make the proxy act like the original: In MyCustomUIViewProxy.cs, we change the base class from UIView
to MyCustomUIView
, and we're good to go: 3)现在我们要做的就是使用继承,以使代理像原始代理一样工作:在MyCustomUIViewProxy.cs中,我们将基类从
UIView
更改为MyCustomUIView
,我们很高兴:
partial class MyCustomUIViewProxy : MyCustomUIView
{
public MyCustomUIViewProxy (IntPtr handle) : base (handle)
{
}
}
That's it: 而已:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.