黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 设备选择的界面
    /// </summary>
    public class SelectDeviceForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备选择确定后的回调函数(此函数在点击确认后,会先关闭界面再执行回调方法,与ActionSelectDeviceEx不共存)
        /// </summary>
        public Action<List<CommonDevice>> ActionSelectDevice = null;
        /// <summary>
        /// 设备选择确定后的回调函数(此函数在点击确认后,不会关闭界面,与ActionSelectDevice不共存,关闭界面请调用CloseForm函数)
        /// </summary>
        public Action<List<CommonDevice>> ActionSelectDeviceEx = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 前回选择的行
        /// </summary>
        private DeviceSelectControl oldSelectRow = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listShowDevice">需要显示设备列表(有些界面需要剔除一些不要的设备)</param>
        /// <param name="i_listSelectDevice">指定哪些设备的初始状态为选中状态(设备的主键)</param>
        /// <param name="SelcetCancel">初始选中状态的设备能否取消 true:可以 false:不允许</param>
        /// <param name="SelectOnlyOne">是否只能选择一个 true:只能选择一个 false:可以多选</param>
        public void ShowForm(List<CommonDevice> i_listShowDevice, List<string> i_listSelectDevice, bool SelcetCancel = true, bool SelectOnlyOne = false)
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uSelectDevice));
 
            //初始化中部信息
            this.InitMiddleFrame(i_listShowDevice, i_listSelectDevice, SelcetCancel, SelectOnlyOne);
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame(List<CommonDevice> i_listShowDevice, List<string> i_listSelectDevice, bool SelcetCancel, bool SelectOnlyOne)
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            if (i_listShowDevice.Count == 0)
            {
                //没有可添加的目标
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotHadAddTarget));
                return;
            }
 
            listView = new VerticalListControl(29);
            listView.Y = Application.GetRealHeight(-6);
            listView.Height = bodyFrameLayout.Height + Application.GetRealHeight(6);
            listView.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(listView);
 
            //添加所有设备行
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                int count = i_listShowDevice.Count - 1;
                for (int i = 0; i < i_listShowDevice.Count; i++)
                {
                    //添加明细行
                    this.AddDeviceRow(i_listShowDevice[i], i_listSelectDevice, SelcetCancel, SelectOnlyOne, i != count);
                }
                //调整列表控件的高度
                listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23), Application.GetRealHeight(6));
            });
 
            //完成
            var btnfinish = new BottomClickButton();
            btnfinish.TextID = R.MyInternationalizationString.uFinish;
            bodyFrameLayout.AddChidren(btnfinish);
            btnfinish.ButtonClickEvent += (sender, e) =>
            {
                List<CommonDevice> listDevice = this.GetListDeviceFromRow();
                if (ActionSelectDeviceEx != null)
                {
                    ActionSelectDeviceEx(listDevice);
                    return;
                }
 
                this.CloseForm();
                if (ActionSelectDevice != null)
                {
                    ActionSelectDevice(listDevice);
                    ActionSelectDevice = null;
                }
            };
        }
 
        #endregion
 
        #region ■ 添加设备行_________________________
 
        /// <summary>
        /// 添加设备行
        /// </summary>
        /// <param name="device">设备</param>
        /// <param name="i_listSelectDevice">指定哪些设备的初始状态为选中状态(设备的主键)</param>
        /// <param name="SelcetCancel">初始选中状态的设备能否取消 true:可以 false:不允许</param>
        /// <param name="SelectOnlyOne">是否只能选择一个 true:只能选择一个 false:可以多选</param>
        /// <param name="addLine">是否添加底线</param>
        private void AddDeviceRow(CommonDevice device, List<string> i_listSelectDevice, bool SelcetCancel, bool SelectOnlyOne, bool addLine)
        {
            string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            var row = new DeviceSelectControl(device, SelectOnlyOne == false, listView.rowSpace / 2);
            row.MainKeys = mainkeys;
            listView.AddChidren(row);
            row.InitControl();
            //底线
            if (addLine == true)
            {
                row.AddBottomLine();
            }
 
            //设置选择状态
            string mainKeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            if (i_listSelectDevice.Contains(mainKeys) == true)
            {
                row.SelectCancel = SelcetCancel;
                row.IsSelected = true;
                if (SelectOnlyOne == true)
                {
                    oldSelectRow = row;
                }
            }
            if (SelectOnlyOne == true)
            {
                row.ButtonClickEvent += (sender, e) =>
                {
                    //取反
                    row.IsSelected = !row.IsSelected;
                    if (oldSelectRow != null && oldSelectRow.MainKeys != row.MainKeys)
                    {
                        oldSelectRow.IsSelected = false;
                        oldSelectRow = null;
                    }
                    if (row.IsSelected == true)
                    {
                        oldSelectRow = row;
                    }
                };
            }
        }
 
        #endregion
 
        #region ■ 获取选中设备_______________________
 
        /// <summary>
        /// 获取选中的设备
        /// </summary>
        /// <returns></returns>
        private List<CommonDevice> GetListDeviceFromRow()
        {
            List<CommonDevice> listDevice = new List<CommonDevice>();
            for (int i = 0; i < this.listView.ChildrenCount; i++)
            {
                var myView = this.listView.GetChildren(i);
                if (myView is DeviceSelectControl)
                {
                    var row = (DeviceSelectControl)myView;
                    if (row.IsSelected == true)
                    {
                        listDevice.Add(row.device);
                    }
                }
            }
            return listDevice;
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            ActionSelectDeviceEx = null;
 
            base.CloseFormBefore();
        }
 
        #endregion
    }
}