C#WPF-在按钮单击事件中从另一个页面访问变量

C#WPF-在按钮单击事件中从另一个页面访问变量,c#,wpf,xaml,C#,Wpf,Xaml,所以,我要做的是创建一个媒体应用程序,它可以出去抓取文件显示在应用程序上,如果用户需要,还可以查看项目。现在,我正在处理一类文件,那就是视频(电影)。我想做的是,当用户单击一个项目时,我会去看看他们单击了哪个文件,因为我是通过数据绑定列出它的,并从我定义的项目中获取路径。问题是,在按钮将您带到带有媒体播放器的页面后,我无法访问为存储项目路径而创建的变量,也无法访问单击的项目。我对C#不是很有经验,我想知道如何访问与用户单击的项目对应的MovieItem 这是我的SharedMovies页面上该项

所以,我要做的是创建一个媒体应用程序,它可以出去抓取文件显示在应用程序上,如果用户需要,还可以查看项目。现在,我正在处理一类文件,那就是视频(电影)。我想做的是,当用户单击一个项目时,我会去看看他们单击了哪个文件,因为我是通过数据绑定列出它的,并从我定义的项目中获取路径。问题是,在按钮将您带到带有媒体播放器的页面后,我无法访问为存储项目路径而创建的变量,也无法访问单击的项目。我对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; }
    } 
提前谢谢你们


用不太冗长的话来说,我只需要一种方法来获取用户从视频播放器页面选择的项目的路径。

一天的计算,我找到了问题的答案。为了传递文件路径,以便媒体元素可以播放选定的视频,需要创建一个可全局使用的应用程序属性。首先,在我的button事件(为每个文件生成的按钮)上,因为我可以从那里检索文件路径,而这正是我最有可能需要设置属性的时候,我创建了一个应用程序属性,如下所示

            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将其设置为属性。否则,您将得到一个错误,说明无法从对象转换为字符串(仅因为路径,普通字符串是可以的):

希望这能帮助那些无法将数据传递到其他页面的人

private class MediaItem
    {
        public string FilePath { get; set; }
    }
            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());                 
            }
protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        MediaElement Video = (MediaElement)Player;
        Video.Source = new Uri(Application.Current.Properties["FilePath"].ToString());
    }