嗨. 我正在尝试通过按按钮上的代码来创建媒体元素. 香港专业教育学院创建了一个很好的xaml媒体元素,但是当我尝试从c#代码创建一个媒体元素时,它将无法正常工作. 这是我的Xaml. mediaelement name ="meXAML" loadingbehavior =手册" 还有背后的代码
Hi. I''m trying to create a media element from code on a button press. Ive created a xaml media element which works fine but when i try to create a media element from c# code it will not work. Here is my Xaml. mediaelement name="meXAML" loadedbehavior="Manual" And the code behind
public partial class MainWindow : Window { MediaElement meC; public MainWindow() { InitializeComponent(); } private void btnCode_Click(object sender, RoutedEventArgs e) { meC = new MediaElement(); meC.LoadedBehavior = MediaState.Manual; meC.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Relative); meC.Play(); } private void btnXaml_Click(object sender, RoutedEventArgs e) { meXAML.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Relative); meXAML.Play(); } }据我所知,两个媒体元素仅使用不同的方法设置为相同. XAML媒体元素可以正常播放,但是生成的代码却不能. 我们将不胜感激.
As far as I can see both media elements are set the same only using different methods. The XAML media element plays fine but the code generated one will not. Any help will be greatly appreciated.
推荐答案您的手动媒体元素不属于您的页面.您是否正在寻找弹出式媒体元素?另外,您的Uri不是相对的,这是绝对的.相对表示您提供的路径没有绝对根,而是从应用程序所在的文件夹导航. Your manual media element is not part of your page. Are you looking for a popup media element ? Also, your Uri is not relative, it''s absolute. Relative would mean that the path you give does not have an absolute root, but navigates from the folder your app is in.
非常感谢. 我修改了代码以将中介元素添加到页面的堆栈面板中. Thank you very much. i modified the code to add the mediaelement to a stack panel in my page. private void btnCode_Click(object sender, RoutedEventArgs e) { meC = new MediaElement(); meC.LoadedBehavior = MediaState.Manual; mystackpanel.Children.Add(meC); meC.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Absolute); meC.Play(); }
我对WPF还是很陌生,我被卡在其中了一段时间. 再次感谢.
I''m still new to WPF and I was stuck on this one for a bit. Thanks again.
更多推荐
媒体元素不会播放
发布评论