从内容页面中的视图访问控件 [英] Access controls from view in content page

查看:43
本文介绍了从内容页面中的视图访问控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考我以前的 我在这里输入名称,并有一个保存工具栏按钮.当我看到输入字段时,将保存"图标放置在我的内容页面上.单击保存"后,我想将文本框中的数据保存到XML文件中.由于我的保存点击事件在我的内容页面中,因此我无法从视图中输入文本.

With reference to my previous I have a entry field here say name and I have a save toolbar button. save icon is placed on my content page while entry field is in my view. On click of save I would like to save the data in text-box to an XML file. Since my save click event is in my content page I cannot get text entered from my view.

这是代码段:

查看页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;

using Xamarin.Forms;

namespace ____
{
    public class View1 : ContentView
    {
        public static Entry txtName;

        public View1()
        {

            //Name
            Label lblName = new Label { Text = "Name", FontAttributes = FontAttributes.Bold, Style = (Style)Application.Current.Resources["LabelStyle"] };

            txtName = new Entry { Style = (Style)Application.Current.Resources["entryStyle"] };


            StackLayout stName = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
                Children ={ 
                    lblName,
                    txtName
                }
            };

            var scrollview = new ScrollView
            {
                Content = new StackLayout
                {
                    Padding = new Thickness(0, 20, 0, 20),
                    //VerticalOptions = LayoutOptions.StartAndExpand,
                    Children = {stName
                }
                }
            };
            Content = new StackLayout
            {
                Padding = new Thickness(0, 20, 0, 20),
                VerticalOptions = LayoutOptions.StartAndExpand,
                Children =
                {

                    scrollview

                }
            };


        }
    }
}

内容页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;

using Xamarin.Forms;

namespace ___
{
    public class Page1 : ContentPage
    {
         Button btnDetails, btnN, btnT;
        View1 myview1, myview2, myview3;
        public Page1()
        {
            //Primary Items
            ToolbarItem Save = new ToolbarItem();
            Save.Text = "Save";
            Save.Clicked += OnClick_Save;
            Save.Order = ToolbarItemOrder.Primary;
            Save.Icon = Device.OnPlatform("Icons/save.png", "save.png", "Toolkit.Content/save.png");

            ToolbarItem Cancel = new ToolbarItem();
            Cancel.Text = "Cancel";
            //Cancel.Clicked += OnClick_Cancel;
            Cancel.Order = ToolbarItemOrder.Primary;
            Cancel.Icon = Device.OnPlatform("Icons/cancel.png", "cancel.png", "Images/cancel.png");


            ToolbarItems.Add(Cancel);
            ToolbarItems.Add(Save);


            StackLayout stHeader = new StackLayout
            {
                Children = { 
                                new Label {
                                    Text= "--------", FontAttributes=FontAttributes.Bold, FontSize=25, TextColor=Color.FromHex("#2C3E50")
                                }
                            },
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = new Thickness(10, 0, 0, 10)
            };

            btnDetails = new Button { Text = "Details", HorizontalOptions = LayoutOptions.FillAndExpand };
            btnN = new Button { Text = "---", HorizontalOptions = LayoutOptions.FillAndExpand };

            btnT = new Button { Text = "---", HorizontalOptions = LayoutOptions.FillAndExpand };

            Grid objGrid = new Grid();
            objGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });

            objGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
            objGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

            //objGrid.RowDefinitions.Add( new RowDefinition { Height = GridLength.Auto });
            //
            objGrid.ColumnDefinitions.Add(new ColumnDefinition
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            objGrid.ColumnDefinitions.Add(new ColumnDefinition
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            objGrid.ColumnDefinitions.Add(new ColumnDefinition
            {
                Width = new GridLength(1, GridUnitType.Star)
            });

            objGrid.Children.Add(stHeader, 0, 0);
            Grid.SetColumnSpan(stHeader, 3);

            btnDetails.Clicked += ButtonClicked;
            objGrid.Children.Add(btnDetails, 0, 1);//col, row

             btnN.Clicked += ButtonClicked;
             objGrid.Children.Add(btnN, 1, 1);

             //btn3 = new Button { Text = "3" };
             btnT.Clicked += ButtonClicked;
             objGrid.Children.Add(btnT, 2, 1);

            myview1 = new View1();
            myview2 = new View1();
            myview3 = new View1();

            objGrid.Children.Add(myview1, 0, 2);
            objGrid.Children.Add(myview2, 0, 2);
            objGrid.Children.Add(myview3, 0, 2);

            Grid.SetColumnSpan(myview1, 3);
            Grid.SetColumnSpan(myview2, 3);
            Grid.SetColumnSpan(myview3, 3);

            Content = objGrid;
            SelectButton(btnDetails);
        }

        private void OnClick_Save(object sender, EventArgs e)
        {
            var s = View1.txtName.Text;

        }

        void SelectButton(Button button)
        {
            View1 view = null;
            if (button == btnDetails)
                view = myview1;
            if (button == btnN)
                view = myview2;
            if (button == btnT)
                view = myview3;
            myview1.IsVisible = myview1 == view;
            myview2.IsVisible = myview2 == view;
            myview3.IsVisible = myview3 == view;
            btnDetails.TextColor = (btnDetails == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
            btnN.TextColor = (btnN == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
            btnT.TextColor = (btnT == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
            btnDetails.BackgroundColor = (btnDetails == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
            btnN.BackgroundColor = (btnN == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
            btnT.BackgroundColor = (btnT == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
        }

        void ButtonClicked(object sender, EventArgs e)
        {
            SelectButton((Button)sender);
        }
    }
}

如何在内容页面的保存按钮单击中输入文本框值?

How can I get entered text-box value in my content page save button click?

推荐答案

使用实例成员引用代替静态引用:

Use an instance member reference instead of a static reference:

  1. 在您的View1类中,将public static Entry txtName;更改为 public Entry txtName;.
  2. Page1类添加新字段View1 _currentView;.
  3. SelectButton()方法的末尾,添加_currentView = view;.
  4. 最后,在OnClick_Save()中,将var s = View1.txtName.Text;更改为var s = _currentView.txtName.Text;.
  1. In your View1 class, change public static Entry txtName; to public Entry txtName;.
  2. Add a new field View1 _currentView; to the Page1 class.
  3. At the end of the SelectButton() method, add _currentView = view;.
  4. And finally, in OnClick_Save(), change var s = View1.txtName.Text; to var s = _currentView.txtName.Text;.

这篇关于从内容页面中的视图访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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