【问题标题】:xamarin forms C#xamarin 表单 C#
【发布时间】:2020-03-09 02:47:51
【问题描述】:

目前正在遵循教程并学习 xamarin 表单来创建我的第一个应用程序。 我不确定该工具的正确名称是什么,因此任何指导都会非常感谢您。

所以我想要一个按钮......'杂货'......当它被点击时,它将在杂货按钮'面包','牛奶,'糖果'下面显示 3 个按钮......如果用户要从这里选择糖果,例如现有的,然后会出现“巧克力”、“糖果”、“口香糖”。

因此,每次用户选择一个选项时,它都会缩进显示新按钮和新选项,但仍会显示以前选择的按钮,如果用户改变主意,可以返回。

我知道这不是一个特定于编程的 Q,但我不确定我应该寻找的教程。谢谢

【问题讨论】:

  • 创建所有按钮并根据您的需要从代码中更改其 IsVisible 属性。
  • 树视图可能更合适吗?您基本上在这里显示分层数据。
  • @John 你检查我的答案了吗?
  • @LucasZhang-MSFT Yes 对延迟返回表示歉意。现在只能解决这个问题,感谢您的帮助
  • 如果对你有帮助,你可以接受我的回答。

标签: c# xamarin xamarin.forms


【解决方案1】:

实际上,有许多不同的解决方案可以实现它。正如@Hobby Dev所说,您可以设置属性IsVisible(也许会使用MVVM)。由于您是 Xamarin 的新手,因此我提供了一种易于理解的解决方案。

在xml中

<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>

        <StackLayout Grid.Row="0" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
            <Button Text="Groceries" Clicked="Button_Clicked"/>
        </StackLayout>

        <StackLayout x:Name="stack1" Grid.Row="1" Orientation="Horizontal" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">

        </StackLayout>

        <StackLayout x:Name="stack2" Grid.Row="2" Orientation="Horizontal" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">

        </StackLayout>


    </Grid>

</StackLayout>

在后面的代码中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace xxx
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();         
        }


        private void Button_Clicked(object sender, EventArgs e)
        {


            List<MyButton> myButtons = new List<MyButton>() { new MyButton("Sweet", MyButton_Clicked), new MyButton("Candy", MyButton_Clicked), new MyButton("Gum", MyButton_Clicked) };

            stack1.Children.Clear();

            foreach(MyButton myButton in myButtons)
            {
                stack1.Children.Add(myButton);
            }


        }

        private void MyButton_Clicked(object sender, EventArgs e)
        {
            var mybutton = sender as MyButton;
            var title = mybutton.Text;
            List<MyButton> myButtons = new List<MyButton>();

            if (title=="Sweet")
            {
                 myButtons = new List<MyButton>() { new MyButton("111", MyButton_Clicked), new MyButton("222", MyButton_Clicked), new MyButton("333", MyButton_Clicked) };              
            }
            else if (title== "Candy")
            {
                myButtons = new List<MyButton>() { new MyButton("444", MyButton_Clicked), new MyButton("555", MyButton_Clicked), new MyButton("666", MyButton_Clicked) };
            }
            else
            {
                myButtons = new List<MyButton>() { new MyButton("777", MyButton_Clicked), new MyButton("888", MyButton_Clicked), new MyButton("999", MyButton_Clicked) };
            }
            stack2.Children.Clear();

            foreach (MyButton myButton in myButtons)
            {
                stack2.Children.Add(myButton);
            }
        }
    }

    public class MyButton:Button
    {
        public MyButton(string title,EventHandler clicked)
        {
            this.Text = title;
            Clicked += clicked;
        }


    }
}

我使用静态数据只是为了演示,你可以从数据库或webservice中获取数据。

【讨论】:

  • 对于从 Xamarin 开始的人来说很好的答案,但请记住,在 Code Behind 中做事并不理想,MVVM 应该是要走的路,如果有人想尝试,这里有一些链接:@ 987654322@stackoverflow.com/questions/32667408/…forums.xamarin.com/discussion/82775/…
  • 同意你的观点,但考虑到实际,在后面的代码中实现它很容易理解。当然使用MVVM更好:)
  • 啊,谢谢@LucasZhang-MSFT,这是一个很好的开始,谢谢你花时间让我开始运行......如果你知道问题出在哪里,那就碰壁吧......stackoverflow.com/questions/58920807/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2020-10-04
  • 2019-03-20
  • 2018-01-02
  • 2020-04-28
  • 2021-01-31
  • 2018-03-11
相关资源
最近更新 更多