回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,164 +1,164 @@
|
|
1
|
-
> どうしてもうまくうごかなかったので再質問になります
|
2
|
-
|
3
|
-
結局何も解決していなかったってことでしょうか?
|
4
|
-
|
5
|
-
「今nananananana77さんの手元でどうなっているかが予想がつかないです。」というのは、「(実装しやすいように)できるだけそちらのコードに合わせたいが、提示コードが一部過ぎて再現できない」という意味です。
|
6
|
-
大した量でないなら、最初から全部出してください。
|
7
|
-
|
8
|
-
> コレをmainwindowにあるボタンから発火させたい
|
9
|
-
|
10
|
-
「ボタンを押すとアニメーションする」ということはわかりますが、どうやって止めるつもりでしょうか?
|
11
|
-
|
12
|
-
前回の回答で「ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?」と聞いたのは、その点が不明だったためです。
|
13
|
-
今回`Storyboard`の中身が判明しましたが、`Forever`では止まりませんね?
|
14
|
-
|
15
|
-
回答者があれこれ聞いてくるのは、要件があいまいだったりXY問題でないかと考えてのことです。
|
16
|
-
[XY問題 - Wikipedia](https://ja.wikipedia.org/wiki/XY%E5%95%8F%E9%A1%8C)
|
17
|
-
|
18
|
-
---
|
19
|
-
|
20
|
-
4パターンありますが、それぞれのボタンを同時に使うとおかしくなります(どれかひとつを採用すると考えてください)
|
21
|
-
|
22
|
-
```
|
23
|
-
<Window
|
24
|
-
x:Class="Questions374216.MainWindow"
|
25
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
26
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
27
|
-
xmlns:local="clr-namespace:Questions374216"
|
28
|
-
Width="800"
|
29
|
-
Height="450">
|
30
|
-
<DockPanel>
|
31
|
-
<UniformGrid DockPanel.Dock="Top" Rows="1">
|
32
|
-
<Button Click="Button_Click" Content="RaiseAaaEvent" />
|
33
|
-
<Button Click="Button_Click1" Content="BeginStoryboard" />
|
34
|
-
<ToggleButton Content="IsAnimating" IsChecked="{Binding IsAnimating, ElementName=userControl1}" />
|
35
|
-
<ToggleButton Content="IsEnabled" IsChecked="{Binding IsEnabled, ElementName=userControl1}" />
|
36
|
-
</UniformGrid>
|
37
|
-
<local:UserControl1 x:Name="userControl1" />
|
38
|
-
</DockPanel>
|
39
|
-
</Window>
|
40
|
-
```
|
41
|
-
```
|
42
|
-
using System.Windows;
|
43
|
-
|
44
|
-
namespace Questions374216
|
45
|
-
{
|
46
|
-
public partial class MainWindow : Window
|
47
|
-
{
|
48
|
-
public MainWindow() => InitializeComponent();
|
49
|
-
|
50
|
-
|
51
|
-
// できはするがまどろっこしいだけで何のメリットも感じない
|
52
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
53
|
-
=> userControl1.RaiseEvent(new RoutedEventArgs(UserControl1.AaaEvent));
|
54
|
-
|
55
|
-
// 直接Beginすりゃいい話
|
56
|
-
private void Button_Click1(object sender, RoutedEventArgs e)
|
57
|
-
=> userControl1.BeginStoryboard();
|
58
|
-
}
|
59
|
-
}
|
60
|
-
```
|
61
|
-
|
62
|
-
```
|
63
|
-
<UserControl
|
64
|
-
x:Class="Questions374216.UserControl1"
|
65
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
66
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
67
|
-
xmlns:local="clr-namespace:Questions374216">
|
68
|
-
<UserControl.Resources>
|
69
|
-
<Storyboard x:Key="storyboard">
|
70
|
-
<DoubleAnimationUsingKeyFrames
|
71
|
-
RepeatBehavior="Forever"
|
72
|
-
Storyboard.TargetProperty="(Canvas.Left)"
|
73
|
-
Duration="0:0:2.2">
|
74
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
|
75
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
|
76
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
|
77
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
|
78
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
|
79
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
|
80
|
-
</DoubleAnimationUsingKeyFrames>
|
81
|
-
</Storyboard>
|
82
|
-
</UserControl.Resources>
|
83
|
-
<UserControl.Triggers>
|
84
|
-
<EventTrigger RoutedEvent="local:UserControl1.Aaa">
|
85
|
-
<BeginStoryboard>
|
86
|
-
<Storyboard>
|
87
|
-
<DoubleAnimationUsingKeyFrames
|
88
|
-
RepeatBehavior="Forever"
|
89
|
-
Storyboard.TargetName="aaaa"
|
90
|
-
Storyboard.TargetProperty="(Canvas.Left)"
|
91
|
-
Duration="0:0:2.2">
|
92
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
|
93
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
|
94
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
|
95
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
|
96
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
|
97
|
-
<DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
|
98
|
-
</DoubleAnimationUsingKeyFrames>
|
99
|
-
</Storyboard>
|
100
|
-
</BeginStoryboard>
|
101
|
-
</EventTrigger>
|
102
|
-
</UserControl.Triggers>
|
103
|
-
<Canvas x:Name="LayoutRoot">
|
104
|
-
<Canvas>
|
105
|
-
<Canvas.Clip>
|
106
|
-
<RectangleGeometry Rect="0,0,256,256" />
|
107
|
-
</Canvas.Clip>
|
108
|
-
<Image Name="aaaa" Source="/aaaaaa.png" />
|
109
|
-
</Canvas>
|
110
|
-
</Canvas>
|
111
|
-
</UserControl>
|
112
|
-
```
|
113
|
-
```
|
114
|
-
using System.Windows;
|
115
|
-
using System.Windows.Controls;
|
116
|
-
using System.Windows.Media.Animation;
|
117
|
-
|
118
|
-
namespace Questions374216
|
119
|
-
{
|
120
|
-
public partial class UserControl1 : UserControl
|
121
|
-
{
|
122
|
-
public event RoutedEventHandler Aaa { add { AddHandler(AaaEvent, value); } remove { RemoveHandler(AaaEvent, value); } }
|
123
|
-
public static readonly RoutedEvent AaaEvent
|
124
|
-
= EventManager.RegisterRoutedEvent(nameof(Aaa), RoutingStrategy.Bubble,
|
125
|
-
typeof(RoutedEventHandler), typeof(UserControl1));
|
126
|
-
|
127
|
-
public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); }
|
128
|
-
public static readonly DependencyProperty IsAnimatingProperty
|
129
|
-
= DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1),
|
130
|
-
new FrameworkPropertyMetadata(false, (d, e) => ((UserControl1)d).OnIsAnimatingChanged()));
|
131
|
-
|
132
|
-
|
133
|
-
private readonly Storyboard sb;
|
134
|
-
|
135
|
-
public UserControl1()
|
136
|
-
{
|
137
|
-
InitializeComponent();
|
138
|
-
|
139
|
-
sb = (Storyboard)FindResource("storyboard");
|
140
|
-
|
141
|
-
IsEnabled = false;
|
142
|
-
IsEnabledChanged += UserControl1_IsEnabledChanged;
|
143
|
-
}
|
144
|
-
|
145
|
-
|
146
|
-
// 直接Beginすりゃいい話
|
147
|
-
public void BeginStoryboard() => sb.Begin(aaaa, true);
|
148
|
-
|
149
|
-
// 状態があるならそれこそboolでON・OFFできたほうが使いやすいと思うが
|
150
|
-
private void OnIsAnimatingChanged()
|
151
|
-
{
|
152
|
-
if (IsAnimating) sb.Begin(aaaa, true);
|
153
|
-
else sb.Stop(aaaa);
|
154
|
-
}
|
155
|
-
|
156
|
-
// DependencyPropertyを作るのが面倒なら、なにか借用してもいいか?^^;
|
157
|
-
private void UserControl1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
|
158
|
-
{
|
159
|
-
if (IsEnabled) sb.Begin(aaaa, true);
|
160
|
-
else sb.Stop(aaaa);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
}
|
1
|
+
> どうしてもうまくうごかなかったので再質問になります
|
2
|
+
|
3
|
+
結局何も解決していなかったってことでしょうか?
|
4
|
+
|
5
|
+
「今nananananana77さんの手元でどうなっているかが予想がつかないです。」というのは、「(実装しやすいように)できるだけそちらのコードに合わせたいが、提示コードが一部過ぎて再現できない」という意味です。
|
6
|
+
大した量でないなら、最初から全部出してください。
|
7
|
+
|
8
|
+
> コレをmainwindowにあるボタンから発火させたい
|
9
|
+
|
10
|
+
「ボタンを押すとアニメーションする」ということはわかりますが、どうやって止めるつもりでしょうか?
|
11
|
+
|
12
|
+
前回の回答で「ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?」と聞いたのは、その点が不明だったためです。
|
13
|
+
今回`Storyboard`の中身が判明しましたが、`Forever`では止まりませんね?
|
14
|
+
|
15
|
+
回答者があれこれ聞いてくるのは、要件があいまいだったりXY問題でないかと考えてのことです。
|
16
|
+
[XY問題 - Wikipedia](https://ja.wikipedia.org/wiki/XY%E5%95%8F%E9%A1%8C)
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
4パターンありますが、それぞれのボタンを同時に使うとおかしくなります(どれかひとつを採用すると考えてください)
|
21
|
+
|
22
|
+
```xml
|
23
|
+
<Window
|
24
|
+
x:Class="Questions374216.MainWindow"
|
25
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
26
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
27
|
+
xmlns:local="clr-namespace:Questions374216"
|
28
|
+
Width="800"
|
29
|
+
Height="450">
|
30
|
+
<DockPanel>
|
31
|
+
<UniformGrid DockPanel.Dock="Top" Rows="1">
|
32
|
+
<Button Click="Button_Click" Content="RaiseAaaEvent" />
|
33
|
+
<Button Click="Button_Click1" Content="BeginStoryboard" />
|
34
|
+
<ToggleButton Content="IsAnimating" IsChecked="{Binding IsAnimating, ElementName=userControl1}" />
|
35
|
+
<ToggleButton Content="IsEnabled" IsChecked="{Binding IsEnabled, ElementName=userControl1}" />
|
36
|
+
</UniformGrid>
|
37
|
+
<local:UserControl1 x:Name="userControl1" />
|
38
|
+
</DockPanel>
|
39
|
+
</Window>
|
40
|
+
```
|
41
|
+
```cs
|
42
|
+
using System.Windows;
|
43
|
+
|
44
|
+
namespace Questions374216
|
45
|
+
{
|
46
|
+
public partial class MainWindow : Window
|
47
|
+
{
|
48
|
+
public MainWindow() => InitializeComponent();
|
49
|
+
|
50
|
+
|
51
|
+
// できはするがまどろっこしいだけで何のメリットも感じない
|
52
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
53
|
+
=> userControl1.RaiseEvent(new RoutedEventArgs(UserControl1.AaaEvent));
|
54
|
+
|
55
|
+
// 直接Beginすりゃいい話
|
56
|
+
private void Button_Click1(object sender, RoutedEventArgs e)
|
57
|
+
=> userControl1.BeginStoryboard();
|
58
|
+
}
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
```xml
|
63
|
+
<UserControl
|
64
|
+
x:Class="Questions374216.UserControl1"
|
65
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
66
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
67
|
+
xmlns:local="clr-namespace:Questions374216">
|
68
|
+
<UserControl.Resources>
|
69
|
+
<Storyboard x:Key="storyboard">
|
70
|
+
<DoubleAnimationUsingKeyFrames
|
71
|
+
RepeatBehavior="Forever"
|
72
|
+
Storyboard.TargetProperty="(Canvas.Left)"
|
73
|
+
Duration="0:0:2.2">
|
74
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
|
75
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
|
76
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
|
77
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
|
78
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
|
79
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
|
80
|
+
</DoubleAnimationUsingKeyFrames>
|
81
|
+
</Storyboard>
|
82
|
+
</UserControl.Resources>
|
83
|
+
<UserControl.Triggers>
|
84
|
+
<EventTrigger RoutedEvent="local:UserControl1.Aaa">
|
85
|
+
<BeginStoryboard>
|
86
|
+
<Storyboard>
|
87
|
+
<DoubleAnimationUsingKeyFrames
|
88
|
+
RepeatBehavior="Forever"
|
89
|
+
Storyboard.TargetName="aaaa"
|
90
|
+
Storyboard.TargetProperty="(Canvas.Left)"
|
91
|
+
Duration="0:0:2.2">
|
92
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.0" Value="0" />
|
93
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.1" Value="-256" />
|
94
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="-512" />
|
95
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.5" Value="-768" />
|
96
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.6" Value="-1024" />
|
97
|
+
<DiscreteDoubleKeyFrame KeyTime="0:0:0.7" Value="-1280" />
|
98
|
+
</DoubleAnimationUsingKeyFrames>
|
99
|
+
</Storyboard>
|
100
|
+
</BeginStoryboard>
|
101
|
+
</EventTrigger>
|
102
|
+
</UserControl.Triggers>
|
103
|
+
<Canvas x:Name="LayoutRoot">
|
104
|
+
<Canvas>
|
105
|
+
<Canvas.Clip>
|
106
|
+
<RectangleGeometry Rect="0,0,256,256" />
|
107
|
+
</Canvas.Clip>
|
108
|
+
<Image Name="aaaa" Source="/aaaaaa.png" />
|
109
|
+
</Canvas>
|
110
|
+
</Canvas>
|
111
|
+
</UserControl>
|
112
|
+
```
|
113
|
+
```cs
|
114
|
+
using System.Windows;
|
115
|
+
using System.Windows.Controls;
|
116
|
+
using System.Windows.Media.Animation;
|
117
|
+
|
118
|
+
namespace Questions374216
|
119
|
+
{
|
120
|
+
public partial class UserControl1 : UserControl
|
121
|
+
{
|
122
|
+
public event RoutedEventHandler Aaa { add { AddHandler(AaaEvent, value); } remove { RemoveHandler(AaaEvent, value); } }
|
123
|
+
public static readonly RoutedEvent AaaEvent
|
124
|
+
= EventManager.RegisterRoutedEvent(nameof(Aaa), RoutingStrategy.Bubble,
|
125
|
+
typeof(RoutedEventHandler), typeof(UserControl1));
|
126
|
+
|
127
|
+
public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); }
|
128
|
+
public static readonly DependencyProperty IsAnimatingProperty
|
129
|
+
= DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1),
|
130
|
+
new FrameworkPropertyMetadata(false, (d, e) => ((UserControl1)d).OnIsAnimatingChanged()));
|
131
|
+
|
132
|
+
|
133
|
+
private readonly Storyboard sb;
|
134
|
+
|
135
|
+
public UserControl1()
|
136
|
+
{
|
137
|
+
InitializeComponent();
|
138
|
+
|
139
|
+
sb = (Storyboard)FindResource("storyboard");
|
140
|
+
|
141
|
+
IsEnabled = false;
|
142
|
+
IsEnabledChanged += UserControl1_IsEnabledChanged;
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
// 直接Beginすりゃいい話
|
147
|
+
public void BeginStoryboard() => sb.Begin(aaaa, true);
|
148
|
+
|
149
|
+
// 状態があるならそれこそboolでON・OFFできたほうが使いやすいと思うが
|
150
|
+
private void OnIsAnimatingChanged()
|
151
|
+
{
|
152
|
+
if (IsAnimating) sb.Begin(aaaa, true);
|
153
|
+
else sb.Stop(aaaa);
|
154
|
+
}
|
155
|
+
|
156
|
+
// DependencyPropertyを作るのが面倒なら、なにか借用してもいいか?^^;
|
157
|
+
private void UserControl1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
|
158
|
+
{
|
159
|
+
if (IsEnabled) sb.Begin(aaaa, true);
|
160
|
+
else sb.Stop(aaaa);
|
161
|
+
}
|
162
|
+
}
|
163
|
+
}
|
164
164
|
```
|