无法在 silverlight 中创建按钮数组?

发布于 2024-12-14 20:12:01 字数 2586 浏览 2 评论 0原文

这是我使用的代码,但我不确定为什么它可以通过我在窗口形式中完成的方式来实现。

        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        #region Monday
        for (int i = 0; i < 20; i++)
        {
            btnMonday[i].SetValue(Height, 38);
            btnMonday[i].SetValue(Width, 256);
            btnMonday[i].SetValue(Content, timeslot[i]);
            btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);
            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

更新

我仍然收到这部分代码的错误 ::

 Button[] btnMonday = new Button[20];
            string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

            #region Monday
            for (int i = 0; i < 20; i++)
            {
                btnMonday[i] = new Button();
                btnMonday[i].SetValue(Height, 38);
                btnMonday[i].SetValue(Width, 256);
                btnMonday[i].SetValue(Content, timeslot[i]);

                // Sets dependency properties
                btnMonday[i].SetValue(Grid.ColumnProperty, 0);
                btnMonday[i].SetValue(Grid.RowProperty, i + 1);
                // Adds the dynamically created control to the canvas
                LayoutRoot.Children.Add(btnMonday[i]);
            }

是否有可能出现语法错误?我可以知道如何设置按钮的背景值吗?它似乎不适用于以前的定义背景颜色的样式。我得到的错误::

Error   2   Argument 1: cannot convert from 'double' to 'System.Windows.DependencyProperty' 

以及

Error   1   The best overloaded method match for 'System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty, object)' has some invalid arguments

这几行

        btnMonday[i].SetValue(Height, 38);
        btnMonday[i].SetValue(Width, 256);
        btnMonday[i].SetValue(Content, timeslot[i]);
        btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);

This is the code i using, but i not sure why it can be implement via the way i done in window form.

        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        #region Monday
        for (int i = 0; i < 20; i++)
        {
            btnMonday[i].SetValue(Height, 38);
            btnMonday[i].SetValue(Width, 256);
            btnMonday[i].SetValue(Content, timeslot[i]);
            btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);
            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

update

i still getting error with this portion of code ::

 Button[] btnMonday = new Button[20];
            string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

            #region Monday
            for (int i = 0; i < 20; i++)
            {
                btnMonday[i] = new Button();
                btnMonday[i].SetValue(Height, 38);
                btnMonday[i].SetValue(Width, 256);
                btnMonday[i].SetValue(Content, timeslot[i]);

                // Sets dependency properties
                btnMonday[i].SetValue(Grid.ColumnProperty, 0);
                btnMonday[i].SetValue(Grid.RowProperty, i + 1);
                // Adds the dynamically created control to the canvas
                LayoutRoot.Children.Add(btnMonday[i]);
            }

is there any possibility of syntac error ?? and may i know how to set value of background to button ?? it seem not work well with the previous style of define background color.The error i get ::

Error   2   Argument 1: cannot convert from 'double' to 'System.Windows.DependencyProperty' 

and

Error   1   The best overloaded method match for 'System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty, object)' has some invalid arguments

for this few line

        btnMonday[i].SetValue(Height, 38);
        btnMonday[i].SetValue(Width, 256);
        btnMonday[i].SetValue(Content, timeslot[i]);
        btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果您坚持使用 SetValue 方法,请尝试以下操作:

        btnMonday[i].SetValue(Button.WidthProperty, 38);
        btnMonday[i].SetValue(Button.HeightProperty, 256);
        btnMonday[i].SetValue(Button.ContentProperty, timeslot[i]);

if you insist using SetValue method, try this:

        btnMonday[i].SetValue(Button.WidthProperty, 38);
        btnMonday[i].SetValue(Button.HeightProperty, 256);
        btnMonday[i].SetValue(Button.ContentProperty, timeslot[i]);
墨小沫ゞ 2024-12-21 20:12:01

我认为它一定会出现空引用错误。

您首先声明并分配给 Button 数组。

但是每个按钮都需要分配。

btnMonday[i] = new Button();

I think it's gotta get null reference error.

You first declare and allocate to Button array.

But each button needs allocation.

btnMonday[i] = new Button();

只是偏爱你 2024-12-21 20:12:01
    Button[] btnMonday = new Button[20];
    string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

    #region Monday
    for (int i = 0; i < 20; i++)
    {
        btnMonday[i] = new Button();
        btnMonday[i].Height = 38;
        btnMonday[i].Width = 256;
        btnMonday[i].Content = timeslot[i];

        // Sets dependency properties
        Grid.SetColumn(btnMonday[i], 0);
        Grid.SetRow(btnMonday[i], i + 1);
        // Adds the dynamically created control to the canvas
        LayoutRoot.Children.Add(btnMonday[i]);
    }

试试这个

    Button[] btnMonday = new Button[20];
    string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

    #region Monday
    for (int i = 0; i < 20; i++)
    {
        btnMonday[i] = new Button();
        btnMonday[i].Height = 38;
        btnMonday[i].Width = 256;
        btnMonday[i].Content = timeslot[i];

        // Sets dependency properties
        Grid.SetColumn(btnMonday[i], 0);
        Grid.SetRow(btnMonday[i], i + 1);
        // Adds the dynamically created control to the canvas
        LayoutRoot.Children.Add(btnMonday[i]);
    }

try this

半﹌身腐败 2024-12-21 20:12:01
        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        for (int i = 0; i < 20; i++)
        {
            btnMonday[i] = new Button();
            btnMonday[i].Height = 38;
            btnMonday[i].Width = 256;
            btnMonday[i].Content = timeslot[i];
            btnMonday[i].Margin = new Thickness(0, i * 68, 0, 0);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);

            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

我完全不明白你的问题,但你的代码无法编译。如上所述,我已正确修复它。

        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        for (int i = 0; i < 20; i++)
        {
            btnMonday[i] = new Button();
            btnMonday[i].Height = 38;
            btnMonday[i].Width = 256;
            btnMonday[i].Content = timeslot[i];
            btnMonday[i].Margin = new Thickness(0, i * 68, 0, 0);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);

            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

I don't understand your question totally, but your code dosen't compile. As above, I fixed it properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文