在Xamarin中为程序动态添加按钮

问题描述 投票:0回答:2

我正在尝试向Xamarin表单中动态添加的按钮添加代码。如何为以下代码创建的按钮添加功能性

   private async void Buttons(object sender, EventArgs e) {
        await Navigation.PushModalAsync(new ContentPage {
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    new Button { Text = "Button1" },
                    new Button { Text = "Button2" },
                    new Button { Text = "Button3" }
                }
            }
        });
    }
c# xamarin xamarin.forms xamarin.android
2个回答
0
投票

请检查下面的代码,希望对您有帮助。

var MainLayout = new StackLayout()
        {
            BackgroundColor = Color.Red
        };

        var button1 = new Button()
        {
            Text = "Button 1"                
        };

        var button2 = new Button()
        {
            Text = "Button 2"
        };

        var button3 = new Button()
        {
            Text = "Button 3"
        };

        button1.Clicked += Button1_Clicked;
        button2.Clicked += Button2_Clicked;
        button3.Clicked += Button3_Clicked;

        MainLayout.Children.Add(button1);
        MainLayout.Children.Add(button2);
        MainLayout.Children.Add(button3);

        Content = MainLayout;

按钮单击方法

private void Button1_Clicked(object sender, EventArgs e)
    {
        DisplayAlert("Alert","Button 1 Clicked", "Ok");
    }

    private void Button2_Clicked(object sender, EventArgs e)
    {
        DisplayAlert("Alert", "Button 2 Clicked", "Ok");
    }

    private void Button3_Clicked(object sender, EventArgs e)
    {
        DisplayAlert("Alert", "Button 3 Clicked", "Ok");
    }

输出:

enter image description here

谢谢


0
投票

如果您想将Button与事件一起使用(通常是不使用ViewModel的方法),则根本没有办法像您那样使用初始化程序。您无法通过对象初始化程序预订事件

您必须从数组初始化程序的外部创建按钮

private void CreateContent()
{
    var button1 = new Button()
                      { 
                          Text = "Button 1"
                      };
    button1.Click += Button1_OnClick;

    var button2 = new Button()
                      { 
                          Text = "Button 2"
                      };
    // and so on
    return new ContentPage()
    {
        Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = { button1, button2, button3 }
                }
    }
}

private void Button1_OnClick(object sender, EventArgs eventArgs)
{
    // handle button 1 bein clicked
}

无论如何,您将在页面中将事件处理程序导航到下一页,这比在显示控件的页面中使事件处理程序糟糕。我宁愿子类ContentPage并从构造函数创建内容。然后,您可以在该类中具有事件处理程序。调用页面不必了解其导航到的页面的内部(内容或行为)。即使您不想使用MVVM(我建议这样做),也可以考虑使用XAML创建新页面。这样,您可以在MyPage.xaml

中使用简单属性订阅事件
<StackLayout>
    <Button Text="Button 1" Click="Button1_OnClick" />
    <Button Text="Button 2" Click="Button2_OnClick" />
    <Button Text="Button 3" Click="Button3_OnClick" />
</StackLayout>

以及您的MyPage.xaml.cs

public class MyPage : ContentPage
{
    // ...

    public void Button1_OnClick(object sender, EventArgs args)
    {
        // whatever
    }

    public void Button2_OnClick(object sender, EventArgs args)
    {
        // whatever
    }

    public void Button3_OnClick(object sender, EventArgs args)
    {
        // whatever
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.