做了将近半年的windows metro应用程序开发,从对这个平台毫无了解,到现在基本上都可以实现,经历了懵懂时候的无从下手,对C#编程中细节的不了解,对技术的模糊,到现在的基本上有所心得,实在是很不容易.今天是第一次在CSDN上写博客(基本上在所有互联网上都没写过两三次),如果写的不好,内容啥的没说清楚,还请各位大牛不要见笑!
一些最基本的简单的东西我就不赘述了(当然过于复杂的我也不会......),今天就跟大家介绍一下数据绑定.
一 数据绑定
数据绑定在windows应用商店应用开发中是非常重要的,也是我们经常用到的东西.
下面我们介绍一下如何在GridView中实现数据绑定(ListView.FlipView中也是一样的)
首先在xmal中的代码:
<GridView x:Name="ItemSource_GridView" Padding="20" Margin="20,45,20,20" ItemClick="ItemSource_GridView_ItemClick" IsItemClickEnabled="True">
<GridView.ItemTemplate>
<DataTemplate>
<Border Height="150" Width="250" Background="Purple">
<Grid Height="150" Width="250">
<Image Source="{Binding Item_Thumbnail}"/>
<Grid Background="Black" Opacity="0.6" Height="40" VerticalAlignment="Bottom"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
<Image Source="{Binding Item_Image}" Width="36" Height="36" Margin="3,0,3,0"/>
<TextBlock Text="{Binding Item_Name}" FontSize="18" VerticalAlignment="Center" TextTrimming="WordEllipsis"/>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
我们可以从上面代码中看到,红色字体部分分别绑定到了三个不同的值上面,接下来,我们在C#代码中定义一个新的类
class LibraryItem
{
public BitmapImage Item_Thumbnail { get;set;}
public BitmapImage Item_Image { get; set; }
public IStorageItem Item_Source { get; set; }
public string Item_Name { get; set; }
}
其中一定要注意每个成员的get和set属性!
然后,
List<LibraryItem> resourceList = new List<LibraryItem>();
try
{
IReadOnlyList<IStorageItem> storageItem = await KnownFolders.VideosLibrary.GetItemsAsync();
if (storageItem.Count > 0)
{
foreach (IStorageItem item in storageItem)
{
if (item.IsOfType(StorageItemTypes.Folder))
{
StorageFolder folder = item as StorageFolder;
StorageItemThumbnail thumbnail = await folder.GetThumbnailAsync(ThumbnailMode.VideosView,190);
if (thumbnail != null)
{
using (IRandomAccessStream stream = thumbnail.CloneStream())
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
LibraryItem folderItem = new LibraryItem
{
Item_Thumbnail = bitmapImage,
Item_Source = folder,
Item_Name = folder.Name,
Item_Image = new BitmapImage(new Uri("ms-appx:///Images/MainPage/videoFolder.png")),
};
resourceList.Add(folderItem);
}
}
else
{
LibraryItem folderItem = new LibraryItem
{
Item_Thumbnail = null,
Item_Source = folder,
Item_Name = folder.Name,
Item_Image = new BitmapImage(new Uri("ms-appx:///Images/MainPage/videoFolder.png")),
};
resourceList.Add(folderItem);
}
}
else if (item.IsOfType(StorageItemTypes.File))
{
StorageFile file = item as StorageFile;
if (fileTypeFilter.Contains(file.FileType.ToLower()))
{
StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.VideosView, 190);
using (IRandomAccessStream stream = thumbnail.CloneStream())
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
LibraryItem fileItem = new LibraryItem
{
Item_Thumbnail = bitmapImage,
Item_Source = file,
Item_Name = file.Name,
Item_Image = new BitmapImage(new Uri("ms-appx:///Images/MainPage/videoFile.png")),
};
resourceList.Add(fileItem);
}
}
}
}
}
ItemSource_GridView.ItemsSource = resourceList;
}
catch(InvalidOperationException e)
{
UC.UserControl_TipMessage uc_tm = new UC.UserControl_TipMessage(e.Message);
uc_tm.Show();
}
以上是实现的效果.
之后我会和大家介绍一下Custom Control,自定义控件,这是一个能让你的应用程序非常个性化的控件,敬请期待!