带 WCF 服务和 MS Excel 数据库的简单 Metro 风格应用程序





5.00/5 (6投票s)
使用 WCF 创建 Metro 风格的应用程序。
介绍
如今,越来越多的技术爱好者对在 Windows 8 及其他平台上创建应用程序感兴趣。当我们分析这些应用程序时,大部分应用程序的数据都是静态的。对于应用程序来说,推荐使用轻量级应用程序,因为它需要下载到移动设备上。
本文将介绍如何创建带有 Excel 数据库的 WCF 服务,并从 Metro 风格的应用程序中消耗该服务。
它提供了将 WCF 服务托管到互联网域名并从 Metro 应用程序中消耗该服务的可能性。
先决条件
在进行下一步之前,您需要在开发环境中安装以下工具:
- Microsoft Visual Studio 2012
- Microsoft Office Excel 2007 或更高版本
- Blend for Microsoft Visual Studio 2012 (可选)
Blend 是一个用于为 Metro 应用程序创建更好 UI 的强大工具。它真的能帮助设计师在更短的时间内创建出色的 XAML 设计。
在 Visual Studio 中设置 WCF 服务
设置 WCF 服务很简单,因为 Visual Studio 会为示例 WCF 服务创建所需的文件夹和类文件。因此,我们需要在服务中更改的是:
- 服务名称
- 数据契约
- 操作契约
- 数据访问
以下步骤将解释如何设置 WCF 服务并从 MS Excel 工作表中读取数据。
- 打开 Visual Studio 2012,然后单击“新建项目”链接。在弹出窗口中,选择 WCF,然后选择 WCF 服务应用程序。 我们可以将服务重命名为“
PrayerTimeService
”,然后单击“确定”。 - 我们可以看到 Visual Studio 创建了一个默认服务,名为“Service1”。我们可以将其重命名为“PrayerTime.svc”。
- 现在我们需要将数据库文件添加到服务项目中。为此,右键单击项目“
PrayerTimeService
”,然后添加“新建文件夹”并将其重命名为“Data”。
我们的 Excel 数据表将如下所示。 - 您可以创建类似这样的 Excel 文件,或者从“下载源”链接下载。 数据获取查询会为一个月中的特定日期获取一行。因此,我们将像工作表一样总共拥有 366 行。
- 现在我们需要将此 Excel 文件添加到“Data”文件夹中。
- 然后,打开接口文件IPrayerTime.cs,并添加数据契约和服务契约定义。
- 右键单击文件“PrayerTime.svc”,然后转到“查看标记”。将服务更改为“
PrayerTimeService
”,如下所示。 - 打开PrayerTime.svc类文件,并在文件中进行以下更改。
- 将类名从“
Service1
”更改为“PrayerTime
” - 在标题中添加命名空间
- 在标题中添加命名空间
- 将以下代码复制并粘贴到类文件中。
- 现在 WCF 项目已准备好“生成”。我们可以生成项目并在浏览器中查看服务属性。右键单击服务项目,然后单击“在浏览器中查看”。
[ServiceContract]
public interface IPrayerTime
{
[OperationContract]
PrayerTimeData GetDohaPryaerTime(int month, int day);
}
[DataContract]
public class PrayerTimeData
{
string fajr;
string sunrise;
string duhr;
string asr;
string magrib;
string isha;
[DataMember]
public string Fajr
{
get { return fajr; }
set { fajr = value; }
}
[DataMember]
public string Sunrise
{
get { return sunrise; }
set { sunrise = value; }
}
[DataMember]
public string Duhr
{
get { return duhr; }
set { duhr = value; }
}
[DataMember]
public string Asr
{
get { return asr; }
set { asr = value; }
}
[DataMember]
public string Magrib
{
get { return magrib; }
set { magrib = value; }
}
[DataMember]
public string Isha
{
get { return isha; }
set { isha = value; }
}
}
}
<%@ ServiceHost Language="C#" Debug="true" Service="PrayerTimeService.PrayerTime" CodeBehind="PrayerTime.svc.cs" %>
using System.Web.Hosting;
由于我们将 Microsoft Excel 用作数据库,因此需要提供物理文件路径。因此,我们使用静态方法HostingEnvironment.MapPath(“Path”)
using System.Data.Common;
使用“DBProvideFactory.GetFactory
”方法需要此命名空间。 public class PrayerTime : IPrayerTime
{
public PrayerTimeData GetDohaPryaerTime(int day, int month)
{
string excelPath = HostingEnvironment.MapPath("~/Data/Doha_PrayerTimes.xlsx");
String ConnectionString = @"Data Source=" + excelPath + ";Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
PrayerTimeData prayerTimeData = new PrayerTimeData();
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = ConnectionString;
using (DbCommand command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "SELECT Fajr, Sunrise, Duhr, Asr, Magrib, Isha FROM [PrayerTime$] where Month = " + month.ToString() + " and Day = " + day.ToString() + " ;"; //Sheet1 here is the name of the sheet which is to be read
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
prayerTimeData.Fajr = Convert.ToDateTime(reader[0]).ToString("H:mm");
prayerTimeData.Sunrise = Convert.ToDateTime(reader[1]).ToString("H:mm");
prayerTimeData.Duhr = Convert.ToDateTime(reader[2]).ToString("H:mm");
prayerTimeData.Asr = Convert.ToDateTime(reader[3]).ToString("H:mm");
prayerTimeData.Magrib = Convert.ToDateTime(reader[4]).ToString("H:mm");
prayerTimeData.Isha = Convert.ToDateTime(reader[5]).ToString("H:mm");
}
}
}
}
return prayerTimeData;
}
}
在 Visual Studio 中设置 Metro 应用
在上一节中,我们创建了一个提供祈祷时间数据的 WCF 服务。现在我们可以创建一个 Windows 应用商店应用程序来消耗此服务并在 Metro 界面中显示它。这里我们将有四个任务:
- 创建项目
- 设计 XAML 界面
- 创建代码隐藏类
- 消耗 WCF 服务
以下 10 个步骤将解释如何设置 WCF 服务
并从 MS Excel 工作表中读取数据。
- 右键单击解决方案,添加新项目
- 选择 Windows 应用商店,然后选择空白应用 (XAML)
- 将名称更改为“
DohaPrayerTime
”,然后单击“确定”。 - 右键单击新项目,然后单击“设置为启动项目”选项。
- 转到 assets 文件夹,添加新图像“Prayer_Time_Icon.jpg”。此图像将在 MainPage 中使用。 我们还可以更改 Logo、small logo、启动屏幕和 store logo 图像的图片。
- 现在我们需要向应用程序添加服务引用。 右键单击“Service”文件夹,然后单击“添加服务引用”,然后单击“发现”按钮。由于我们的服务在解决方案中可用,Visual Studio 将会发现它。在弹出窗口中将服务名称提供为“PrayerTimeService”,然后单击“确定”。
- 打开MainPage.xaml文件,用新代码替换 XAML Grid 标签。
- 转到“设计”视图以查看页面的设计和布局。
- 现在我们需要编写代码隐藏逻辑。打开MainPage.xaml.cs并在类文件中粘贴代码。
- 运行应用程序。我们可以看到具有我们设计的应用程序,如下图所示,并且值是从 Excel 工作表通过 WCF 服务获得的。这个
是一个单页应用程序,我们可以通过添加更多页面、服务
和功能来增强它。



粘贴此 XAML 代码
<Grid Width="1000">
<Grid.Background>
<LinearGradientBrush EndPoint="0.005,0.185" StartPoint="0.999,0.982">
<GradientStop Color="#333399"/>
<GradientStop Color="#00CC99" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition Width="280" ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Canvas Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="5" >
<Canvas.Background>
<LinearGradientBrush EndPoint="1.007,1" StartPoint="0.005,0.013">
<GradientStop Color="#000066"/>
<GradientStop Color="#00CC99" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
<TextBlock Canvas.Left="25" Canvas.Top="30" Height="55" Width="437" Style="{StaticResource HeaderTextStyle}" >Doha Prayer Time</TextBlock>
<Image Canvas.Left="640" Source="Assets/Prayer_Time_Icon.jpg" Stretch="Fill" Canvas.Top="0" Height="300" Width="360" />
</Canvas>
<TextBlock Name="txtDate" HorizontalAlignment="Left" Margin="107,61,0,0" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="4"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="675"/>
<StackPanel Grid.Row="2" Grid.Column="3" Grid.ColumnSpan="2" Background="#577599" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Fajr</TextBlock>
<TextBlock Name="txtFajr" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
<StackPanel Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Background="#478599" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Sunrise</TextBlock>
<TextBlock Name="txtSunrise" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
<StackPanel Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Background="#389499" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Duhr</TextBlock>
<TextBlock Name="txtDuhr" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
<StackPanel Grid.Row="5" Grid.Column="3" Grid.ColumnSpan="2" Background="#24A899" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Asr</TextBlock>
<TextBlock Name="txtAsr" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
<StackPanel Grid.Row="6" Grid.Column="3" Grid.ColumnSpan="2" Background="#0FBD99" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Magrib</TextBlock>
<TextBlock Name="txtMagrib" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
<StackPanel Grid.Row="7" Grid.Column="3" Grid.ColumnSpan="2" Background="#00CC99" Orientation="Horizontal">
<TextBlock Margin="15,5,0,0" Style="{StaticResource SubheaderTextStyle}" >Isha</TextBlock>
<TextBlock Name="txtIsha" HorizontalAlignment="Left" Margin="15,5,0,0" Text="Time"
TextWrapping="Wrap" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}" Width="275"/>
</StackPanel>
</Grid>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
txtDate.Text = DateTime.Now.ToString("dddd dd MMM yyyy hh:mm");
ShowPrayerTime();
}
private async void ShowPrayerTime()
{
PrayerTimeService.PrayerTimeClient client = new PrayerTimeService.PrayerTimeClient();
PrayerTimeService.PrayerTimeData data = await client.GetDohaPryaerTimeAsync(DateTime.Today.Day, DateTime.Today.Month);
txtFajr.Text = data.Fajr;
txtSunrise.Text = data.Sunrise;
txtDuhr.Text = data.Duhr;
txtAsr.Text = data.Asr;
txtMagrib.Text = data.Magrib;
txtIsha.Text = data.Isha;
}
