1

我在查找带有名称的生成故事板时遇到问题。我不能使用类变量,因为这段代码被多次调用并且返回是异步的。

您可以查看我的其他问题以获取更多信息:这里

我的代码:

private void refreshPostIt(int index)
    {
        Button btn = // Button to rotate
        Storyboard sb = new Storyboard();
        sb.Name = "sb" + postItIndex;

        Storyboard.SetTarget(rotate, btn);
        Storyboard.SetTargetName(rotate, btn.Name);
        Storyboard.SetTargetProperty(rotate, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        sb.Children.Add(rotate);
        sb.Begin(btn, true);

        // Asynchronous call
        BackgroundWorker bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        bgw.RunWorkerAsync(postItIndex);
    }
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        int postItIndex = // Retrieved from e.Arguments
        Button btn = // Find the Button thanks to the postItIndex

        Storyboard sb = // Here is my problem, how to find the Storyboard
        if (sb != null)
        {
            // Stop rotation
            //sb.Stop(btn);
        }
    }
4

2 回答 2

1

总是可以创建一个继承自 BackgroundWorker 的类,它不是密封的。向该类添加一个属性,该属性包含您要传递给已完成事件的任何信息。

    class StoryboardWorkerArgs
    {
        public Storyboard StoryBoard { get; set; }
        public object Other { get; set; }
    }

    class BackgroundWorker2 : BackgroundWorker
    {
        public StoryboardWorkerArgs Args { get; set; }
    }

然后,您可以在 args 属性中设置故事板。

BackgroundWorker2 worker = new BackgroundWorker2();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.Args = new StoryboardWorkerArgs ()
{
  StoryBoard = null, /* reference to your storyboard*/
  Other = null, /* postItIndex */
 };
 worker.RunWorkerAsync();

然后在您完成的活动中。

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    BackgroundWorker2 sender2 = sender as BackgroundWorker2;
    if (sender2 != null)
    {
        var story = sender2.Args.StoryBoard; // <- your storyboard.
    }
}
于 2012-06-01T09:39:20.317 回答
1

如果您使用 MVVM 显示“postIt”-s 启动和停止情节提要将完全在 XAML 中。

在代表 Post 的 UIItem dataTemplate 中有以下伪样式:

<Style>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsRefreshing}"
                     Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard x:Name="rotating">
                    <Storyboard>
                        <DoubleAnimation... />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <StopStoryboard BeginStoryboardName="rotating" />
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

您的视图模型应该类似于(伪代码):

Class MyApp : INotifyPropertyChanged
  ObservableCollection<Post> Posts {get;set;}
  void RefreshPost(int id);

Class Post : INotifyPropertyChanged
  bool IsRefreshing {get;set;}

在窗口中有一个带有 ItemTemplate 的 listBox 用于您的 Post,并为 ItemsSource 提供 Posts 集合。

并将窗口的数据上下文设置为 MyApp 的一个实例。因此,每当您更改某些帖子的 IsRrefreshing 时,UI 都会完成其余的工作。

于 2012-06-01T10:21:29.813 回答