开发者

Binding Source-Property of MediaElement to FileInfo

开发者 https://www.devze.com 2023-03-06 19:14 出处:网络
I have a viewmodel class that provides a property MediaFile of type FileInfo and i want to bind 开发者_StackOverflow社区the property to the Source property of a MediaElement.

I have a viewmodel class that provides a property MediaFile of type FileInfo and i want to bind 开发者_StackOverflow社区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}}/>


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消