Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Source-Property of MediaElement to FileInfo

I have a viewmodel class that provides a property MediaFile of type FileInfo and i want to bind the property to the Source property of a MediaElement.

The problem is that, that the Source property of the MediaElement expects an Uri, but i can´t access the FullName property (in a converter defined in the binding) of the FileInfo class, cause this will raise a SecurityException.

With images there is no problem, cause the Image control expects an ImageSource object that i can create in the converter using the stream of the FileInfo instance.

How can i define the binding, so that my MediaElement gets the right source? Or how can i pass in the MediaElement to the converter so that i can call SetSource(Stream) on the MediaElement.

The ViewModel:

public class ViewModel {
  // additional code omitted
  public FileInfo MediaFile {get; set;}
}

The Converter:

public class FileInfoToMediaConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        var file = value as System.IO.FileInfo;
        if (MediaResourceFactory.IsImage(file.Extension)) {
            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
            image.SetSource(file.OpenRead());
            return image;
        }
        else if (MediaResourceFactory.IsVideo(file.Extension)) {
           // create source for MediaElement
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
    }
}

The Bindings:

    <Image Source="{Binding MediaFile, Converter={StaticResource fileInfoToMediaConverter} }"/>
    <MediaElement Source="{Binding MediaFile, Converter={StaticResource fileInfoToMediaConverter}}/>
like image 826
Jehof Avatar asked May 19 '11 08:05

Jehof


1 Answers

Are you running out of browser with elevated permissions? Otherwise you will not have access to the local file system and you will receive a security exception. You will still be limited to the directories you have access to even with elevated permissions (My Documents, My Pictures, etc).

Assuming you are OOB with elevated permissions, you can do something like the following:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{
    var file = value as System.IO.FileInfo;
    if (MediaResourceFactory.IsImage(file.Extension)) {
        System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
        image.SetSource(file.OpenRead());
        return image;
    }
    else if (MediaResourceFactory.IsVideo(file.Extension)) {
       // create source for MediaElement
       return new Uri(file.FullName).AbsoluteUri;
    }
    return null;
}
like image 82
Matthew Keelan Avatar answered Oct 15 '22 14:10

Matthew Keelan