public SoundSourceSelector() { InitializeComponent(); AllowDrop = true; DragEnter += (s, e) => { e.Effect = DragDropEffects.None; if (!e.Data.GetDataPresent(DataFormats.FileDrop)) { return; } var items = (string[])e.Data.GetData(DataFormats.FileDrop); if (items.Length != 1) { return; } string path = items.Single(); if (SupportedExtensions.Any(p => Path.GetExtension(path) == p) && File.Exists(path)) { e.Effect = DragDropEffects.Copy; } }; DragDrop += (s, e) => { filePathBox.Text = ((string[])e.Data.GetData(DataFormats.FileDrop)).Single(); }; }
private static async void SourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { if (e.OldValue is string oldFilePath && PreviewedFilePaths.Contains(oldFilePath)) { PreviewedFilePaths.Remove(oldFilePath); } if (obj is Image image) { if (e.NewValue is string filePath && !String.IsNullOrEmpty(filePath)) { FileInfo file = new FileInfo(filePath); if (file.Exists && SupportedExtensions.Any(x => x.OrdinalEquals(file.Extension))) { try { image.Tag = filePath; PreviewedFilePaths.Add(filePath); int hashCode = image.GetHashCode(); if (!ImageReferences.ContainsKey(hashCode)) { ImageReferences.Add(hashCode, new WeakReference <Image>(image)); image.Unloaded += (x, y) => { PreviewedFilePaths.Remove(image.Tag?.ToString()); }; } if (file.Length > 1048576) { image.Visibility = Visibility.Collapsed; } image.Tag = file.FullName; image.Source = await ReadImageAsync(file.FullName); } finally { image.Visibility = Visibility.Visible; } } } else { image.Tag = null; image.Source = null; } }
protected override void OnDragEnter(DragEventArgs e) { base.OnDragEnter(e); e.Effect = DragDropEffects.None; if (!e.Data.GetDataPresent(DataFormats.FileDrop)) { return; } var items = (string[])e.Data.GetData(DataFormats.FileDrop); if (items.Length != 1) { return; } string path = items[0]; if (SupportedExtensions.Any(p => Path.GetExtension(path) == p)) { e.Effect = DragDropEffects.Copy; } }
/// <summary> /// Returns a value indicating whether or not there is an importer for the given extension /// </summary> public static bool IsValidMovieExtension(string extension) { return(SupportedExtensions.Any(e => string.Equals(extension, e, StringComparison.OrdinalIgnoreCase))); }