1

我按照原来的android API,用monodroid写了一个很简单的CustomView来画一个矩形。一旦我进入应用程序,它会自动关闭。虽然我用 eclipse 编写了一个纯 android,但它工作正常。或者当我删除 drawRect 方法代码时,它也可以正常工作。有谁知道这个或我做错了什么?

这里附上应用程序代码:

[活动1.cs]

int count = 1;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        LinearLayout layoutRoot = FindViewById<LinearLayout>(Resource.Id.LayoutRoot);
        layoutRoot.AddView(new DrawableView(this));

        button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
    }

[绘图视图.cs]

protected override void OnDraw(Android.Graphics.Canvas canvas)
    {
        base.OnDraw(canvas);
        canvas.DrawRect(new Rect(10, 10, 100, 100), new Paint { Color = Color.Red });
    }

这是我在 Eclipse 中使用的代码:

public class DrawableView extends View {

public DrawableView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    canvas.drawRect(new Rect(10, 10, 110, 110), paint);
}

}

非常感谢。霍华德

4

1 回答 1

1

您需要检查android日志以查看错误是什么:

http://mono-android.net/Documentation/Guides/Android_Debug_Log

于 2011-04-13T14:49:43.817 回答