回答編集履歴

1

フォーカス移動に対応

2021/12/09 15:55

投稿

f-miyu
f-miyu

スコア1625

test CHANGED
@@ -46,6 +46,8 @@
46
46
 
47
47
  Control.Click -= Control_Click;
48
48
 
49
+ Control.FocusChange -= Control_FocusChange;
50
+
49
51
  }
50
52
 
51
53
  }
@@ -58,6 +60,8 @@
58
60
 
59
61
  Control.Click += Control_Click;
60
62
 
63
+ Control.FocusChange += Control_FocusChange;
64
+
61
65
  }
62
66
 
63
67
  }
@@ -118,9 +122,201 @@
118
122
 
119
123
  }
120
124
 
121
-
125
+ }
126
+
127
+
128
+
122
-
129
+ protected override void OnFocusChanged(bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, Android.Graphics.Rect previouslyFocusedRect)
130
+
131
+ {
132
+
133
+ base.OnFocusChanged(gainFocus, direction, previouslyFocusedRect);
134
+
135
+ }
136
+
137
+
138
+
139
+ protected override void Dispose(bool disposing)
140
+
141
+ {
142
+
143
+ if (disposing)
144
+
145
+ {
146
+
147
+ if (Control != null)
148
+
149
+ {
150
+
151
+ Control.Click -= Control_Click;
152
+
153
+ Control.FocusChange -= Control_FocusChange;
154
+
155
+ }
156
+
157
+ }
158
+
159
+
160
+
161
+ base.Dispose(disposing);
162
+
163
+ }
164
+
165
+
166
+
167
+ private void Control_Click(object sender, EventArgs e)
168
+
169
+ {
170
+
171
+ OnClick();
172
+
173
+ }
174
+
175
+
176
+
177
+ private void OnClick()
178
+
179
+ {
180
+
181
+ Picker model = Element;
182
+
183
+
184
+
185
+ if (_dialog != null)
186
+
187
+ return;
188
+
189
+
190
+
191
+ var picker = new NumberPicker(Context);
192
+
193
+ if (model.Items != null && model.Items.Any())
194
+
195
+ {
196
+
197
+ picker.MaxValue = model.Items.Count - 1;
198
+
199
+ picker.MinValue = 0;
200
+
201
+ picker.SetDisplayedValues(model.Items.ToArray());
202
+
203
+ picker.WrapSelectorWheel = false;
204
+
205
+ picker.DescendantFocusability = Android.Views.DescendantFocusability.BlockDescendants;
206
+
207
+ picker.Value = model.SelectedIndex;
208
+
209
+ }
210
+
211
+
212
+
213
+ var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
214
+
215
+ layout.AddView(picker);
216
+
217
+
218
+
219
+ ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
220
+
221
+
222
+
223
+ var builder = new AlertDialog.Builder(Context);
224
+
225
+ builder.SetView(layout);
226
+
227
+
228
+
229
+ if (!Element.IsSet(Picker.TitleColorProperty))
230
+
231
+ {
232
+
233
+ builder.SetTitle(model.Title ?? "");
234
+
235
+ }
236
+
237
+ else
238
+
239
+ {
240
+
241
+ var title = new SpannableString(model.Title ?? "");
242
+
243
+ title.SetSpan(new ForegroundColorSpan(model.TitleColor.ToAndroid()), 0, title.Length(), SpanTypes.ExclusiveExclusive);
244
+
245
+
246
+
247
+ builder.SetTitle(title);
248
+
249
+ }
250
+
251
+
252
+
253
+ builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) =>
254
+
255
+ {
256
+
257
+ ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
258
+
259
+ _dialog = null;
260
+
261
+ });
262
+
263
+ builder.SetPositiveButton(global::Android.Resource.String.Ok, (s, a) =>
264
+
265
+ {
266
+
267
+ ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
268
+
269
+ // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
270
+
271
+ // In this case, the Element & Control will no longer exist.
272
+
273
+ if (Element != null)
274
+
275
+ {
276
+
277
+ if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
278
+
279
+ Control.Text = model.Items[Element.SelectedIndex];
280
+
281
+ ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
282
+
283
+ }
284
+
285
+ _dialog = null;
286
+
287
+
288
+
289
+ // ★ここに何か実行したい処理を書く★
290
+
291
+ });
292
+
293
+
294
+
295
+ _dialog = builder.Create();
296
+
297
+ _dialog.DismissEvent += (sender, args) =>
298
+
299
+ {
300
+
301
+ ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
302
+
303
+ _dialog?.Dispose();
304
+
305
+ _dialog = null;
306
+
307
+ };
308
+
309
+ _dialog.Show();
310
+
311
+ }
312
+
313
+
314
+
315
+ private void Control_FocusChange(object sender, FocusChangeEventArgs e)
316
+
317
+ {
318
+
123
- if (e.Focus)
319
+ if (e.HasFocus)
124
320
 
125
321
  {
126
322
 
@@ -148,180 +344,6 @@
148
344
 
149
345
  }
150
346
 
151
-
152
-
153
- protected override void Dispose(bool disposing)
154
-
155
- {
156
-
157
- if (disposing)
158
-
159
- {
160
-
161
- if (Control != null)
162
-
163
- {
164
-
165
- Control.Click -= Control_Click;
166
-
167
- }
168
-
169
- }
170
-
171
-
172
-
173
- base.Dispose(disposing);
174
-
175
- }
176
-
177
-
178
-
179
- private void Control_Click(object sender, EventArgs e)
180
-
181
- {
182
-
183
- OnClick();
184
-
185
- }
186
-
187
-
188
-
189
- private void OnClick()
190
-
191
- {
192
-
193
- Picker model = Element;
194
-
195
-
196
-
197
- if (_dialog != null)
198
-
199
- return;
200
-
201
-
202
-
203
- var picker = new NumberPicker(Context);
204
-
205
- if (model.Items != null && model.Items.Any())
206
-
207
- {
208
-
209
- picker.MaxValue = model.Items.Count - 1;
210
-
211
- picker.MinValue = 0;
212
-
213
- picker.SetDisplayedValues(model.Items.ToArray());
214
-
215
- picker.WrapSelectorWheel = false;
216
-
217
- picker.DescendantFocusability = Android.Views.DescendantFocusability.BlockDescendants;
218
-
219
- picker.Value = model.SelectedIndex;
220
-
221
- }
222
-
223
-
224
-
225
- var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
226
-
227
- layout.AddView(picker);
228
-
229
-
230
-
231
- ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
232
-
233
-
234
-
235
- var builder = new AlertDialog.Builder(Context);
236
-
237
- builder.SetView(layout);
238
-
239
-
240
-
241
- if (!Element.IsSet(Picker.TitleColorProperty))
242
-
243
- {
244
-
245
- builder.SetTitle(model.Title ?? "");
246
-
247
- }
248
-
249
- else
250
-
251
- {
252
-
253
- var title = new SpannableString(model.Title ?? "");
254
-
255
- title.SetSpan(new ForegroundColorSpan(model.TitleColor.ToAndroid()), 0, title.Length(), SpanTypes.ExclusiveExclusive);
256
-
257
-
258
-
259
- builder.SetTitle(title);
260
-
261
- }
262
-
263
-
264
-
265
- builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) =>
266
-
267
- {
268
-
269
- ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
270
-
271
- _dialog = null;
272
-
273
- });
274
-
275
- builder.SetPositiveButton(global::Android.Resource.String.Ok, (s, a) =>
276
-
277
- {
278
-
279
- ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
280
-
281
- // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
282
-
283
- // In this case, the Element & Control will no longer exist.
284
-
285
- if (Element != null)
286
-
287
- {
288
-
289
- if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
290
-
291
- Control.Text = model.Items[Element.SelectedIndex];
292
-
293
- ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
294
-
295
- }
296
-
297
- _dialog = null;
298
-
299
-
300
-
301
- // ★ここに何か実行したい処理を書く★
302
-
303
- });
304
-
305
-
306
-
307
- _dialog = builder.Create();
308
-
309
- _dialog.DismissEvent += (sender, args) =>
310
-
311
- {
312
-
313
- ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
314
-
315
- _dialog?.Dispose();
316
-
317
- _dialog = null;
318
-
319
- };
320
-
321
- _dialog.Show();
322
-
323
- }
324
-
325
347
  }
326
348
 
327
349
  ```