Skip to main content
 首页 » 编程设计

c#之MonoTouch 绑定(bind)不创建部分类

2024年02月23日17duanxz

我正在尝试执行 MonoTouch 上显示的操作 binding page并向绑定(bind)类添加一些方法。它说我所要做的就是将它们包含在一个单独的文件中定义的部分类中,该文件包含在带有 -s 选项的生成过程中。问题是生成的类不是部分类,当我运行 btouch 时出现以下错误:

$ /Developer/MonoTouch/usr/bin/btouch -v Node.cs -s:Extensions.cs /Developer/MonoTouch/usr/bin/smcs -unsafe -target:library Node.cs -nowarn:436 -out:/var/folders/cy/g71b7mr91rn4slbpq5cm39140000gn/T/u983s550.k1k/temp.dll -r:/Developer/MonoTouch/usr/lib/btouch/btouch.exe Extensions.cs -r:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll
Node.cs(7,19): error CS0260: Missing partial modifier on declaration of type NodeNameSpace.Node'. Another partial declaration of this type exists Extensions.cs(5,30): (Location of the symbol related to previous error) Extensions.cs(5,30): error CS0261: Partial declarations ofNodeNameSpace.Node' must be all classes, all structs or all interfaces Node.cs(7,19): (Location of the symbol related to previous error) Extensions.cs(7,29): error CS0106: The modifier public' is not valid for this item Extensions.cs(10,25): error CS0531:NodeNameSpace.Node.IsExpanded.get': interface members cannot have a definition Extensions.cs(14,25): error CS0531: `NodeNameSpace.Node.IsExpanded.set': interface members cannot have a definition Compilation failed: 5 error(s), 0 warnings btouch: API binding contains errors.

根据 binding 上的 MonoTouch 页面:

Each class produced by the generator from the API definition file is a partial class

Node.cs

using System; 
using MonoTouch.Foundation; 
 
namespace NodeNameSpace 
{ 
    [BaseType (typeof (NSObject))] 
    interface Node 
    { 
        [Export ("expanded")] 
        NSNumber Expanded { get; set; } 
    } 
} 

Extensons.cs

using MonoTouch.Foundation; 
 
namespace NodeNameSpace  
{ 
    public partial class Node 
    { 
        public bool IsExpanded 
        { 
            get 
            { 
                return Expanded.BoolValue; 
            } 
            set 
            { 
                Expanded = NSNumber.FromBool(value); 
            } 
        } 
    } 
} 

请您参考如下方法:

这是一个很长的文档页面,但这应该可以满足您的需求:

$ /Developer/MonoTouch/usr/bin/btouch Node.cs -x:Extensions.cs   
$ ls -l Node.dll  
-rwxr-xr-x  1 sebastienpouliot  staff  5120 30 Sep 13:30 Node.dll 

您的问题是使用 -s,它在将您的接口(interface)编译为分部类时使用(对于扩展方法而言太早)并将绑定(bind)编译为 .dll。

您想要的是-x,其中源文件将仅用于第二次编译(source to .dll)。 -s 选项对枚举 最有用,因为您在界面和最终程序集 (.dll) 中 都需要它们。

要获得进一步的帮助,请执行以下操作:

/Developer/MonoTouch/usr/bin/btouch --help