を追加するには、ここに私のコードは次のとおりです。System.InvalidOperationExceptionが - 私は多くの時間カウンタを提供しようとしているダイナミックcountdate
for(int i=0;i<10;i++)
{
long date=8000;
TextBlock tx = new TextBlock();
tx.Name="txt"+i;
CountDateLoad(date,tx);
grid.Children.Add(tx);
}
。ここで
void CountDateLoad(long dt, TextBlock tx)
{
var countdownAnimation = new StringAnimationUsingKeyFrames();
for (var i = dt; i > 0; i--)
{
var keyTime = TimeSpan.FromSeconds(dt - i);
string result = CountDate(i);
var frame = new DiscreteStringKeyFrame(result, KeyTime.FromTimeSpan(keyTime));
countdownAnimation.KeyFrames.Add(frame);
}
countdownAnimation.KeyFrames.Add(new DiscreteStringKeyFrame(" ", KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6))));
Storyboard.SetTargetName(tx.Name);
Storyboard.SetTargetProperty(countdownAnimation, new PropertyPath(TextBlock.TextProperty));
var countdownStoryboard = new Storyboard();
countdownStoryboard.Children.Add(countdownAnimation);
// countdownStoryboard.Completed += CountdownTimer_Completed;
countdownStoryboard.Begin(this);
}
private string CountDate(long p)
{
long hour = p/3600;
long minute = (p - (hour * 3600))/60;
long second = p % 60;
string result = "";
if (hour < 10) result += "0";
result += hour + " : ";
if (minute < 10) result += "0";
result += minute + " : ";
if (second < 10) result += "0";
result += second;
return result;
}
はエラーです:。
StoryBoard
tx.Nameについては
An exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll but was not handled in user code
Additional information: 'txt0' name cannot be found in the name scope of 'MainWindow'.
SomeFrameworkElementがスコープである(someFrameworkElement).RegsiterName(Name、element)を使用します。ここにサンプルがあります:https://www.codeproject.com/Articles/167364/RegisterName-for-StoryBoards-in-WPF-NameScopes – sTrenat
@sTrenatがそれに取り組んでいます。それを答えとして書いてください – Meysam