5

我已经搜索了所有内容,似乎无法找到有关此问题的答案。我的应用程序生活在松散的 XAML 世界中,因此必须依靠 XamlReaders 和树遍历来查找元素。我有一个组件可以处理这些 XAML 页面的呈现。该渲染器需要知道可以在加载的 XAML 上运行的情节提要的状态。所以我想在我的渲染器中做的是这样的: -

var resources = _currentScreenFrameworkElement.Resources;
foreach (var item in resources.Values)
{
    if (item is Storyboard)
    {
        try
        {
            var storyboard = item as Storyboard;
            **if (storyboard.GetCurrentState() == ClockState.Active)**

一切都很好。但是问题是当我尝试对 CurrentState 进行 dcheck 时,它会引发异常:-

"Cannot perform action because the specified Storyboard was not applied to this object for interactive control."

环顾四周,我看到这是因为我需要使情节提要可控。所以我的问题是如何在 XAML 中执行此操作?我没有在代码中启动故事板,因此无法将 true 传递给重载的 BeginStoryboard。

4

1 回答 1

5

I just ran into this same problem so i figured i'd share my findings.

You get that error when your storyboard is not marked as Controllable. Storyboards are marked as Controllable when the Begin method is called.

If you're doing it from code behind then you just use an overload that has this IsControllable boolean argument (list of Begin overloads).

If you've used the BeginAnimation element in Xaml then you'll need to do 2 things.

  1. assign a Name to the BeginAnimation element. The documentation for this property states: "If the Name of BeginStoryboard is unspecified, the Storyboard cannot be interactively affected after it is begun"
  2. When you're trying to interact with your storyboard in codebehind you must pass in the reference to the object that your BeginStoryboard was declared in.

Here's an example showing you step 1 (name the beginstoryboard)

<Button Name="btn1" Content="bla">
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <BeginStoryboard 
             Name="bnt1_beginStoryboard" 
             Storyboard={StaticResource someSharedStoryboard}"/>
    </EventTrigger>
  </Button.Triggers>
</Button>

and here's an example for step 2. Since you've named your beginStoryboard you can use that as a local variable in your class.. or you can just reference the actual storyboard directly. The main point is that you must pass in the owner of the beginStoryboard (which is the button in this case)

//The main point here is that we're passing in btn1
bnt1_beginStoryboard.Storyboard.Stop(btn1);
bnt1_beginStoryboard.Storyboard.SkipToFill(btn1);
bnt1_beginStoryboard.Storyboard.Resume(btn1);

Here's a list of all the "action" methods on a storyboard that require you to pass in the owning framework element: Control a Storyboard After It Starts

于 2012-11-21T19:49:24.217 回答