如何在 Xamarin 中动态添加 UI 元素(如“选择器")以查看? [英] How to add UI elements (like "picker") to View dynamically in Xamarin?

查看:21
本文介绍了如何在 Xamarin 中动态添加 UI 元素(如“选择器")以查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在图像 disscriptions 中提到的,我有更新注册表,我想更新我的用户组,但现在我只能用一个组来做到这一点.我应该能够将此用户添加到多个组.为此,我需要多个选择器,就像在图像中一样,我需要根据用户的需要在屏幕中动态弹出选择器.也许用户会想要选择一组或可能想要三组.

As I mentinoned in image discriptions, I have update register form and I want to update groups of my User, But for now I can do that with just one group. I should be able to add this User to multiple groups. For that I need multiple Pickers and like in the images I need to pop up pickers in the screen dynamically according to needs of the User. Maybe the User will want to select one group or maybe want to three groups.

我在这里问的是,如何在应用程序运行时动态添加此选择器或任何 UI 元素.最后一个问题是,用户可能想要从视图中删除第二个或第三个选择器.取消选择我的意思.我怎样才能做到这一点.感谢您的想法和代码.

What I am asking here is, How can I add this pickers or any UI element dynamically while app is running. And last question is, user can ,maybe, want to remove that second or third picker from the view. Deselect I mean. How can I do that. Thanks for ideas and codes.

推荐答案

可以实现的解决方案很多.例如,您可以检查以下代码.

There are many solutions which can implement it . For example you can check the following code .

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="xxx.PickerView">
  <ContentView.Content>

        <Grid HeightRequest="40">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="0.9*" />
                <ColumnDefinition Width="0.1*" />

            </Grid.ColumnDefinitions>

            <Picker Grid.Column="0" x:Name="picker" Title="Select Groups" TitleColor="Red"  />

            <Label Grid.Column="1" Text="Canel" TextColor="Red">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />

                </Label.GestureRecognizers>
            </Label>

        </Grid>

    </ContentView.Content>
</ContentView>

using System;
using System.Collections.ObjectModel;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace xxx
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PickerView : ContentView
    {
        public ObservableCollection<string> pickerSource { get; set; }

        //public PickerView()
        //{
        //    InitializeComponent();
        //}

        public PickerView(ObservableCollection<string> source)
        {
            InitializeComponent();

            picker.ItemsSource = source;
        }

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            var stack = this.Parent as StackLayout;

            stack.Children.Remove(this);
        }
    }
}

在内容页面

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

    <StackLayout x:Name="pickerStack" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        
        
        
    </StackLayout>

    <Button Text="+ add another group" Clicked="Button_Clicked" />

</StackLayout>

private void Button_Clicked(object sender, EventArgs e)
{
   var source = new ObservableCollection<string>() {"111","222","333" };

   pickerStack.Children.Add(new PickerView(source));
}

这篇关于如何在 Xamarin 中动态添加 UI 元素(如“选择器")以查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆