xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备选择的界面
    /// </summary>
    public class SelectDeviceForm : UserCenterCommonForm
    {
        /// <summary>
        /// 设备选择确定后的回调函数(此函数在点击确认后,会先关闭界面再执行回调方法,与ActionSelectDeviceEx不共存)
        /// </summary>
        public Action<List<CommonDevice>> ActionSelectDevice = null;
        /// <summary>
        /// 设备选择确定后的回调函数(此函数在点击确认后,不会关闭界面,与ActionSelectDevice不共存,关闭界面请调用CloseForm函数)
        /// </summary>
        public Action<List<CommonDevice>> ActionSelectDeviceEx = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalScrolViewLayout listView = null;
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listShowDevice">需要显示设备列表(有些界面需要剔除一些不要的设备)</param>
        /// <param name="i_listSelectDevice">指定哪些设备的初始状态为选中状态(设备的主键)</param>
        /// <param name="SelcetCancel">初始选中状态的设备能否取消 true:可以 false:不允许</param>
        public void ShowForm(List<CommonDevice> i_listShowDevice, List<string> i_listSelectDevice, bool SelcetCancel = true)
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uSelectDevice));
 
            //完成
            var btnfinish = new TopLayoutFinshView();
            topFrameLayout.AddChidren(btnfinish);
            btnfinish.MouseUpEventHandler += (sender, e) =>
            {
                List<CommonDevice> listDevice = this.GetDeviceFromRow();
                if (ActionSelectDeviceEx != null)
                {
                    ActionSelectDeviceEx(listDevice);
                    return;
                }
 
                this.CloseForm();
                if (ActionSelectDevice != null)
                {
                    ActionSelectDevice(listDevice);
                }
            };
 
            listView = new VerticalScrolViewLayout();
            listView.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listView);
 
            //添加所有设备行
            this.AddAllDeviceRow(i_listShowDevice, i_listSelectDevice, SelcetCancel);
        }
 
        /// <summary>
        /// 添加所有设备行
        /// </summary>
        /// <param name="i_listShowDevice">设备列表</param>
        /// <param name="i_listSelectDevice">指定哪些设备的初始状态为选中状态(设备的主键)</param>
        /// <param name="SelcetCancel">初始选中状态的设备能否取消 true:可以 false:不允许</param>
        private void AddAllDeviceRow(List<CommonDevice> i_listShowDevice, List<string> i_listSelectDevice, bool SelcetCancel = true)
        {
            new System.Threading.Thread(() =>
            {
                foreach (CommonDevice device in i_listShowDevice)
                {
                    Application.RunOnMainThread(() =>
                    {
                        //添加明细行
                        this.AddDeviceRow(device, i_listSelectDevice, SelcetCancel);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 添加设备行
        /// </summary>
        /// <param name="device">设备</param>
        /// <param name="i_listSelectDevice">指定哪些设备的初始状态为选中状态(设备的主键)</param>
        /// <param name="SelcetCancel">初始选中状态的设备能否取消 true:可以 false:不允许</param>
        private void AddDeviceRow(CommonDevice device, List<string> i_listSelectDevice, bool SelcetCancel = true)
        {
            var row = new DeviceSelectRow(listView, device);
 
            //设置选择状态
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            if (i_listSelectDevice.Contains(mainKeys) == true)
            {
                row.SelectCancel = SelcetCancel;
                row.IsSelected = true;
            }
        }
 
        /// <summary>
        /// 获取选中的设备
        /// </summary>
        /// <returns></returns>
        private List<CommonDevice> GetDeviceFromRow()
        {
            List<CommonDevice> listDevice = new List<CommonDevice>();
            for (int i = 0; ; i++)
            {
                var row = (DeviceSelectRow)this.listView.GetChildren(i);
                if (row == null)
                {
                    break;
                }
                if (row.IsSelected == true)
                {
                    listDevice.Add(row.device);
                }
            }
            return listDevice;
        }
    }
}