Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Contents tagged with Windows 8

  • Binding MediaElement to a ViewModel in a Windows 8 Store App

    If you want to play a video from your video-library in a MediaElement control of a Metro Windows Store App and tried to bind the Url of the video file as a source to the MediaElement control like this, you may have noticed it’s not working as well for you:

    <MediaElement Source="{Binding Url}" />

    I have no idea why it’s not working, but I managed to get it going using  ContentControl instead:

    <ContentControl Content="{Binding Video}" />

    The code behind for this is:

    protected override void OnNavigatedTo(NavigationEventArgs e)

    {

        _viewModel = new VideoViewModel("video.mp4");

        DataContext = _viewModel;

    }

    And the VideoViewModel looks like this:

    public class VideoViewModel

    {

        private readonly MediaElement _video;

        private readonly string _filename;

     

        public VideoViewModel(string filename)

        {

            _filename = filename;

            _video = new MediaElement { AutoPlay = true };

            //don't load the stream until the control is ready

            _video.Loaded += VideoLoaded;

        }

     

        public MediaElement Video

        {

            get { return _video; }

        }

     

        private async void VideoLoaded(object sender, RoutedEventArgs e)

        {

            var file = await KnownFolders.VideosLibrary.GetFileAsync(_filename);

            var stream = await file.OpenAsync(FileAccessMode.Read);

            _video.SetSource(stream, file.FileType);

        }

    }

    I had to wait for the MediaElement.Loaded event until I could load and set the video stream.

  • File Activation in Windows RT

    The code sample for file activation on MSDN is lacking some code Winking smile so a simple way to pass the file clicked to your MainPage could be:

    protected override void OnFileActivated(FileActivatedEventArgs args)

    {

        var page = new Frame();

        page.Navigate(typeof(MainPage));

        Window.Current.Content = page;

     

        var p = page.Content as MainPage;

        if (p != null) p.FileEvent = args;

        Window.Current.Activate();

    }

    And in MainPage:

    public MainPage()

    {

        InitializeComponent();

        Loaded += MainPageLoaded;

    }

    void MainPageLoaded(object sender, RoutedEventArgs e)

    {

        if (FileEvent != null && FileEvent.Files.Count > 0)

        {

            //… do something with file

        }

    }

  • Read All Text from Textfile with Encoding in Windows RT

    A simple extension for reading all text from a text file in WinRT with a specific encoding, made as an extension to StorageFile:

    public static class StorageFileExtensions
    {
        async public static Task<string> ReadAllTextAsync(this StorageFile storageFile)
        {
            var buffer = await FileIO.ReadBufferAsync(storageFile);
            var fileData = buffer.ToArray();
            var encoding = Encoding.GetEncoding("Windows-1252");
            var text = encoding.GetString(fileData, 0, fileData.Length);
            return text;
        }
    }