简体   繁体   中英

How to bind MediaElement source in C# windows store 8.1

This example for Image and Title binding though i am trying to bind for Media Element but Not working Here is the code for Image and Title binding

public class Item : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

    private string _Title = string.Empty;
    public string Title
    {
        get
        {
            return this._Title;
        }

        set
        {
            if (this._Title != value)
            {
                this._Title = value;
                this.OnPropertyChanged("Title");
            }
        }
    }



    private ImageSource _Image = null;
    public ImageSource Image
    {
        get
        {
            return this._Image;
        }

        set
        {
            if (this._Image != value)
            {
                this._Image = value;
                this.OnPropertyChanged("Image");
            }
        }
    }

    public void SetImage(Uri baseUri, String path)
    {
        Image = new BitmapImage(new Uri(baseUri, path));
    }


    private string _Category = string.Empty;
    public string Category
    {
        get
        {
            return this._Category;
        }

        set
        {
            if (this._Category != value)
            {
                this._Category = value;
                this.OnPropertyChanged("Category");
            }
        }
    }


public class GroupInfoList<T> : List<object>
{

    public object Key { get; set; }


    public new IEnumerator<object> GetEnumerator()
    {
        return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
    }
}



public class ToppingsData
{
    public ToppingsData()
    {
        Item item;
        Uri _baseUri = new Uri("ms-appx:///");

        item = new Item();
        item.Title = "Caramel Sauce";
        item.Category = "Sauces";
        item.SetImage(_baseUri, "SampleData/Images/60SauceCaramel.png");
        Collection.Add(item);

        item = new Item();
        item.Title = "Chocolate Sauce";
        item.Category = "Sauces";
        item.SetImage(_baseUri, "SampleData/Images/60SauceChocolate.png");
        Collection.Add(item);

        item = new Item();
        item.Title = "Strawberry Sauce";
        item.Category = "Sauces";
        item.SetImage(_baseUri, "SampleData/Images/60SauceStrawberry.png");
        Collection.Add(item);

        item = new Item();
        item.Title = "Chocolate Sprinkles";
        item.Category = "Sprinkles";
        item.SetImage(_baseUri, "SampleData/Images/60SprinklesChocolate.png");
        Collection.Add(item);

        item = new Item();
        item.Title = "Rainbow Sprinkles";
        item.Category = "Sprinkles";
        item.SetImage(_baseUri, "SampleData/Images/60SprinklesRainbow.png");
        Collection.Add(item);

        item = new Item();
        item.Title = "Vanilla Sprinkles";
        item.Category = "Sprinkles";
        item.SetImage(_baseUri, "SampleData/Images/60SprinklesVanilla.png");
        Collection.Add(item);

    }
    private ItemCollection _Collection = new ItemCollection();

    public ItemCollection Collection
    {
        get
        {
            return this._Collection;
        }
    }
}


// Workaround: data binding works best with an enumeration of objects that does not implement IList
public class ItemCollection : IEnumerable<Object>
{
    private System.Collections.ObjectModel.ObservableCollection<Item> itemCollection = new System.Collections.ObjectModel.ObservableCollection<Item>();

    public IEnumerator<Object> GetEnumerator()
    {
        return itemCollection.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Add(Item item)
    {
        itemCollection.Add(item);
    }
}

I am new to Windows development. Any and all help will be appreciated.

i got answer we have to Uri class below is the code

private Uri _media; public Uri Media { get { return this._media; }

        set
        {
            if (this._media != value)
            {
                this._media = value;
                this.OnPropertyChanged("Media");
            }
        }
    }

    public void SetMedia(Uri baseUri, String path)
    {
       Media = new Uri(baseUri,path);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM