Я новичок в Xamarin, раньше работал с android, и теперь я пытаюсь создать класс доски, но не могу создать кнопки динамически.
Я пытаюсь сгенерировать кнопки и для некоторыхпричина сейчас я сталкиваюсь с этой проблемой, пытаясь динамически создавать кнопки ... надеюсь, вы могли бы помочь мне:)
в активность :
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
в axml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>