【问题标题】:Getting Error "No Element 'X' found in stack panel" when using EventTrigger from XAML使用 XAML 中的 EventTrigger 时出现错误“堆栈面板中未找到元素 'X'”
【发布时间】:2015-10-09 04:34:19
【问题描述】:

我测试了an example I copied from MSDN,只是稍作改动以满足我的需要,
但我得到一个看起来非常基本的错误 - 但我似乎无法解决它。

Resharper 说:“在 StackPanel 中找不到元素播放按钮”,
对于所有其他路由事件,同样的错误。
即使我只将根元素从页面更改为窗口,它对原始代码也说了同样的话。

代码符合并运行 - 但MediaOpened 的处理程序将无法到达。
所以我认为 resharper 实际上确实发现了问题。

要重现,只需创建一个新的 WPF 应用程序并复制此代码,请确保媒体元素 url 路径适用于您机器中的电影。

那么我在这里做错了什么?

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication8"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Background="Black">
        <MediaElement Name="myMediaElement" MediaOpened="Element_MediaOpened" Width="260" Height="150" Stretch="Fill" />
        <!-- Button controls for play, pause, resume, and stop. -->
        <StackPanel HorizontalAlignment="Center" Width="260" Orientation="Horizontal">
            <Button x:Name="PlayButton" Margin="2" Width="50" Content="Play" />
            <Button x:Name="PauseButton" Margin="2" Width="50" Content="Pause" />
            <Button x:Name="ResumeButton" Margin="2" Width="50" Content="Resume" />
            <Button x:Name="StopButton" Margin="2" Width="50" Content="Stop" />
        </StackPanel>

        <!-- Ths slider shows the progress of the media. -->
        <Slider Name="timelineSlider" Margin="5" Width="250" HorizontalAlignment="Center"/>

        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="Button.MouseDown" SourceName="PlayButton">
                <EventTrigger.Actions>
                    <BeginStoryboard Name= "myBegin">

                        <Storyboard SlipBehavior="Slip">
                            <!-- The MediaTimeline controls the timing of the video and acts like other Timeline objects.  
                                 For example, although the video clip (numbers.wmv) lasts longer, playback ends after six  
                                 seconds because that is the duration of the MediaTimeline (Duration="0:0:6"). -->
                            <!--<MediaTimeline Source="C:\AgasCinemaTest\DataStore\0\10"
                                           Storyboard.TargetName="myMediaElement"  
                                           BeginTime="0:0:0" Duration="0:0:6" CurrentTimeInvalidated="MediaTimeChanged" />-->
                            <MediaTimeline Source="D:\DreamScene\Videos\Changer.mpg"
                                           Storyboard.TargetName="myMediaElement"  
                                           BeginTime="0:0:0" Duration="0:0:16" CurrentTimeInvalidated="MediaTimeChanged" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>

            <!-- These triggers impliment the functionality of the Pause, Resume and Stop buttons.-->
            <EventTrigger RoutedEvent="Image.MouseDown" SourceName="PauseButton">
                <EventTrigger.Actions>
                    <PauseStoryboard BeginStoryboardName="myBegin" />
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="Image.MouseDown" SourceName="ResumeButton">
                <EventTrigger.Actions>
                    <ResumeStoryboard BeginStoryboardName="myBegin" />
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="Image.MouseDown" SourceName="StopButton">
                <EventTrigger.Actions>
                    <StopStoryboard BeginStoryboardName="myBegin" />
                </EventTrigger.Actions>
            </EventTrigger>
        </StackPanel.Triggers>

    </StackPanel>

</Window>

这是背后的代码:

namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // When the media opens, initialize the "Seek To" slider maximum value
        // to the total number of miliseconds in the length of the media clip.
        private void Element_MediaOpened(object sender, EventArgs e)
        {
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        private void MediaTimeChanged(object sender, EventArgs e)
        {
            timelineSlider.Value = myMediaElement.Position.TotalMilliseconds;
        }

    }
}

【问题讨论】:

  • 我无法重现您的问题。你的代码看起来很好。我尝试了具有相同结构的类似 XAML 代码,它刚刚编译并运行正常。那么你的意思是你不能编译和运行你的代码吗?我不确定 Resharper 是如何工作的,但看起来它是错误的,代码很好(顺便说一句 - 我不使用 Resharper - 坦率地说,它在许多方面可能都很棒,但它使编辑器完全丑陋 比 VS 默认支持的,我认为它是一团糟)。
  • @Hopeless 嗯.. 这是一个令人惊讶的消息——当你说“作品”时——你的意思是你能用它看电影? - 你提到了类似的代码 - 但是这个特定的代码呢?
  • 你的问题是错误No Element PlayButton Found In StackPanel,所以我觉得很奇怪,并尝试了一个没有任何媒体的简单代码。这里不涉及媒体元素。
  • 如果元素 PauseButton 是按钮而不是图像,为什么要使用
  • @Hopeless 谢谢 :) 现在可以使用了 - 如果您可以将最后一条评论作为答案发布,我将很乐意将其标记为已接受的答案。

标签: c# wpf xaml resharper visual-studio-2015


【解决方案1】:

不要使用Name,而是使用x:Name

x:Name 是一个 xaml 概念,主要用于引用元素。当您为元素提供x:Name xaml 属性时,“指定的x:Name 将成为在处理 xaml 时在基础代码中创建的字段的名称,并且该字段包含对该对象的引用。” (MSDN) 所以,它是一个设计器生成的字段,默认情况下具有内部访问权限。

NameFrameworkElement 的现有字符串属性,以 xaml 属性的形式列为任何其他 wpf 元素属性。

同时将Button.MouseDown 更改为Button.PreviewMouseDown 即可。

【讨论】:

  • 如果你想知道的话,我没有 -1 你的答案。但我也不能将它标记为有帮助,因为我的问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 2015-02-14
相关资源
最近更新 更多