简体   繁体   English

xamarin forms C#

[英]xamarin forms C#

Currently following tutorials and learning xamarin forms to create my first app.目前正在按照教程学习 xamarin forms 来创建我的第一个应用程序。 I'm not sure what the correct name of the tool is for what I am looking so any guidance would be great thank you.我不确定该工具的正确名称是什么,所以任何指导都会非常感谢您。

So I want a button...'Groceries'....When it is clicked it will show 3 buttons below the groceries button 'Bread', 'Milk, 'Sweets'...From here if user was to choose sweets for example existing, then 'Chocolate', 'Candy','Gum' would appear.所以我想要一个按钮......'杂货'......当它被点击时,它会在杂货按钮'面包','牛奶,'糖果'下面显示 3 个按钮......如果用户要选择糖果,请从这里示例存在,然后会出现“巧克力”、“糖果”、“口香糖”。

So everytime the user selects an option it will indent showing new buttons, with new options, but also still displaying the previous selected buttons, allowing the user to go back, if they change their mind.因此,每次用户选择一个选项时,它都会缩进显示新按钮和新选项,但仍会显示以前选择的按钮,如果用户改变主意,则允许用户返回 go。

I reailse this is not a programming specific Q, but I am not sure of the tutorial I should be looking for, for this.我知道这不是一个特定于编程的 Q,但我不确定我应该为此寻找的教程。 Thank you谢谢

Actually, there are many different solution which can implement it.实际上,有许多不同的解决方案可以实现它。 As @Hobby Dev said you can set the property IsVisible (maybe will use MVVM).正如@Hobby Dev所说,您可以设置属性IsVisible (也许会使用 MVVM)。 Since you are new to Xamarin, I provide one of the solutions which is easy to understand.由于您是 Xamarin 的新手,因此我提供了一种易于理解的解决方案。

in xaml在 xaml

<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>

in code behind在后面的代码中

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;
        }


    }
}

I used static data just for demo, and you can get the data from database or webservice.我使用 static 数据只是为了演示,您可以从数据库或 web 服务中获取数据。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM