简体   繁体   English

Xamarin Forms 清除所有条目

[英]Xamarin Forms Clear All Entries

Is there a way to clear all entries in an Xamarin.Forms app?有没有办法清除 Xamarin.Forms 应用程序中的所有条目? I checked this link here but it did not help.在这里检查了这个链接,但它没有帮助。

I have used @Bassies code, modified to clear entries from layout dynamically.我使用了@Bassies 代码,经过修改以动态清除布局中的条目。 So, you do not need to add reference in code behind.因此,您不需要在后面的代码中添加引用。 This method will loop through each items and clear text.此方法将遍历每个项目和明文。 So, while comparing @Bassies way, this dynamic method will take little more time to process.因此,在比较@Bassies 方式时,这种动态方法将花费更多时间来处理。 But it will be negligible time as single page view will not contain huge amount of views (like 10000 or more)但这将是微不足道的时间,因为单个页面视图不会包含大量视图(例如 10000 或更多)

 public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            var child = this.Content as Layout;
            if (child != null)
            {
                ProcessLayoutChildren(child);
            }
        }

        public void ProcessLayoutChildren(Layout child)
        {
            foreach (var item in child.Children)
            {
                var layout = item as Layout;
                if (layout != null)
                {
                    ProcessLayoutChildren(layout);
                }
                else
                {
                    ClearEntry(item);
                }
            }

            void ClearEntry(Element entryElement)
            {
                var entry = entryElement as Entry;
                if (entry != null)
                {
                    entry.Text = string.Empty;
                }
            }
        }
    }

I haven't tested this but it should work我没有对此进行测试,但它应该可以工作

You can iterate through all the child views in your layout and clear the entries inside of it.您可以遍历布局中的所有子视图并清除其中的条目。

If you are using XAML, give your layout a name so you could access it in the backend code如果您使用的是 XAML,请为您的布局命名,以便您可以在后端代码中访问它

<StackLayout x:Name="MyForm">
    <Label Text="Anything"/>
    <Entry />
    <Entry />
    <Entry />
    <Button Text="Clear" x:Name="Clear" Clicked="Clear_Clicked" />

</StackLayout>

Then in your Backend:然后在你的后端:

private void Clear_Clicked(object sender, EventArgs e)
{
    foreach (View view in MyForm.Children)
    {

        if(view is Entry)
        {
            (view as Entry).Text = String.Empty;
        }
    }

}

Maybe not the right way but an example how it works on 1 Page.也许不是正确的方法,而是它如何在 1 Page 上工作的示例。

You might want to change it, but give's an idea how it works您可能想更改它,但请了解它是如何工作的

<StackLayout>
    <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
        <Label Text="Clear All" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
    </Frame>

    <Entry x:Name="Nr1" />
    <Entry x:Name="Nr2" />
    <Entry x:Name="Nr3" />
    <Button Text="Clear" Clicked="Button_Clicked" />

</StackLayout>

And Make a List and give the Entry's a Name, i use Nr1, Nr2 and Nr3 but only for example.并制作一个列表并为条目命名,我使用 Nr1、Nr2 和 Nr3,但仅作为示例。

public partial class MainPage : ContentPage
{
    List<Entry> entry = new List<Entry>();
    public MainPage()
    {
        InitializeComponent();
        entry.Clear();
        entry.Add(Nr1);
        entry.Add(Nr2);
        entry.Add(Nr3);

    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        foreach (var entry in entry)
        {

            entry.Text = "";
        }

    }
}

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

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