如果您查看 github 上的 script# 源代码,您将看到如何在 jQuery 世界中执行此操作的示例。
答案取决于您是只是尝试导入现有脚本,还是想使用 script# 来编写 mixin 本身。
如果您想简单地导入,我建议您查看源代码中的jQuery 和jQuery.History 项目。 jQuery.History 表示一个 jQuery 历史 mixin(但没有任何扩展方法或接口)。
如果您想在 script# 中编写 mixin,请继续阅读... 高级方法是定义一个静态类并使用 [Mixin] 属性对其进行注释。将来,该方法将更改为使用 c# 扩展方法,但这种自定义方法是您现在需要的。
回到示例 - 在https://github.com/nikhilk/scriptsharp/blob/master/samples/PhotoDemo/Gallery/GalleryPlugin.cs 你会看到:
[Mixin("$.fn")]
public static class GalleryPlugin {
public static jQueryObject Gallery(GalleryPluginOptions customOptions) {
...
// Use jQuery.Current to access the "this" object at runtime pointing
// to the object instance whose prototype now contains the mixin methods.
}
}
这将生成到脚本中:
$.fn.gallery = function(customOptions) {
}
作为参考,jQuery.Current 在开箱即用的 jQuery 库中定义的方式(完整源代码请参见https://github.com/nikhilk/scriptsharp/blob/master/src/Libraries/jQuery/jQuery.Core/jQuery.cs):
[IgnoreNamespace]
[Imported]
[ScriptName("$")]
public sealed class jQuery {
private jQuery() {
}
[ScriptAlias("this")]
[IntrinsicProperty]
public static jQueryObject Current {
get {
return null;
}
}
}
有点涉及,但希望一旦你尝试过,它就会变得简单。开箱即用的脚本# 为 jQuery 场景提供 API 和模板,通过从最前沿移除机制来帮助简化,在使用不同的脚本框架时需要了解这些机制。
希望这会有所帮助!