所以,我想要完成的是创建一个媒体应用程序,该应用程序将在应用程序上显示并抓取文件,如果用户需要,还可以查看该项目。现在,我正在处理一类文件,即视频(电影)。我想要做的是当用户点击某个项目时,我会去查看他们点击的文件,因为我列出它的方式是通过数据绑定,并从我已定义的项目中获取路径。问题是,在按钮带您进入带有媒体播放器的页面后,我无法访问我创建的用于存储项目路径的变量,也无法访问被单击的项目。我对C#不太熟悉,我想知道如何访问与用户点击的项目相对应的MovieItem。
以下是该项目属于我的SharedMovies页面的按钮的点击事件...
public void GetFile_Click(object sender, RoutedEventArgs e)
{
Button OpenFile = (Button)sender;
if (OpenFile.DataContext is MovieItem)
{
MovieItem FileInfo = (MovieItem)OpenFile.DataContext;
string MoviePath = FileInfo.filePathExt;
string ItemPath = FileInfo.filePath;
if (MoviePath == ".mp4" || MoviePath == ".AVI")
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("VideoPlayer.xaml"));
}
else
{
MessageBoxResult result = MessageBox.Show("Sorry, this media is not supported by our player. Please select another item, or change the media so it is in a supported format.", "Unsupported Format", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
这是为电影列表中的每个文件创建的SharedMovies页面MovieItem对象的电影列表背后的逻辑:
public SharedMovies()
{
InitializeComponent();
///Declares variables and lists for files and icons
string[] filePaths = Directory.GetFiles(@"\\READYSHARE\USB_Storage\Movies", "*",
SearchOption.AllDirectories);
List<string> fileExt = new List<string>();
List<string> fileList = new List<string>();
List<BitmapSource> iconList = new List<BitmapSource>();
///Sets filename of each file without the extension/full path, and gets the icons from each file
foreach (string File in filePaths)
{
string Extension = System.IO.Path.GetExtension(File);
string Movie = System.IO.Path.GetFileNameWithoutExtension(File);
BitmapSource icon = ShellFile.FromFilePath(File).Thumbnail.BitmapSource;
iconList.Add(icon);
fileList.Add(Movie);
fileExt.Add(Extension);
}
///Setting counter for both icon and file count (Compatibility) and create new item list that extends from MovieItem function - same with MovieIcon
List<MovieItem> items = new List<MovieItem>();
int MovieIconCount = -1;
int MovieCount = 0;
//makes count and adds item to
foreach (string Movie in fileList)
{
MovieIconCount = MovieIconCount + 1;
MovieCount = MovieCount + 1;
items.Add(new MovieItem() { FileIcon = iconList[MovieIconCount], FileName = Movie, FileNumber = MovieCount, filePath = filePaths[MovieCount - 1], filePathExt = fileExt[MovieCount - 1]});
}
MovieGrid.ItemsSource = items;
}
public class MovieItem
{
public BitmapSource FileIcon { get; set; }
public string FileName { get; set; }
public int FileNumber { get; set; }
public string filePath { get; set; }
public string filePathExt { get; set; }
}
最后,这是SharedMovies的xaml页面(SharedMovies.xaml)上列表的绑定代码:
<ItemsControl Name="MovieGrid" Margin="20">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="MovieFile" Margin="15,1" Background="{x:Null}" BorderBrush="{x:Null}" Click="GetFile_Click" >
<StackPanel Name="MovieFileBlock">
<Image Name="MovieIcon" Source="{Binding FileIcon}" OpacityMask="Black" Width="30" Height="30" VerticalAlignment="Top"/>
<TextBlock Text="{Binding FileName}" Foreground="White" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
对于我试图将其传递给VideoPlayer的页面,我有一个媒体元素,它具有对媒体源的绑定,这就是我将变量设置为(这是到目前为止的代码)只是我对它的约束:
private class MediaItem
{
public string FilePath { get; set; }
}
提前谢谢你们!
换句之话,我只需要一种方法来获取用户从VideoPlayer页面中选择的项目的路径。
答案 0 :(得分:0)
一天的工作,我找到了问题的答案。为了传递文件路径以便媒体元素可以播放所选择的视频是创建可以在全局使用的Application属性。首先,在我的按钮事件(为每个文件生成的按钮)上,因为我可以从那里检索文件路径,那时我很可能需要设置属性,我创建了一个类似的应用程序属性,
MovieItem FileInfo = (MovieItem)OpenFile.DataContext;
string MoviePath = FileInfo.filePathExt;
string ItemPath = FileInfo.filePath;
if (MoviePath == ".mp4" || MoviePath == ".AVI")
{
///Created new app property here:
Application.Current.Properties["FilePath"] = ItemPath;
this.NavigationService.Navigate(new VideoPlayer());
}
而不是使用旧的导航语句,我只是使用更简单的导航技术创建了一个新的页面实例。然后我创建了一个覆盖方法(VideoPlayer页面),当页面初始化时,播放器(媒体元素的名称)的源数等于所单击项目的文件路径。当然,我必须转换为字符串,然后创建一个新的Uri来设置它等于属性。否则你会得到一个错误,说你不能从对象转换为字符串(只是因为路径,普通字符串很好):
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
MediaElement Video = (MediaElement)Player;
Video.Source = new Uri(Application.Current.Properties["FilePath"].ToString());
}
希望这有助于任何将数据传递到其他页面时遇到问题的人!