【问题标题】:UWP Begin Storyboard on an object created from a GridView.ItemTemplate DataTemplate using an Image Tapped EventUWP 在使用图像点击事件从 GridView.ItemTemplate DataTemplate 创建的对象上开始情节提要
【发布时间】:2017-11-15 21:25:12
【问题描述】:

我已经使用了诸如此类的 Microsoft 文档和网站并取得了一些成功,但我无法实现我的目标。

我想要一个图像点击来启动故事板,图像点击来自一个网格视图内的对象实例。

<GridView ItemSource="{Binding"}>
<GridView.ItemTemplate>
<DataTempate>
<Grid>
<Grid.Resources>
<Storyboard>
<!-- storyboard code here no worries it works outside the data template-->
</Storyboard>
<Image Tapped="Image_Tapped" Source="assets/stuff/stuff.png">
</Image>

在 DataTemplate 中有一个图像,我希望点击该图像的事件来启动与该实例相关的情节提要。 Gridview.DataContext 来自 ObservableCollection&lt;Player&gt;。 Player 是我编写的一个类,它包含一些字符串和 int 属性并使用 INotifyPropertyChanged

我试过了

<Image>
<Image.Triggers>
<EventTrigger RoutedEvent="">
<!--this only works when the UIElement is loaded-->

我以为我正在使用 EventTrigger 到达某个地方,但我需要情节提要从点击图像或按钮单击或某种形式的用户输入开始。

我还尝试将情节提要移动到各种 xaml 位置并使用

this.Resources["MyStoryboard"]as Storyboard).Begin();

任何帮助想法都会很棒,谢谢。

【问题讨论】:

    标签: c# xaml uwp storyboard


    【解决方案1】:

    成功!我知道会有一个简单的方法来做到这一点。

    好的,重新开始

    <GridView>
        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <!--stuff like columns and rows etc etc-->
                    <Image x:Name="MyImageToAnimate" source="assets/stuff/stuff.png"/>
                    <!--other bits etc-->
                    <Image x:Name="ImageThatUserTaps" source="assets/stuff/other_stuff.png">
                        <Image.Resources>
                            <Storyboard x:Name="MyStoryboard">
                                <!--write your storyboard here-->
                                    <!--my storyboard targets the opacity of "MyImageToAnimate"-->
                            </Storyboard>
                        </Image.Resources>
                    </Image>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    

    所以上面的代码是我的 xaml,我已经把情节提要放在了图像资源中。然后在代码后面...

    private void ImageThatUserTaps_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Storyboard sb = (sender as Image).Resources["MyStoryboard"] as Storyboard;
        sb.Begin();
    }
    

    这让我心烦意乱太久了,像往常一样,有一个很好的简单解决方案,只是有点看不见。

    希望这可以帮助其他人。

    【讨论】:

      【解决方案2】:

      它不应该是Tapped=__ 它应该是按钮单击的事件处理程序。使用一个按钮并用它包围图像。

      <Button onClick="buttonClicked">
      <Image/>
      </Button>
      

      【讨论】:

      • 您好,谢谢。我尝试了与您的建议类似的方法,但它失败了,因为 UWP 是有限的,并且 EventTrigger 在加载 UIElement 时发生,我需要在单击或点击事件时使用它。当我点击图像时会触发其他代码,这只是故事板问题。
      • 问题是,我不知道是否可以通过简单的方式使图像可点击。我认为这需要更多。但是,您可以使用 AppBarButton 包围图像而不是按钮并删除图像。相反,请使用外部资源图标将 ABB 图标作为您的图像。
      • 嗯,我觉得你错过了问题的重点。我有从 Image Tapped 事件中调用的代码。在 UWP xaml 中可能会有这个被点击的事件。我的问题是在点击图像时开始制作故事板。目前,图像点击事件导致玩家失去生命,我可以通过查看玩家类对象的实例来看到这种情况。我需要的是访问或调用数据模板中定义的故事板,该模板代表 xaml 中的播放器类对象
      • 好的,那就不要使用事件触发器了。相反,使用后面的代码(c#、Visual Basic、你有什么)并将图像点击事件处理程序路由到那里。
      • 但是如何开始故事板呢?仅供参考 Storyboard.Begin();不能在数据模板中使用。
      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2019-10-05
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多