如何使用代码创建的 mediaElement 的名称并在另一个方法中使用其名称?

发布于 2024-11-04 03:47:18 字数 641 浏览 0 评论 0原文

我正在使用 Visual Studio 2010,c# WPF。我在运行时创建了一个 MediaElement 控件;

MediaElement video= new MediaElement();
video.Width = 400;
video.Height = 400;
video.Play();

video.Source = new Uri(path, UriKind.Relative);

在 XAML 视图中,我创建了 2 个按钮:“播放”和“停止”,仅当创建了 mediaElement 时,它们才可见。但我需要将 click_events 添加到这两个按钮。但是当我写 video.Play(); 它不知道这个名字。有谁知道如何让这个名字在这些单独的方法中被识别?

private void play_Click(object sender, RoutedEventArgs e)
{
   video.Play();    //syntax error under video
}

private void stop_Click(object sender, RoutedEventArgs e)
{
   video.Play();    //syntax error under video
}

I'm using Visual Studio 2010, c# WPF. I have created a MediaElement control during runtime;

MediaElement video= new MediaElement();
video.Width = 400;
video.Height = 400;
video.Play();

video.Source = new Uri(path, UriKind.Relative);

In the XAML view I created 2 buttons, Play and Stop which I will show to be visible only if a mediaElement is created. But I need to add click_events to these 2 buttons. But when I write video.Play(); it does not know this name. Does anyone know how to get this name to be recognised in these seperate methods?

private void play_Click(object sender, RoutedEventArgs e)
{
   video.Play();    //syntax error under video
}

private void stop_Click(object sender, RoutedEventArgs e)
{
   video.Play();    //syntax error under video
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-11-11 03:47:18

您需要将 video 定义为成员变量。例如,假设您在一个名为 MediaWindow 的类中...

public class MediaWindow
{
    private MediaElement video = new MediaElement { Width = 400, Height = 400};

    public void SetVideoSource(string path)
    {
        video.Source = new Uri(path, UriKind.Relative);
    }

    private void play_Click(object sender, RoutedEventArgs e)
    {
       video.Play();
    }

    private void stop_Click(object sender, RoutedEventArgs e)
    {
       video.Stop(); 
    }
}

根据您的实际类结构和设置进行调整。

You need to define video as a member variable. For example, say you're in a class called MediaWindow...

public class MediaWindow
{
    private MediaElement video = new MediaElement { Width = 400, Height = 400};

    public void SetVideoSource(string path)
    {
        video.Source = new Uri(path, UriKind.Relative);
    }

    private void play_Click(object sender, RoutedEventArgs e)
    {
       video.Play();
    }

    private void stop_Click(object sender, RoutedEventArgs e)
    {
       video.Stop(); 
    }
}

Adjust for your actual class structure and setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文