我正在try 制作多个按钮(添加和删除),它们都使用一个计数器来添加一个数字.按钮的数量取决于列表中的人数,每个人的按钮也应该不同.

我的主要问题是让所有的按钮,而不是只做最后一个,我也希望计数器将使用每个按钮,而不是为该计数器设计的按钮.

这是我第一次使用毛伊岛,我似乎找不到任何东西来帮助我解决我的问题.

private void CreateButton()
{
    string names = "1;2;3";
    var list = names.ToString().Split(';');
    for (int i = 0; i < list.Length; i++)
    {
        Label lblName = new Label { Text = list[i].ToString() };
        var counter = 0;
        Label lblCounter = new Label
        {
            Text = $": {counter}",            
        };
        Button btnAdd = new Button
        {
            Text = $"{list[i]}: add",
            BackgroundColor = Colors.Green,
            BorderColor= Colors.Black,
            Command = new Command
            (
                execute: async() =>
                {
                    counter += 1;
                    lblCounter.Text = $": {counter.ToString()}";
                }
            )
        };
        Button btnRemove = new Button
        {
            Text = $"{list[i]}: remove",
            BackgroundColor = Colors.Red,
            BorderColor = Colors.Black,
            Command = new Command
            (
                execute: async () =>
                {
                    counter -= 1;
                    if (counter <= 0)
                    {
                        counter = 0;
                    }
                    lblCounter.Text = $": {counter.ToString()}";
                }
            )
        };
        Content = new StackLayout
        {
            Children =
            {
                lblName, btnAdd, lblCounter, btnRemove
            }
        };
    }
}

这是创建按钮的完整代码,它只为数字3创建一个按钮.

推荐答案

这里的问题是,您设置了View的Content或循环的Page inside,这意味着您将用每次迭代替换它.

要解决此问题,您需要将Add()个项目添加到StackLayout.Children,而不是每次都用新内容替换:

private void CreateButton()
{
    string names = "1;2;3";
    var list = names.ToString().Split(';');

    var stackLayout = new StackLayout();

    for (int i = 0; i < list.Length; i++)
    {
        Label lblName = new Label { Text = list[i].ToString() };
        var counter = 0;
        Label lblCounter = new Label
        {
            Text = $": {counter}",            
        };
        Button btnAdd = new Button
        {
            Text = $"{list[i]}: add",
            BackgroundColor = Colors.Green,
            BorderColor= Colors.Black,
            Command = new Command
            (
                execute: async() =>
                {
                    counter += 1;
                    lblCounter.Text = $": {counter.ToString()}";
                }
            )
        };
        Button btnRemove = new Button
        {
            Text = $"{list[i]}: remove",
            BackgroundColor = Colors.Red,
            BorderColor = Colors.Black,
            Command = new Command
            (
                execute: async () =>
                {
                    counter -= 1;
                    if (counter <= 0)
                    {
                        counter = 0;
                    }
                    lblCounter.Text = $": {counter.ToString()}";
                }
            )
        };

        stackLayout.Children.Add(lblName);
        stackLayout.Children.Add(lblCounter );
        stackLayout.Children.Add(btnAdd);
        stackLayout.Children.Add(btnRemove);
    }

    Content = stackLayout;
}

我认为您也可以使用Add()扩展方法直接将项目添加到stackLayout中:

stackLayout.Add(btnAdd);

Csharp相关问答推荐

如何从顶部提取发票号作为单词发票后的第一个匹配

为什么我不能更改尚未设置的模拟对象属性的值?

需要深入了解NpgSQL DateTimeOffset处理

有没有一种方法可以防止在编译时在MicrosoftC或非单线程上下文中调用方法?

属性getter和setter之间的空性不匹配?

如何修改中间件或其注册以正确使用作用域服务?

StackExchange.Redis.RedisServerException:调用ITransaction.ExecuteAsync()时出现错误未知命令取消监视

C#DateTime.ParseExact不使用特定日期

我如何让我的秒表保持运行场景而不重置

如何让游戏对象在切换场景时被销毁,但在开始新游戏时重新锁定

为什么我的用户界面对象移动到略低于实际目标?

C# Winforms:从对象树到TreeView的递归转换重复条目

如何将默认区域性更改为fr-FR而不是en-US?

如何对特定异常使用Polly重试机制?

将字符串类型日期输入(yyyy-mm-ddthh:mm:ss)转换为MM/dd/yyyy格式

为什么在使用JsonDerivedType序列化泛型时缺少$type?

Linq SELECT的多条指令

如何在C#中反序列化Java持续时间?

MudRadioGroup不切换

使用c#中的Windows 10关机消息