简体   繁体   English

如何在Interface Builder文件中使用自定义iOS视图(位于引用的类库中)?

[英]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.

相关问题 在 Interface Builder 中使用泛型类作为自定义视图 - Use a generic class as a custom view in Interface Builder iOS:如何在Interface Builder中使用自定义包中的图像? - iOS: How to use images in custom bundle in Interface Builder? iOS在界面生成器中使用继承的View - IOS use inherited View inside interface builder 如何在 iOS Interface Builder 的子文件夹中使用图像 - How to use images in subfolders in iOS Interface Builder 从捆绑包加载自定义View Controller失败-“ Interface Builder文件中的未知类” - Loading custom View Controllers from a Bundle fails - “Unknown class in Interface Builder file” 界面生成器中的iOS Swift 3.0自定义视图导致重新编译和放错位置 - iOS Swift 3.0 custom view in interface builder causes recompiling and misplacements iOS版。 我可以在Interface Builder中为UIScrollView制作自定义视图吗? - iOS. Could I made custom view for UIScrollView in Interface Builder? iOS开发-如何显示使用自定义Table View Controller在Interface Builder中创建的静态单元格? - iOS development - How to display static cells created in Interface Builder with custom Table View Controller? 如何在Interface Builder中使用自定义UIButton? - How to use a custom UIButton with Interface Builder? 如何在Interface Builder中使用自定义UIButton类别? - How to use a custom UIButton category with Interface Builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM