forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboBoxHelper.cs
More file actions
147 lines (129 loc) · 5.22 KB
/
ComboBoxHelper.cs
File metadata and controls
147 lines (129 loc) · 5.22 KB
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
using Android.Content;
using Android.Views;
using Android.Widget;
using Google.Android.Material.Dialog;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ListPopupWindow = AndroidX.AppCompat.Widget.ListPopupWindow;
namespace System.Application.UI
{
/// <summary>
/// 实现类似 WPF/UWP ComboBox | HTML Select 的列表框助手类
/// <para>https://docs.microsoft.com/zh-cn/windows/apps/design/controls/combo-box</para>
/// </summary>
public static class ComboBoxHelper
{
/// <summary>
/// 使用弹窗(<see cref="MaterialAlertDialogBuilder"/>)的实现
/// </summary>
/// <param name="context"></param>
/// <param name="items"></param>
/// <param name="click"></param>
public static void Dialog(Context context, string[] items, Action<string> click)
{
new MaterialAlertDialogBuilder(context).SetItems(items, (_, e) =>
{
if (e.Which >= 0 && e.Which < items.Length)
{
var item = items[e.Which];
click(item);
}
}).Show();
}
/// <summary>
/// 使用弹出菜单(<see cref="ListPopupWindow"/>)的实现
/// </summary>
/// <param name="context"></param>
/// <param name="items"></param>
/// <param name="click"></param>
/// <param name="anchorView">弹出的描点</param>
public static ListPopupWindowWrapper<T> Popup<T>(Context context, IEnumerable<T> items, Action<T> click, View anchorView)
{
// https://material.io/components/menus/android#dropdown-menus
ListPopupWindow listPopupWindow = new(context, null, Resource.Attribute.listPopupWindowStyle);
// Set button as the list popup's anchor
listPopupWindow.AnchorView = anchorView;
var adapter = CreateArrayAdapter(context, items: items);
listPopupWindow.SetAdapter(adapter);
ListPopupWindowWrapper<T> wrapper = new(items, listPopupWindow, adapter, click);
listPopupWindow.SetOnItemClickListener(wrapper);
return wrapper;
}
public sealed class ListPopupWindowWrapper<T> : Java.Lang.Object, AdapterView.IOnItemClickListener
{
public ListPopupWindow Window { get; }
public ArrayAdapter Adapter { get; }
IEnumerable<T> items;
public IEnumerable<T> Items
{
get => items;
set
{
if (items.SequenceEqual(value)) return;
Adapter.Clear();
Adapter.AddAll((ICollection)value);
Adapter.NotifyDataSetChanged();
items = value;
}
}
public Action<T> ItemClick { get; }
public ListPopupWindowWrapper(IEnumerable<T> items, ListPopupWindow listPopupWindow, ArrayAdapter adapter, Action<T> click)
{
this.items = items;
Window = listPopupWindow;
Adapter = adapter;
ItemClick = click;
}
void AdapterView.IOnItemClickListener.OnItemClick(AdapterView? parent, View? view, int position, long id)
{
if (position >= 0 && position < items.Count())
{
var item = items.ElementAt(position);
ItemClick(item);
}
Window.Dismiss();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Window.Dispose();
Adapter.Dispose();
}
base.Dispose(disposing);
}
public void Show() => Window.Show();
}
public static ArrayAdapter<T> CreateArrayAdapter<T>(Context context, int resource = 0, IEnumerable<T>? items = null)
{
resource = resource == default ? Resource.Layout.list_combobox_item : resource;
var adapter = items == null ?
new ArrayAdapter<T>(context, resource, 0) :
new ArrayAdapter<T>(context, resource, 0, items.ToJavaList());
return adapter;
}
/// <summary>
/// 使用 MaterialAutoCompleteTextView 的实现
/// https://material.io/components/menus/android#exposed-dropdown-menus
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="textView"></param>
/// <param name="resource"></param>
/// <param name="items"></param>
/// <returns></returns>
public static ArrayAdapter<T> CreateArrayAdapter<T>(AutoCompleteTextView textView, int resource = 0, IEnumerable<T>? items = null)
{
var adapter = CreateArrayAdapter(textView.Context!, resource, items);
if (textView.Adapter != null)
{
var oldAdapter = textView.Adapter;
textView.Adapter = null;
oldAdapter.Dispose();
}
textView.Adapter = adapter;
textView.Threshold = int.MaxValue;
return adapter;
}
}
}