如何在C#windows store 8.1中绑定MediaElement源

时间:2014-11-19 04:56:04

标签: c# xaml windows-8.1

这个示例用于图像和标题绑定,虽然我试图绑定媒体元素但不工作这里是图像和标题绑定的代码

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

我是Windows开发的新手。任何和所有帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我得到了答案,我们必须下面的Uri课程是代码

私人Uri _media;         公共Uri媒体         {             得到             {                 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);
    }