回答編集履歴

1

見直しキャンペーン中

2023/07/29 12:39

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,327 +1,164 @@
1
1
  > どうしてもうまくうごかなかったので再質問になります
2
-
3
-
4
2
 
5
3
  結局何も解決していなかったってことでしょうか?
6
4
 
7
-
8
-
9
5
  「今nananananana77さんの手元でどうなっているかが予想がつかないです。」というのは、「(実装しやすいように)できるだけそちらのコードに合わせたいが、提示コードが一部過ぎて再現できない」という意味です。
10
-
11
6
  大した量でないなら、最初から全部出してください。
12
-
13
-
14
7
 
15
8
  > コレをmainwindowにあるボタンから発火させたい
16
9
 
17
-
18
-
19
10
  「ボタンを押すとアニメーションする」ということはわかりますが、どうやって止めるつもりでしょうか?
20
11
 
21
-
22
-
23
12
  前回の回答で「ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?」と聞いたのは、その点が不明だったためです。
24
-
25
13
  今回`Storyboard`の中身が判明しましたが、`Forever`では止まりませんね?
26
14
 
27
-
28
-
29
15
  回答者があれこれ聞いてくるのは、要件があいまいだったりXY問題でないかと考えてのことです。
30
-
31
16
  [XY問題 - Wikipedia](https://ja.wikipedia.org/wiki/XY%E5%95%8F%E9%A1%8C)
32
-
33
-
34
17
 
35
18
  ---
36
19
 
37
-
38
-
39
20
  4パターンありますが、それぞれのボタンを同時に使うとおかしくなります(どれかひとつを採用すると考えてください)
40
21
 
41
-
42
-
43
- ```xaml
22
+ ```xml
44
-
45
23
  <Window
46
-
47
24
  x:Class="Questions374216.MainWindow"
48
-
49
25
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
50
-
51
26
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
52
-
53
27
  xmlns:local="clr-namespace:Questions374216"
54
-
55
28
  Width="800"
56
-
57
29
  Height="450">
58
-
59
30
  <DockPanel>
60
-
61
31
  <UniformGrid DockPanel.Dock="Top" Rows="1">
62
-
63
32
  <Button Click="Button_Click" Content="RaiseAaaEvent" />
64
-
65
33
  <Button Click="Button_Click1" Content="BeginStoryboard" />
66
-
67
34
  <ToggleButton Content="IsAnimating" IsChecked="{Binding IsAnimating, ElementName=userControl1}" />
68
-
69
35
  <ToggleButton Content="IsEnabled" IsChecked="{Binding IsEnabled, ElementName=userControl1}" />
70
-
71
36
  </UniformGrid>
72
-
73
37
  <local:UserControl1 x:Name="userControl1" />
74
-
75
38
  </DockPanel>
76
-
77
39
  </Window>
78
-
79
40
  ```
80
-
81
- ```C#
41
+ ```cs
82
-
83
42
  using System.Windows;
84
43
 
85
-
86
-
87
44
  namespace Questions374216
88
-
89
45
  {
90
-
91
46
  public partial class MainWindow : Window
92
-
93
47
  {
94
-
95
48
  public MainWindow() => InitializeComponent();
96
49
 
97
50
 
98
-
99
-
100
-
101
51
  // できはするがまどろっこしいだけで何のメリットも感じない
102
-
103
52
  private void Button_Click(object sender, RoutedEventArgs e)
104
-
105
53
  => userControl1.RaiseEvent(new RoutedEventArgs(UserControl1.AaaEvent));
106
54
 
107
-
108
-
109
55
  // 直接Beginすりゃいい話
110
-
111
56
  private void Button_Click1(object sender, RoutedEventArgs e)
112
-
113
57
  => userControl1.BeginStoryboard();
114
-
115
58
  }
116
-
117
59
  }
118
-
119
60
  ```
120
61
 
121
-
122
-
123
- ```xaml
62
+ ```xml
124
-
125
63
  <UserControl
126
-
127
64
  x:Class="Questions374216.UserControl1"
128
-
129
65
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
130
-
131
66
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
132
-
133
67
  xmlns:local="clr-namespace:Questions374216">
134
-
135
68
  <UserControl.Resources>
136
-
137
69
  <Storyboard x:Key="storyboard">
138
-
139
70
  <DoubleAnimationUsingKeyFrames
140
-
141
71
  RepeatBehavior="Forever"
142
-
143
72
  Storyboard.TargetProperty="(Canvas.Left)"
144
-
145
73
  Duration="0:0:2.2">
146
-
147
74
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
148
-
149
75
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
150
-
151
76
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
152
-
153
77
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
154
-
155
78
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
156
-
157
79
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
158
-
159
80
  </DoubleAnimationUsingKeyFrames>
160
-
161
81
  </Storyboard>
162
-
163
82
  </UserControl.Resources>
164
-
165
83
  <UserControl.Triggers>
166
-
167
84
  <EventTrigger RoutedEvent="local:UserControl1.Aaa">
168
-
169
85
  <BeginStoryboard>
170
-
171
86
  <Storyboard>
172
-
173
87
  <DoubleAnimationUsingKeyFrames
174
-
175
88
  RepeatBehavior="Forever"
176
-
177
89
  Storyboard.TargetName="aaaa"
178
-
179
90
  Storyboard.TargetProperty="(Canvas.Left)"
180
-
181
91
  Duration="0:0:2.2">
182
-
183
92
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
184
-
185
93
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
186
-
187
94
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
188
-
189
95
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
190
-
191
96
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
192
-
193
97
  <DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
194
-
195
98
  </DoubleAnimationUsingKeyFrames>
196
-
197
99
  </Storyboard>
198
-
199
100
  </BeginStoryboard>
200
-
201
101
  </EventTrigger>
202
-
203
102
  </UserControl.Triggers>
204
-
205
103
  <Canvas x:Name="LayoutRoot">
206
-
207
104
  <Canvas>
208
-
209
105
  <Canvas.Clip>
210
-
211
106
  <RectangleGeometry Rect="0,0,256,256" />
212
-
213
107
  </Canvas.Clip>
214
-
215
108
  <Image Name="aaaa" Source="/aaaaaa.png" />
216
-
217
109
  </Canvas>
218
-
219
110
  </Canvas>
220
-
221
111
  </UserControl>
222
-
223
112
  ```
224
-
225
- ```C#
113
+ ```cs
226
-
227
114
  using System.Windows;
228
-
229
115
  using System.Windows.Controls;
230
-
231
116
  using System.Windows.Media.Animation;
232
117
 
233
-
234
-
235
118
  namespace Questions374216
236
-
237
119
  {
238
-
239
120
  public partial class UserControl1 : UserControl
240
-
241
121
  {
242
-
243
122
  public event RoutedEventHandler Aaa { add { AddHandler(AaaEvent, value); } remove { RemoveHandler(AaaEvent, value); } }
244
-
245
123
  public static readonly RoutedEvent AaaEvent
246
-
247
124
  = EventManager.RegisterRoutedEvent(nameof(Aaa), RoutingStrategy.Bubble,
248
-
249
125
  typeof(RoutedEventHandler), typeof(UserControl1));
250
126
 
251
-
252
-
253
127
  public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); }
254
-
255
128
  public static readonly DependencyProperty IsAnimatingProperty
256
-
257
129
  = DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1),
258
-
259
130
  new FrameworkPropertyMetadata(false, (d, e) => ((UserControl1)d).OnIsAnimatingChanged()));
260
-
261
-
262
-
263
131
 
264
132
 
265
133
  private readonly Storyboard sb;
266
134
 
267
-
268
-
269
135
  public UserControl1()
270
-
271
136
  {
272
-
273
137
  InitializeComponent();
274
-
275
-
276
138
 
277
139
  sb = (Storyboard)FindResource("storyboard");
278
140
 
279
-
280
-
281
141
  IsEnabled = false;
282
-
283
142
  IsEnabledChanged += UserControl1_IsEnabledChanged;
284
-
285
143
  }
286
144
 
287
145
 
288
-
289
-
290
-
291
146
  // 直接Beginすりゃいい話
292
-
293
147
  public void BeginStoryboard() => sb.Begin(aaaa, true);
294
148
 
295
-
296
-
297
149
  // 状態があるならそれこそboolでON・OFFできたほうが使いやすいと思うが
298
-
299
150
  private void OnIsAnimatingChanged()
300
-
301
151
  {
302
-
303
152
  if (IsAnimating) sb.Begin(aaaa, true);
304
-
305
153
  else sb.Stop(aaaa);
306
-
307
154
  }
308
155
 
309
-
310
-
311
156
  // DependencyPropertyを作るのが面倒なら、なにか借用してもいいか?^^;
312
-
313
157
  private void UserControl1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
314
-
315
158
  {
316
-
317
159
  if (IsEnabled) sb.Begin(aaaa, true);
318
-
319
160
  else sb.Stop(aaaa);
320
-
321
161
  }
322
-
323
162
  }
324
-
325
163
  }
326
-
327
164
  ```