日期:2014-05-18 浏览次数:21151 次
免责声明:本文章由fengyun1989创作,采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。
首先,打开VS2012,然后新建一个工程,命名为TimeTable。

点击确定后,看到编译器如下地方:
现在空的工程也可以运行,选择Simulator就是模拟器运行,选择本地计算机就是直接在本机运行。现在就可以运行看看,就是黑屏。
在本机运行的话,如果想退出程序,可以把光标放到左上角,等待出现桌面缩略图就点击,这样就能退出了。
数据绑定
细想下这个工程,首先要做的是要把课程的数据显示出来,那么,就要用到数据绑定了。熟悉wp7和silverlight开发的朋友,接下来你就会发现,在win8(XAML+c#)开发当中,数据绑定是一样的。
首先,我们修改下我们的工程,新建一个命名为Resources.然后在 文件夹右键--添加--新建项,选取资源字典。命名为:MyDictionary.xaml。添加一行画刷来作为背景。并且修改代码如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TimeTable.Resources">
<SolidColorBrush Color="#3E790A" x:Key="AppBackgroundColor"/>
</ResourceDictionary>
现在打开App.xaml。添加对MyDicitonary.xaml的声明。修改如下:<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="Resources/MyDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Grid Background="{StaticResource AppBackgroundColor}">
现在,就能看到背景色变为了深绿色。接下来就进行数据定义吧。我们的数据类都继承INotifyPropertyChanged接口,以便数据更新的时候能够自己通知控件更新。添加一个Data的文件夹。然后新建添加三个类,分别是ViewModel.cs,WeekdayItem.cs,ScheduleItem.cs .类图如下:

代码如下:
class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<WeekdayItem> weekdayList;
public ObservableCollection<WeekdayItem> WeekdayList { get { return weekdayList; } }
private int selectedItemIndex;
public int SelectedItemIndex
{
get { return selectedItemIndex; }
set { selectedItemIndex = value; NotifyPropertyChanged("SelectedItemIndex"); }
}
public ViewModel()
{
weekdayList = new ObservableCollection<WeekdayItem>();
selectedItemIndex = -1;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
class WeekdayItem : INotifyPropertyChanged
{
private ObservableCollection<ScheduleItem> scheduleList;
private int scheduleSelected;
public int ScheduleSelected
{
get { return scheduleSelected; }
set { scheduleSelected = value; NotifyPropertyChanged("ScheduleSelected"); }
}
public ObservableCollection<ScheduleItem> ScheduleList
{
get { return scheduleList; }
}
public WeekdayItem()