C# 属性的索引器能否在WindowsPhone8的xaml绑定中工作?
我做错了什么?有人给我一些建议吗 据 可以在应用索引器的属性名称后的方括号内指定属性的索引器。例如,子句Path=ShoppingCart[0]将绑定设置为与属性的内部索引如何处理文本字符串“0”相对应的索引。还支持多个索引器 我在xaml中放置了一个属性的索引器C# 属性的索引器能否在WindowsPhone8的xaml绑定中工作?,c#,xaml,windows-phone-7,windows-phone-8,binding,C#,Xaml,Windows Phone 7,Windows Phone 8,Binding,我做错了什么?有人给我一些建议吗 据 可以在应用索引器的属性名称后的方括号内指定属性的索引器。例如,子句Path=ShoppingCart[0]将绑定设置为与属性的内部索引如何处理文本字符串“0”相对应的索引。还支持多个索引器 我在xaml中放置了一个属性的索引器 <Image Source="{Binding ImagePathList[0]}" Height="50" Width="50" Grid.Row="0" Grid.Column="0" VerticalAlignment="
<Image Source="{Binding ImagePathList[0]}" Height="50" Width="50" Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" Margin="0,7,7,0" Grid.RowSpan="2">
我没有给出viewmodel代码,因为我非常确定ListImagePathList包含数据
编辑*
更多详细信息:ImagePathList[0]是一个web图像url
为Brendan编辑
模型是文章
public class Article : INotifyPropertyChanged
{
private long _Id;
public long ID
{
get { return _Id; }
set
{
if (_Id != value)
{
_Id = value;
NotifyPropertyChanged();
}
}
}
private string _subject;
public string Subject
{
get
{
return _subject;
}
set
{
if (_subject != value)
{
_subject = value;
NotifyPropertyChanged();
}
}
}
private string _words;
public string Words
{
get
{
return _words;
}
set
{
if (_words != value)
{
_words = value;
NotifyPropertyChanged();
}
}
}
private DateTime _publishDate;
public DateTime PublishDate
{
get
{ return _publishDate; }
set
{
if (_publishDate != value)
{
_publishDate = value;
NotifyPropertyChanged();
}
}
}
public List<string> ImagePathList = new List<string>();
private string _firstImage;
public string FirstImage
{
get
{
return _firstImage;
}
set
{
if (_firstImage != value)
{
_firstImage = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共类文章:INotifyPropertyChanged
{
私人长Id;
公共长ID
{
获取{return\u Id;}
设置
{
如果(_Id!=值)
{
_Id=值;
NotifyPropertyChanged();
}
}
}
私有字符串(subject);;
公共字符串主题
{
得到
{
返回主题;
}
设置
{
如果(_subject!=值)
{
_主题=价值;
NotifyPropertyChanged();
}
}
}
私有字符串(字),;
公共字符串
{
得到
{
返回单词;
}
设置
{
如果(_words!=值)
{
_文字=价值;
NotifyPropertyChanged();
}
}
}
私有日期时间publishDate;
公共日期时间发布日期
{
得到
{返回_publishDate;}
设置
{
如果(_publishDate!=值)
{
_publishDate=值;
NotifyPropertyChanged();
}
}
}
public List ImagePathList=新列表();
私有字符串_firstImage;
公共字符串图像
{
得到
{
返回_firstImage;
}
设置
{
if(_firstImage!=值)
{
_firstImage=值;
NotifyPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
ArticleViewModel如下所示从网络返回的所有数据均正确强>
public class ArticleListViewModel : INotifyPropertyChanged
{
public ArticleListViewModel()
{
this.ArticleCollection = new ObservableCollection<Article>();
}
public ObservableCollection<Article> ArticleCollection
{
get;
private set;
}
public void LoadPage(int pageNumber)
{
if (pageNumber == 1)
{
this.ArticleCollection.Clear();
}
IsLoading = true;
ReadArticleList(pageNumber);
}
private async void ReadArticleList(int pageNumber)
{
try
{
List<Article> articleList = new List<Article>();
articleList = await CollectionHttpClient.GetArticlesByPageAsync(pageNumber);
this.ArticleCollection.Add(item);
}
}
catch(Exception ex)
{
if (ex.HResult == -2146233088 && ex.Message.Equals("Response status code does not indicate success: 404 ()."))
{
MessageBox.Show("The network is not set right. Internet cannot be accessed.");
}
else
{
MessageBox.Show("sorry, no data.");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共类ArticleListViewModel:INotifyPropertyChanged
{
public ArticleListViewModel()
{
this.ArticleCollection=新的ObservableCollection();
}
公共可观测集合文章集合
{
得到;
私人设置;
}
公共无效加载页(内部页码)
{
如果(页码==1)
{
此.ArticleCollection.Clear();
}
IsLoading=true;
阅读文章列表(页码);
}
私有异步无效ReadArticleList(int页码)
{
尝试
{
List articleList=新列表();
articleList=await CollectionHttpClient.GetArticlesByPageAsync(页码);
此.ArticleCollection.Add(项目);
}
}
捕获(例外情况除外)
{
if(ex.HResult==-2146233088&&ex.Message.Equals(“响应状态代码不表示成功:404()))
{
MessageBox.Show(“网络设置不正确,无法访问Internet”);
}
其他的
{
Show(“对不起,没有数据”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
您显示的XAML代码很好
DataContext
可能有问题。可能尚未设置页面DataContext
?或者,DataContext
可能已更改,例如在ItemTemplate
否则,问题可能与绑定属性有关。试试下面的方法
private ObservableCollection<string> _imagePathList = new ObservableCollection<string>();
public ObservableCollection<string> ImagePathList {
get { return this._imagePathList; }
set {
if (this._imagePathList != value)
{
this._imagePathList = value;
// I'm going to assume you have the NotifyPropertyChanged
// method defined on the view-model
this.NotifyPropertyChanged();
}
}
}
您还需要向包含类添加INotifyPropertyChanged
接口,例如
public class MyViewModelClass : INoftifyPropertyChanged
{
...
}
我认为问题在于你的图片来自哪里。正如上面所说: 您可以通过指定绝对URL(例如)或指定相对于应用程序的XAP文件的URL来设置源 您可以在XAML中设置该属性,但在本例中,您将该属性设置为URI。XAML行为依赖于将字符串作为URI处理并调用BitmapImage(URI)构造函数的基础类型转换。这反过来可能会从该URI请求一个流,并返回图像源对象 仅用于测试-在项目中放置一些图像,然后将其设置为ImagePathList。看看这是否有效(我想应该是这样)。或者查看是否可以获取BitmapImage(图像)
public class MyViewModelClass : INoftifyPropertyChanged
{
...
}
// a property in your class
public Uri FirstImage
{
get
{
return new Uri(ImagePathList.FirstOrDefault(), UriKind.Absolute);
}
}
public BitmapImage FirstImage // getter - BitmapImage
{
get
{
if (String.IsNullOrEmpty(ImagePath[0])) return null;
BitmapImage temp = new BitmapImage();
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream file = ISF.OpenFile(ImagePath[0], FileMode.Open, FileAccess.Read))
temp.SetSource(file);
return temp;
}
}