none
关于数据绑定的 RRS feed

  • 问题

  •    

    下面的GetLocalFiles();发生警告

    怎样解决?

      public SampleDataSource()
            {
                // Loca local data
                GetLocalFiles();
            }

            /// <summary>
            /// Bind data to AllGroups
            /// </summary>
            public async Task GetLocalFiles()
            {
                string folderIndex = "Index_"; // Foler filter
                var Path = @"Data"; //Binding Folder
     
                var Folders = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(Path).GetResults().GetFoldersAsync();
                foreach (var folder in Folders)
                {
                    var group = new SampleDataGroup(folder.Name, folder.Name, "", "", "");
                    var files = await folder.GetFilesAsync();

                    foreach (var file in files)
                    {
                        if (!file.FileType.Equals(TXTFILEEXTENSION))
                        {
                            string filename = file.Name.Split(new char[] { '.' })[0];
                            if (filename.Contains(folderIndex))
                            {
                                group.SetImage("/" + Path + "/" + folder.Name + "/" + file.Name);
                            }
                            else
                            {
                                var pictureItem = new SampleDataItem(filename, filename, "",
                                    "/" + Path + "/" + folder.Name + "/" + file.Name, "", "", group);
                                group.Items.Add(pictureItem);
                            }
                        }
                    }

                    foreach (var file in files)
                    {
                        if (file.FileType.Equals(TXTFILEEXTENSION))
                        {
                            string filename = file.Name.Split(new char[] { '.' })[0];
                            if (filename.Contains("Index_"))
                            {
                                group.Description = await Windows.Storage.FileIO.ReadTextAsync(file);
                            }
                            else
                            {
                                var item = group.Items.First<SampleDataItem>(i => i.Title.Split(new char[] { '.' })[0] == filename);
                                item.Description = item.Content = await Windows.Storage.FileIO.ReadTextAsync(file);
                            }
                        }
                    }
                    _sampleDataSource.AllGroups.Add(group);
                }
            }

            public string TXTFILEEXTENSION { get; set; }

    2012年12月5日 10:00

全部回复

  • Hi,

    你给的代码并不全,比如SampleDataGroup怎么定义的?我也没办法测试。你需要提供更多的信息,比如哪一行出了错误,具体是什么错误?


    Aaron
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年12月7日 6:06
    版主
  • namespace Beauty.Data
    {
        /// <summary>
        /// <see cref="SampleDataItem"/> 和 <see cref="SampleDataGroup"/> 的基类,
        /// 定义对两者通用的属性。
        /// </summary>
        [Windows.Foundation.Metadata.WebHostHidden]
        public abstract class SampleDataCommon : Beauty.Common.BindableBase
        {
            private static Uri _baseUri = new Uri("ms-appx:///");
    
            public SampleDataCommon(String uniqueId, String title, String subtitle, String imagePath, String description)
            {
                this._uniqueId = uniqueId;
                this._title = title;
                this._subtitle = subtitle;
                this._description = description;
                this._imagePath = imagePath;
            }
    
            private string _uniqueId = string.Empty;
            public string UniqueId
            {
                get { return this._uniqueId; }
                set { this.SetProperty(ref this._uniqueId, value); }
            }
    
            private string _title = string.Empty;
            public string Title
            {
                get { return this._title; }
                set { this.SetProperty(ref this._title, value); }
            }
    
            private string _subtitle = string.Empty;
            public string Subtitle
            {
                get { return this._subtitle; }
                set { this.SetProperty(ref this._subtitle, value); }
            }
    
            private string _description = string.Empty;
            public string Description
            {
                get { return this._description; }
                set { this.SetProperty(ref this._description, value); }
            }
    
            private ImageSource _image = null;
            private String _imagePath = null;
            public ImageSource Image
            {
                get
                {
                    if (this._image == null && this._imagePath != null)
                    {
                        this._image = new BitmapImage(new Uri(SampleDataCommon._baseUri, this._imagePath));
                    }
                    return this._image;
                }
    
                set
                {
                    this._imagePath = null;
                    this.SetProperty(ref this._image, value);
                }
            }
    
            public void SetImage(String path)
            {
                this._image = null;
                this._imagePath = path;
                this.OnPropertyChanged("Image");
            }
    
            public override string ToString()
            {
                return this.Title;
            }
        }
    
        /// <summary>
        /// 泛型项数据模型。
        /// </summary>
        public class SampleDataItem : SampleDataCommon
        {
            public SampleDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, SampleDataGroup group)
                : base(uniqueId, title, subtitle, imagePath, description)
            {
                this._content = content;
                this._group = group;
            }
    
            private string _content = string.Empty;
            public string Content
            {
                get { return this._content; }
                set { this.SetProperty(ref this._content, value); }
            }
    
            private SampleDataGroup _group;
            public SampleDataGroup Group
            {
                get { return this._group; }
                set { this.SetProperty(ref this._group, value); }
            }
        }
    
        /// <summary>
        /// 泛型组数据模型。
        /// </summary>
        public class SampleDataGroup : SampleDataCommon
        {
            public SampleDataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
                : base(uniqueId, title, subtitle, imagePath, description)
            {
                Items.CollectionChanged += ItemsCollectionChanged;
            }
    
            private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                // 由于两个原因提供要从 GroupedItemsPage 绑定到的完整
                // 项集合的子集: GridView 不会虚拟化大型项集合,并且它
                // 可在浏览包含大量项的组时改进用户
                // 体验。
                //
                // 最多显示 12 项,因为无论显示 1、2、3、4 还是 6 行,
                // 它都生成填充网格列
    
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        if (e.NewStartingIndex < 6)
                        {
                            TopItems.Insert(e.NewStartingIndex,Items[e.NewStartingIndex]);
                            if (TopItems.Count > 6)
                            {
                                TopItems.RemoveAt(6);
                            }
                        }
                        break;
                    case NotifyCollectionChangedAction.Move:
                        if (e.OldStartingIndex < 6 && e.NewStartingIndex < 6)
                        {
                            TopItems.Move(e.OldStartingIndex, e.NewStartingIndex);
                        }
                        else if (e.OldStartingIndex < 6)
                        {
                            TopItems.RemoveAt(e.OldStartingIndex);
                            TopItems.Add(Items[5]);
                        }
                        else if (e.NewStartingIndex < 6)
                        {
                            TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
                            TopItems.RemoveAt(6);
                        }
                        break;
                    case NotifyCollectionChangedAction.Remove:
                        if (e.OldStartingIndex < 6)
                        {
                            TopItems.RemoveAt(e.OldStartingIndex);
                            if (Items.Count >= 6)
                            {
                                TopItems.Add(Items[5]);
                            }
                        }
                        break;
                    case NotifyCollectionChangedAction.Replace:
                        if (e.OldStartingIndex < 6)
                        {
                            TopItems[e.OldStartingIndex] = Items[e.OldStartingIndex];
                        }
                        break;
                    case NotifyCollectionChangedAction.Reset:
                        TopItems.Clear();
                        while (TopItems.Count < Items.Count && TopItems.Count < 6)
                        {
                            TopItems.Add(Items[TopItems.Count]);
                        }
                        break;
                }
            }
    
            private ObservableCollection<SampleDataItem> _items = new ObservableCollection<SampleDataItem>();
            public ObservableCollection<SampleDataItem> Items
            {
                get { return this._items; }
            }
    
            private ObservableCollection<SampleDataItem> _topItem = new ObservableCollection<SampleDataItem>();
            public ObservableCollection<SampleDataItem> TopItems
            {
                get {return this._topItem; }
            }
        }
    
        /// <summary>
        /// 创建包含硬编码内容的组和项的集合。
        /// 
        /// SampleDataSource 用占位符数据而不是实时生产数据
        /// 初始化,因此在设计时和运行时均需提供示例数据。
        /// </summary>
        public sealed class SampleDataSource
        {
            private const string TXTFILEEXTENSION = ".txt";
            private static SampleDataSource _sampleDataSource = new SampleDataSource();
    
            private ObservableCollection<SampleDataGroup> _allGroups = new ObservableCollection<SampleDataGroup>();
            public ObservableCollection<SampleDataGroup> AllGroups
            {
                get { return this._allGroups; }
            }
    
            public static IEnumerable<SampleDataGroup> GetGroups(string uniqueId)
            {
                if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");
                
                return _sampleDataSource.AllGroups;
            }
    
            public static SampleDataGroup GetGroup(string uniqueId)
            {
                // 对于小型数据集可接受简单线性搜索
                var matches = _sampleDataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));
                if (matches.Count() == 1) return matches.First();
                return null;
            }
    
            public static SampleDataItem GetItem(string uniqueId)
            {
                // 对于小型数据集可接受简单线性搜索
                var matches = _sampleDataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
                if (matches.Count() == 1) return matches.First();
                return null;
            }
    
            public SampleDataSource()
            {
                // Loca local data
                GetLocalFiles();
            }
    
            /// <summary>
            /// Bind data to AllGroups
            /// </summary>
            public async Task  GetLocalFiles()
            {
                string folderIndex = "Index_"; // Foler filter
                var Path = @"Data"; //Binding Folder
     
                var Folders = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(Path).GetResults().GetFoldersAsync();
                foreach (var folder in Folders)
                {
                    var group = new SampleDataGroup(folder.Name, folder.Name, "", "", "");
                    var files = await folder.GetFilesAsync();
    
                    foreach (var file in files)
                    {
                        if (!file.FileType.Equals(TXTFILEEXTENSION))
                        {
                            string filename = file.Name.Split(new char[] { '.' })[0];
                            if (filename.Contains(folderIndex))
                            {
                                group.SetImage("/" + Path + "/" + folder.Name + "/" + file.Name);
                            }
                            else
                            {
                                var pictureItem = new SampleDataItem(filename, filename, "",
                                    "/" + Path + "/" + folder.Name + "/" + file.Name, "", "", group);
                                group.Items.Add(pictureItem);
                            }
                        }
                    }
    
                    foreach (var file in files)
                    {
                        if (file.FileType.Equals(TXTFILEEXTENSION))
                        {
                            string filename = file.Name.Split(new char[] { '.' })[0];
                            if (filename.Contains("Index_"))
                            {
                                group.Description = await Windows.Storage.FileIO.ReadTextAsync(file);
                            }
                            else
                            {
                                var item = group.Items.First<SampleDataItem>(i => i.Title.Split(new char[] { '.' })[0] == filename);
                                item.Description = item.Content = await Windows.Storage.FileIO.ReadTextAsync(file);
                            }
                        }
                    }
                    _sampleDataSource.AllGroups.Add(group);
                }
            }

    2012年12月23日 4:44