![](/img/trans.png)
[英]Change MediaElement Source for Windows Phone Application in C#
[英]How to bind MediaElement source in C# windows store 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开发的新手。 任何和所有帮助将不胜感激。
我得到了答案,我们下面的Uri类是代码
私人Uri _media; 公共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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.