简体   繁体   English

如何获取自定义UIView XIB以在Xamarin iOS Designer中显示?

[英]How do I get custom UIView XIB to show in the Xamarin iOS Designer?

I built a custom control for Xamarin.iOS. 我为Xamarin.iOS构建了自定义控件。 It's just a switch with a label. 只是带有标签的开关。 You can press anywhere on the control to toggle the switch. 您可以按控件上的任意位置来切换开关。

The switch works great and I can see it at Runtime, but I was hoping not to confuse future users of the custom control. 该开关很好用,我可以在运行时看到它,但是我希望不要让以后使用该自定义控件的用户感到困惑。 The control is nothing but a blank box or a red box with an exception at design time, and I can't seem to figure out how to get it to show up. 该控件不过是一个空白框或一个红色框,在设计时有一个例外,我似乎无法弄清楚如何显示它。 The documentation makes no mention of using a XIB file to design the visual elements of your control. 该文档没有提及使用XIB文件来设计控件的可视元素。

I did a little digging and found an objective-c question about this problem: Failed to render instance of ClassName: The agent threw an exception loading nib in bundle 我做了一些挖掘,发现了一个关于此问题的Objective-C问题: 无法呈现ClassName实例:代理在捆绑中装入笔尖时抛出异常

Unfortunately I can't find a C# equivalent of the code mentioned. 不幸的是,我找不到与上述代码等效的C#。 What needs to be done to get my control to show at Design Time in the Xamarin iOS designer? 要使我的控件在Xamarin iOS设计器中的“设计时”显示,需要做些什么?

Here's the code behind: 这是背后的代码:

/// <summary>
/// Labeled switch is a custom control
/// </summary>
[DesignTimeVisible(true)]
public partial class LabeledSwitch : UIView, IComponent
{
    public LabeledSwitch (IntPtr handle) : base (handle)
    {
        Initialize(CGRect.Empty);
    }

    public LabeledSwitch (CGRect frame) : base(frame)
    {
        Initialize(frame);
    }

    #region IComponent Impl

    public ISite Site { get ; set; }
    public event EventHandler Disposed;

    #endregion
    [Export("Text"), Browsable(true)]
    public string Text { get; set; }

    public UISwitch LabelSwitch { get; set; }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();
        Initialize(CGRect.Empty);
    }

    private void Initialize(CGRect frame)
    {

        if ((Site != null) && Site.DesignMode)
        {
            // Bundle resources aren't available in DesignMode?
            // Thought this might work but it does nothing
            var bundle = NSBundle.FromIdentifier("LabeledSwitch");
            bundle.LoadNib("LabeledSwitch", this, null);
            return;
        }

        BackgroundColor = UIColor.Clear;
        NSBundle.MainBundle.LoadNib("LabeledSwitch", this, null);

        AddSubview(RootView);

        SetTouchBehaviorOfLabeledSwitch();

        Frame = Bounds;
        RootView.Frame = Bounds;

        Label.Text = Text;

        LabelSwitch = Switch;

        HeightAnchor.ConstraintEqualTo(40f);
        RefreshSwitchBackground();
    }

    private void SetTouchBehaviorOfLabeledSwitch()
    {
        BackgroundView.TouchUpInside += TouchedLabeledSwitch;
    }

    private void TouchedLabeledSwitch(object sender, EventArgs e)
    {
        Switch.SetState(!Switch.On, true);
        RefreshSwitchBackground();
    }

    private void RefreshSwitchBackground()
    {
        if (Switch.On)
        {
            BackgroundView.BackgroundColor = UIColor.FromRGB(191, 249, 191);
            return;
        }
        BackgroundView.BackgroundColor = UIColor.FromRGB(239, 239, 244);
    }
}

And here's the xib: 这是xib:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <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="LabeledSwitch"> <connections> <outlet property="BackgroundView" destination="4" id="name-outlet-4"/> <outlet property="Switch" destination="5" id="name-outlet-5"/> <outlet property="Label" destination="6" id="name-outlet-6"/> <outlet property="RootView" 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" customColorSpace="calibratedWhite" colorSpace="calibratedWhite" white="0" alpha="0"/> <subviews> <view contentMode="scaleToFill" id="4" translatesAutoresizingMaskIntoConstraints="NO" customClass="UIControl"> <rect key="frame" x="0.0" y="0.0" width="600" height="600"/> <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/> <userDefinedRuntimeAttributes> <userDefinedRuntimeAttribute keyPath="layer.cornerRadius" type="number"> <real key="value" value="5"/> </userDefinedRuntimeAttribute> </userDefinedRuntimeAttributes> <subviews> <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" id="5" translatesAutoresizingMaskIntoConstraints="NO" userInteractionEnabled="NO"> <rect key="frame" x="533" y="285" width="51" height="31"/> <constraints> <constraint id="16" firstItem="5" firstAttribute="height" constant="31"/> <constraint id="17" firstItem="5" firstAttribute="width" constant="49"/> </constraints> <accessibility key="accessibilityConfiguration"> <accessibilityTraits key="traits" button="YES" notEnabled="YES"/> </accessibility> <connections> <action selector="HandleSwitchStateChanged:" destination="-1" id="20" eventType="valueChanged"/> </connections> </switch> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="6" translatesAutoresizingMaskIntoConstraints="NO"> <rect key="frame" x="16" y="290" width="42" height="21"/> <fontDescription key="fontDescription" type="system" pointSize="17"/> <color key="textColor" colorSpace="calibratedWhite" white="0.333333333333333" alpha="1"/> <nil key="highlightedColor"/> <constraints> <constraint id="8" firstItem="6" firstAttribute="height" constant="21"/> </constraints> <accessibility key="accessibilityConfiguration"> <accessibilityTraits key="traits" staticText="YES" notEnabled="YES"/> </accessibility> </label> </subviews> <constraints> <constraint id="7" firstItem="6" firstAttribute="leading" secondItem="4" secondAttribute="leading" constant="16"/> <constraint id="9" firstItem="6" firstAttribute="centerY" secondItem="4" secondAttribute="centerY"/> <constraint id="14" firstItem="4" firstAttribute="trailing" secondItem="5" secondAttribute="trailing" constant="18"/> <constraint id="15" firstItem="5" firstAttribute="centerY" secondItem="4" secondAttribute="centerY"/> </constraints> </view> </subviews> <constraints> <constraint id="10" firstItem="4" firstAttribute="top" secondItem="1" secondAttribute="top"/> <constraint id="11" firstItem="4" firstAttribute="leading" secondItem="1" secondAttribute="leading"/> <constraint id="12" firstItem="4" firstAttribute="bottom" secondItem="1" secondAttribute="bottom"/> <constraint id="13" firstItem="4" firstAttribute="trailing" secondItem="1" secondAttribute="trailing"/> </constraints> </view> </objects> <resources> <!-- ... Image tags are here--> </resources> </document> 

设计师例外

I've had to mess around with this a little myself, Xamarin isn't always perfect, I find re-registering the class sometimes forces it to appear in the toolbox and in the storyboard. 我本人必须对此稍作改动,Xamarin并不总是完美的,我发现重新注册课程有时会迫使其出现在工具箱和情节提要中。 (although I don't believe you should have to do this). (尽管我不认为您应该这样做)。 This code would sit above your class and within your namespace. 该代码将位于您的类上方和名称空间内。

[DesignTimeVisible(true), Category("Controls")]
[Register("MyView")]

If you ever have any concerns over how you should be formatting and creating controls theres an awesome github Here . 如果您对如何格式化和创建控件有任何疑问,可以在这里找到很棒的github Here These are all open source user and xamarin contributed controls, really well put together, and if you look at some of the classes can give you the missing bits of information about how to properly create design time viewable controls in Xamarin. 这些都是开放源代码用户和xamarin贡献的控件,它们确实很好地组合在一起,如果您看一些类,可以为您提供有关如何在Xamarin中正确创建设计时可见控件的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM