回答編集履歴

1

他の解決方法を追記

2019/05/27 16:26

投稿

f-miyu
f-miyu

スコア1625

test CHANGED
@@ -27,3 +27,187 @@
27
27
  }
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ ---
34
+
35
+ ちょっと本格的に、画面が表示されるタイミングでフォーカスを当てる様なエフェクトを作ってみました。
36
+
37
+
38
+
39
+ Androidプロジェクト
40
+
41
+ ```C#
42
+
43
+ [assembly: ResolutionGroupName("BlankApp1")]
44
+
45
+ [assembly: ExportEffect(typeof(FocusEffect), nameof(FocusEffect))]
46
+
47
+ namespace BlankApp1.Droid
48
+
49
+ {
50
+
51
+ public class FocusEffect : PlatformEffect
52
+
53
+ {
54
+
55
+ private Listener _listener;
56
+
57
+
58
+
59
+ protected override void OnAttached()
60
+
61
+ {
62
+
63
+ _listener = new Listener(this);
64
+
65
+ Control.ViewTreeObserver.AddOnPreDrawListener(_listener);
66
+
67
+ }
68
+
69
+
70
+
71
+ protected override void OnDetached()
72
+
73
+ {
74
+
75
+ if (_listener != null)
76
+
77
+ {
78
+
79
+ Control.ViewTreeObserver.RemoveOnPreDrawListener(_listener);
80
+
81
+ _listener = null;
82
+
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+ private void Focus()
90
+
91
+ {
92
+
93
+ (Element as VisualElement)?.Focus();
94
+
95
+
96
+
97
+ if (_listener != null)
98
+
99
+ {
100
+
101
+ Control.ViewTreeObserver.RemoveOnPreDrawListener(_listener);
102
+
103
+ _listener = null;
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ private class Listener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
112
+
113
+ {
114
+
115
+ private FocusEffect _effect;
116
+
117
+
118
+
119
+ public Listener(FocusEffect effect)
120
+
121
+ {
122
+
123
+ _effect = effect;
124
+
125
+ }
126
+
127
+
128
+
129
+ public bool OnPreDraw()
130
+
131
+ {
132
+
133
+ _effect.Focus();
134
+
135
+ return true;
136
+
137
+ }
138
+
139
+ }
140
+
141
+ }
142
+
143
+ }
144
+
145
+ ```
146
+
147
+
148
+
149
+ 共通プロジェクト
150
+
151
+ ```C#
152
+
153
+ namespace BlankApp1.Effects
154
+
155
+ {
156
+
157
+ public class FocusEffect : RoutingEffect
158
+
159
+ {
160
+
161
+ public FocusEffect() : base($"BlankApp1.{nameof(FocusEffect)}")
162
+
163
+ {
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ ```
172
+
173
+
174
+
175
+ ```xml
176
+
177
+ <?xml version="1.0" encoding="utf-8" ?>
178
+
179
+ <ContentPage
180
+
181
+ x:Class="BlankApp1.Views.LoginPage"
182
+
183
+ xmlns="http://xamarin.com/schemas/2014/forms"
184
+
185
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
186
+
187
+ xmlns:b="clr-namespace:BlankApp1.Behavior"
188
+
189
+ xmlns:e="clr-namespace:BlankApp1.Effects"
190
+
191
+ xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
192
+
193
+ prism:ViewModelLocator.AutowireViewModel="True">
194
+
195
+
196
+
197
+ <StackLayout Style="{StaticResource StackLayoutStyle}">
198
+
199
+ <Entry x:Name="User" >
200
+
201
+ <Entry.Effects>
202
+
203
+ <e:FocusEffect />
204
+
205
+ </Entry.Effects>
206
+
207
+ </Entry>
208
+
209
+ </StackLayout>
210
+
211
+ </ContentPage>
212
+
213
+ ```