第一次发帖,所以要温柔,很抱歉发了很长的帖子,但我想提供尽可能多的细节。
我在用户控件中有一个 MediaElement 控件,其 LoadedBehaviour 属性设置为手动,见下文。当我单击“打开”按钮时,处理程序代码包括播放媒体,以便我可以在 MediaOpened 事件处理程序中准备好持续时间属性,它会成功完成。当我只需要媒体长度时,MediaOpened 处理程序会停止播放。
我还想在程序开始时加载一个播放列表并用信息填充数据网格,其中一个元素是媒体的长度。
XAML
<MediaElement Grid.Row="5" Name="MediaEL" Grid.ColumnSpan="6" MediaOpened="MediaEL_MediaOpened" LoadedBehavior="Manual" Height="169" />
<DataGrid Grid.Row="1" Grid.ColumnSpan="6" Name="dgPlayList" AutoGenerateColumns="False" Height="300" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Position}" Header="#" Width="30"/>
<DataGridTextColumn Binding="{Binding Title}" Header="Title" Width="182"/>
<DataGridTextColumn Binding="{Binding Time}" Header="Time" Width="50"/>
<DataGridCheckBoxColumn Binding="{Binding Dnp}" Header="Dnp" Width="35"/>
<DataGridTextColumn Binding="{Binding Location}" Visibility="Hidden" />
</DataGrid.Columns>
</DataGrid>
C#
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MediaEL.Source = new Uri(ofd.FileName);
btnPlay.IsEnabled = true;
MediaEL.Visibility = Visibility.Hidden;
SettingTime = true;
MediaEL.Play();
}
}
private void MediaEL_MediaOpened(object sender, RoutedEventArgs e)
{
if (MediaEL.NaturalDuration.HasTimeSpan)
{
TimeSpan ts = MediaEL.NaturalDuration.TimeSpan;
Length.Content = FormatLength(ts.TotalSeconds); // make it look like 00:00:00
seekBar.Maximum = ts.TotalSeconds;
seekBar.SmallChange = 1;
seekBar.LargeChange = Math.Min(10, ts.Seconds / 10);
}
if (!SettingTime)
timer.Start();
else
{
SettingTime = false;
MediaEL.Stop();
MediaEL.Visibility = Visibility.Visible;
MediaEL.Close();
}
}
dgPlayList.ItemsSource = LoadPlayListData();
在窗口加载方法中调用。
我已经在 foreach 语句下面的方法中注释掉了,直到我开始工作为止。
现在的问题是当试图让媒体长度填充数据网格时间列时,MediaOpened 事件不会触发,我看不出为什么不触发,并且已经用尽了对该主题的搜索。
任何想法将不胜感激!
谢谢,吉姆
C#
private ObservableCollection<PlayListEntry> LoadPlayListData()
{
var playListEntries = new ObservableCollection<PlayListEntry>();
var position = 1;
var bPlay = false;
var doc = new XmlDocument();
doc.Load(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"/mpcplaylist.xml");
var root = doc.DocumentElement;
var nodes = root.SelectNodes("/playlist/media");
XmlNode node = nodes[0];
if (node.InnerXml.Contains("title"))
{
var xmlElement = node["title"];
if (xmlElement != null)
Title = xmlElement.InnerText;
}
if (node.InnerXml.Contains("artist"))
{
var xmlElement = node["artist"];
if (xmlElement != null)
Artist = xmlElement.InnerText;
}
if (node.InnerXml.Contains("location"))
{
var xmlElement = node["location"];
if (xmlElement != null)
Location = xmlElement.InnerText;
}
if (node.InnerXml.Contains("include"))
{
var xmlElement = node["include"];
if (xmlElement != null)
Include = xmlElement.InnerText;
}
if (Include == "No")
bPlay = true;
else
bPlay = false;
//foreach (XmlNode node in nodes)
//{
// if (node.InnerXml.Contains("title"))
// {
// var xmlElement = node["title"];
// if (xmlElement != null)
// Title = xmlElement.InnerText;
// }
// if (node.InnerXml.Contains("artist"))
// {
// var xmlElement = node["artist"];
// if (xmlElement != null)
// Artist = xmlElement.InnerText;
// }
// if (node.InnerXml.Contains("location"))
// {
// var xmlElement = node["location"];
// if (xmlElement != null)
// Location = xmlElement.InnerText;
// }
// if (node.InnerXml.Contains("include"))
// {
// var xmlElement = node["include"];
// if (xmlElement != null)
// Include = xmlElement.InnerText;
// }
// if (Include == "No")
// bPlay = true;
// else
// bPlay = false;
MediaEL.Source = new Uri(Location);
SettingTime = true;
MediaEL.Play();
Medialength = Length.Content.ToString();
playListEntries.Add(new PlayListEntry()
{
Dnp = bPlay,
Position = position++,
Time = Medialength,
Title = Title,
Location = Location
});
//}
return playListEntries;
}