xm
2021-12-01 6d73bf6e816570291865674bef8bce8972e4de3f
HDL_ON/UI/UI2/4-PersonalCenter/PirDevice/AddButton.cs
@@ -4,23 +4,29 @@
using Shared;
namespace HDL_ON.UI.UI2.PersonalCenter.PirDevice
{
    public class AddButton:FrameLayout
    public class AddButton : FrameLayout
    {
        public AddButton()
        {
            Tag = "PirView";
        }
        public void Show()
        public void Show(Control control)
        {
            #region 界面布局
            this.BackgroundColor = CSS.CSS_Color.viewMiddle;
            PirDevice.View.TopView topView = new View.TopView();
            topView.topNameBtn.TextID = StringId.shebeigaunli;
            topView.topNameBtn.TextID = StringId.tianjiayaokongqi;
            this.AddChidren(topView.FLayoutView());
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) => { this.RemoveFromParent(); };
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
                RemoveFromParent();
            };
            FrameLayout editfLayout = new FrameLayout
            {
                Y= topView.frameLayout.Bottom,
                Y = topView.frameLayout.Bottom,
                BackgroundColor = CSS.CSS_Color.textWhiteColor,
                Height = Application.GetRealHeight(152),
                Width = Application.GetRealWidth(375),
@@ -40,9 +46,9 @@
            EditText editText = new EditText()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(375-16*2),
                Width = Application.GetRealWidth(375 - 16 * 2),
                Height = Application.GetRealHeight(44),
                PlaceholderText=Language.StringByID(StringId.anjianmingcheng),
                PlaceholderText = Language.StringByID(StringId.anjianmingcheng),
                PlaceholderTextColor = CSS.CSS_Color.textCancelColor,
                TextColor = CSS.CSS_Color.textColor,
                TextSize = TextSize.text14,
@@ -56,21 +62,21 @@
                Height = Application.GetRealHeight(44),
                Y = Application.GetRealHeight(92),
                X = Application.GetRealWidth(78),
                TextID = StringId.save,
                TextID = StringId.xiayibu,
                TextSize = TextSize.text16,
                TextColor = CSS.CSS_Color.textWhiteColor,
                TextAlignment = TextAlignment.Center,
                BackgroundColor = CSS.CSS_Color.btnSaveBackgroundColor,
                Radius = (uint)Application.GetRealHeight(22),
            };
            editfLayout.AddChidren(saveBtn);
            editfLayout.AddChidren(saveBtn);
            FrameLayout fLayout = new FrameLayout
            {
                Y = editfLayout.Bottom+Application.GetRealHeight(8),
                BackgroundColor = CSS.CSS_Color.textWhiteColor,
                Height = Application.GetRealHeight(667-64-152-8),
                Y = editfLayout.Bottom + Application.GetRealHeight(8),
                Height = Application.GetRealHeight(667 - 64 - 152 - 8),
                Width = Application.GetRealWidth(375),
                BackgroundColor = CSS.CSS_Color.textWhiteColor,
            };
            this.AddChidren(fLayout);
            //推荐按键
@@ -86,68 +92,123 @@
                TextAlignment = TextAlignment.CenterLeft,
            };
            fLayout.AddChidren(titleBtn);
            //动态加载Button按钮父控件
            FrameLayout buttonFLayout = new FrameLayout
            {
                Y = titleBtn.Bottom,
                Height = Application.GetRealHeight(667 - 64 - 152 - 8-12-20),
                Y = Application.GetRealHeight(32),
                Height = Application.GetRealHeight(667 - 64 - 152 - 8 - 12 - 20),
                Width = Application.GetRealWidth(375),
            };
            fLayout.AddChidren(buttonFLayout);
            VerticalScrolViewLayout vv = new VerticalScrolViewLayout();
            buttonFLayout.AddChidren(vv);
            List<string> buttonNameList = new List<string> {
                "电源", "音量+", "音量-", "频道+", "频道-", "上", "下", "左",
                 "右", "静音", "确认", "播放", "退出", "菜单", "暂停", "返回",
                  "停止", "主页", "快进", "快退","定时",
            };
            for (int i = 0; i <7; i++)
            #endregion
            View.Buttons buttons = new View.Buttons();
            var buttonNameList = buttons.GetList("默认按钮");
            buttons.AddButton(buttonFLayout, buttonNameList, (s) =>
            {
                //vv.AddChidren(new FrameLayout { Height = Application.GetRealWidth(16) });
                var currFLayout = new FrameLayout { Height = Application.GetRealWidth(16+ 44+2)};
                vv.AddChidren(currFLayout);
                for (int a =i*3+1,b=0; a<= buttonNameList.Count; a++,b++)
                editText.Text = s;
#if __Android__
                editText.SetSelectionEnd();
#endif
            });
            ///下一步的点击事件
            saveBtn.MouseUpEventHandler += (sender, e) =>
            {
                if (control.status.Count > 29)
                {
                    var name = buttonNameList[a-1];
                    var nameBtn = new Button
                    {
                        Y = Application.GetRealHeight(16),
                        X = Application.GetRealWidth(24 + (98 + 16) * b),
                        Gravity=Gravity.CenterVertical,
                        Width = Application.GetRealWidth(98),
                        Height = Application.GetRealHeight(44),
                        Text = name,
                        TextSize = TextSize.text16,
                        TextColor = CSS.CSS_Color.textColor,
                        TextAlignment = TextAlignment.Center,
                        Radius = (uint)Application.GetRealHeight(8),
                        BorderWidth = 1,
                        BorderColor = CSS.CSS_Color.textCancelColor,
                    };
                    currFLayout.AddChidren(nameBtn);
                    if (a % 3 == 0)
                    {
                        break;
                    }
                    nameBtn.MouseUpEventHandler += (sender, e) =>
                    {
                        editText.Text = nameBtn.Text;
                    };
                    ///超出30个红外宝内存满了会出现异常
                    new PirMethod().ErrorShow(null, "添加按钮超出30个");
                    return;
                }
            }
            #endregion
                var texts = editText.Text.Trim();
                if (string.IsNullOrEmpty(editText.Text))
                {
                    //名称不能为空
                    return;
                }
                var butName = control.status.Find((c) => c.value == texts);
                if (butName != null)
                {
                    //名称已经存在
                    return;
                }
                //new引导界面
                View.ReplicationView replication = new View.ReplicationView();
                replication.Show(this);
                //添加数据对象
                Entity.AttributesStatus buttonObj = new Entity.AttributesStatus();
                string keyName = GetKeyName(control.status);
                buttonObj.key = keyName;
                buttonObj.value = texts;
                PirSend.CodeStudy(control, buttonObj, (mqttdate) =>
                {
                    if (mqttdate != null)
                    {
                        //休眠是为显示引导界面
                        // System.Threading.Thread.Sleep(3000);
                        //移除添加按钮引导界面
                        replication.RemView();
                        var buttonName = control.status.Find((c) => c.value == buttonObj.value);
                        if (buttonName == null)
                        {
                            //添加数据
                            control.status.Add(buttonObj);
                        }
                        //移除添加按钮界面
                        this.RemoveFromParent();
                        //移除之前可能存在的界面
                        PirMethod.RemoveView("AddControl");
                        //加载数据界面
                        AddControl addControl = new AddControl();
                        MainPage.BasePageView.AddChidren(addControl);
                        addControl.Show(control);
                        MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                    }
                    else
                    {
                        replication.RemView();
                        PirMethod method = new PirMethod();
                        method.ErrorShow(null, "添加失败");
                    }
                });
            };
        }
        /// <summary>
        /// 移除界面
        /// </summary>
        public override void RemoveFromParent()
        {
            //刷新指定界面
            PirMethod.RefreshView("PirMain");
            base.RemoveFromParent();
        }
        /// <summary>
        /// 查找唯一健名值
        /// </summary>
        /// <param name="listKeyName">已经存在健名列表</param>
        /// <returns></returns>
        private string GetKeyName(List<Entity.AttributesStatus> listKeyName)
        {
            //目前红外宝学习按钮最多是30个;
            for (int i = 0; i < 50; i++)
            {
                string keyName = "key" + i.ToString();
                ///查找是否存在
                var currKeyName = listKeyName.Find((c) => c.key == keyName);
                if (currKeyName == null)
                {
                    return keyName;
                }
            }
            return "key" + listKeyName.Count;
        }
    }
}