如何将外部API的属性导入Script# [英] How to import properties of an external API into Script#

查看:122
本文介绍了如何将外部API的属性导入Script#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2010中使用Script#来导入HTML5 Canvas元素的API.

I'm using Script# inside Visual Studio 2010 to import the API for the HTML5 Canvas element.

它非常适合FillRect(),MoveTo(),LineTo()等.我声明了以下接口,然后可以在C#中对其进行编码.然后,Script#将其很好地转换为JavaScript.

Its working great for things like FillRect(), MoveTo(), LineTo() and so on. I've declared the following interface and then I can code against it in C#. Then, Script# converts it to JavaScript nicely.

public interface ICanvasContext
{
    void FillRect(int x, int y, int width, int height);
    void BeginPath();
    void MoveTo(int x, int y);
    void LineTo(int x, int y);
    void Stroke();
    void FillText(string text, int x, int y);
}

我想包括一个采用简单字符串的StrokeStyle属性,但是我看不到如何通过接口来实现.以下接口属性在JavaScript中创建一个前缀,这将导致它失败.生成的JavaScript与HTML5 Canvas API不匹配.

I want to include the StrokeStyle property that takes a simple string, but I don't see how to do this with an interface. The following interface properties create a prefix in the JavaScript, which causes it to fail. The resulting JavaScript will not match the HTML5 Canvas API.

string StrokeStyle { get; set; }
string Font { get; set; }

先前的属性将创建此JavaScript:

The previous property will create this JavaScript:

ctx.set_strokeStyle('#FF0');

如何获取Script#来生成没有get_/set_前缀的画布上下文的简单分配属性?

How can I get Script# to generate the simple assignment properties of the canvas context without the get_/set_ prefix?

推荐答案

知道了!我使用的接口在某些情况下很好,但是当我需要该字段时,我不得不切换到抽象类,以免出现编译错误.

Got it! I was using an interface, which is fine for some cases, but when I needed the field, I had to switch to an abstract class so as not to get the compilation error.

public abstract class Canvas : DOMElement
{
    public abstract CanvasContext GetContext(string dimension);
}

public abstract class CanvasContext
{
    public abstract void FillRect(int x, int y, int width, int height);
    public abstract void BeginPath();
    public abstract void MoveTo(int x, int y);
    public abstract void LineTo(int x, int y);
    public abstract void Stroke();
    public abstract void FillText(string text, int x, int y);

    public string StrokeStyle;
    public string Font;
}

这两个抽象类让我使用Script#中的HTML5 Canvas API,如下所示:

Those two abstract classes let me use the HTML5 Canvas API from Script#, like so:

public class MySample
{
    public void DoWork(string canvasId)
    {
        Canvas canvas = (Canvas) Document.GetElementById(canvasId);
        CanvasContext ctx = canvas.GetContext("2d");

        // ctx.FillRect(10,20,100,300);

        ctx.BeginPath();
        ctx.MoveTo(10, 10);
        ctx.LineTo(100, 300);
        ctx.MoveTo(20,10);
        ctx.LineTo(559,300);
        ctx.StrokeStyle = "#F00";
        ctx.Stroke();
    }
}

这篇关于如何将外部API的属性导入Script#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆