以前写过一个wp8的工程,说实话那会的代码很麻烦,写起来费劲,另外还没多少人下载,不过到windows 10 开始微软出了UWP架构以后一切都像以前的winform wpf一样好转起来,新建一个工程以后模板很简洁。

现在就开始介绍一下基本控件的使用,为新手写程序提供一个例子。7天酒店没有官方客户端,不过不妨碍拿他做例子。

  由于以前代码使用到了json.net 为了减少代码修改,所以还是需要引入进来,但去官方网站下载的无法直接使用,说什么不适配,只要打开

然后呢搜索Json  其实我打开默认第二个就是

添加好之后呢

开始布局代码

在MainPage.xaml中写如下代码

<Page
x:Class="_7inns.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:_7inns"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<CollectionViewSource x:Name="itemcollectSource" IsSourceGrouped="true" ItemsPath="ItemContent" />
</Grid.Resources>
<Image Source="Assets/2.png" Margin="42,23,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="109" Width="94" />
<TextBlock Margin="42,193,264,0" TextWrapping="Wrap" Text="城市" VerticalAlignment="Top" FontSize="20" FontWeight="Bold"/>
<TextBlock Margin="42,252,264,0" TextWrapping="Wrap" Text="区域" VerticalAlignment="Top" FontSize="20" FontWeight="Bold"/>
<Button x:Name="buttonCities" Content="" HorizontalAlignment="Stretch" Margin="104,194,3,0" VerticalAlignment="Top" Click="buttonCities_Click" BorderThickness="1" Height="33"/>
<SemanticZoom x:Name="semanticZoomCities" Margin="0" ViewChangeCompleted="SemanticZoom_ViewChangeCompleted" Visibility="Collapsed" Canvas.ZIndex="5" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<SemanticZoom.ZoomedInView>
<ListView x:Name="ListCities" ScrollViewer.IsHorizontalScrollChainingEnabled="False" ScrollViewer.IsVerticalScrollChainingEnabled="False" ItemClick="ListCities_ItemClick" SelectionChanged="ListCities_SelectionChanged">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border Background="#FFECAE0A" Height="60" Width="60">
<TextBlock Text="{Binding Key}" FontSize="50" Foreground="{ThemeResource ApplicationForegroundThemeBrush}"></TextBlock>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding name}" Height="40" FontSize="30"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
<GridView x:Name="GridCitiesFirstName" ScrollViewer.IsHorizontalScrollChainingEnabled="False" ScrollViewer.IsVerticalScrollChainingEnabled="False" Margin="0,100" HorizontalAlignment="Center" VerticalAlignment="Center">
<GridView.ItemTemplate>
<DataTemplate>
<Border Height="60">
<TextBlock Text="{Binding Group.Key}" FontSize="24"></TextBlock>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid ItemWidth="60" ItemHeight="60" VerticalChildrenAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="Background" Value="#FFECAE0A" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</GridView.ItemContainerStyle>
</GridView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
<ComboBox x:Name="comboBoxDistricts" Margin="107,251,0,0" VerticalAlignment="Top" BorderThickness="1" SelectionChanged="comboBoxDistricts_SelectionChanged" Width="251"/>
<TextBlock Margin="42,368,264,0" TextWrapping="Wrap" Text="关键字" VerticalAlignment="Top" FontSize="20" FontWeight="Bold"/>
<TextBlock Margin="42,442,264,0" TextWrapping="Wrap" Text="入住时间" VerticalAlignment="Top" FontSize="20" FontWeight="Bold"/>
<Button x:Name="search" Content="搜索" HorizontalAlignment="Right" Margin="0,0,10,49" VerticalAlignment="Bottom" Click="search_Click" FontSize="20" FontWeight="Bold" BorderThickness="1" RenderTransformOrigin="0.925,0.455" Width="129"/>
<DatePicker x:Name="inRoomDate" Margin="96,454,0,0" VerticalAlignment="Top" DateChanged="inRoomDate_DateChanged" BorderThickness="1" Width="255"/>
<TextBox x:Name="keyword" Margin="104,358,1,0" TextWrapping="Wrap" VerticalAlignment="Top"/> </Grid>
</Page>

恕当时没什么布局概念,基本就是手动布局,罗列控件。布局的事情以后再慢慢写,不过微软的教程或者其他书里面这块讲的很清楚,各种panel grid 布局。

这个里面基本就是一些textblock 跟textbox button 和一个SemanticZoom  SemanticZoom的效果就是联系人菜单那个感觉,这里用来选择城市

先看一下选择前的

点一下字母 就会有

然后可以选个北京

后台呢就会对这个选择的city进行行政区查询

        private async void ListCities_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("ListCities_SelectionChanged"); if (e.AddedItems.Count == )
{
currentCityItem = (CityItem)e.AddedItems.First();
Debug.WriteLine(currentCityItem.name);
buttonCities.Content = currentCityItem.name;
} string di = await FindDistrict(currentCityItem.id);
int total = di.Length;
semanticZoomCities.Visibility = Visibility.Collapsed;
if (total <= )
return; JArray jobjectAllDistricts = JArray.Parse(di);
comboBoxDistricts.PlaceholderText = "";
comboBoxDistricts.Items.Clear();
if (DItem != null)
{
DItem.Clear();
for (int i = ; i < jobjectAllDistricts.Count; i++)
{
DItem.Add(new DistrictItem { id = (string)jobjectAllDistricts[i]["id"], name = (string)jobjectAllDistricts[i]["name"] });
//Debug.WriteLine((string)jobjectAllDistricts[i]["name"]); comboBoxDistricts.Items.Add((string)jobjectAllDistricts[i]["name"]);
}
} Debug.WriteLine("Total bytes returned: " + total); }
FindDistrict这个函数就需要用到http请求了

当时这么写的 可能是win10推荐的代码 不过能用
        private async Task<string> FindDistrict(string id)
{
string url = @"http://m.7daysinn.cn/q/city/" + id + @"/districts"; HttpClientHandler handler = new HttpClientHandler(); handler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(handler); // Limit the max buffer size for the response so we don't get overwhelmed httpClient.MaxResponseContentBufferSize = ;
string result;
try
{
result = await httpClient.GetStringAsync(url);
return result;
}
catch (Exception)
{ } return "";
}

点击搜索按钮
就可以得到这个日子的酒店列表啦
        private async void search_Click(object sender, RoutedEventArgs e)
{
try
{
string url = @"http://m.7daysinn.cn/q/inns?cityId=" + currentCityItem.id; if (currentDistrictItem != null && currentDistrictItem.id != "")
{
url += "&districtId=";
url += currentDistrictItem.id;
} if (keyword.Text != "")
{
url += "&keyword=";
url += Uri.EscapeUriString(keyword.Text);
} url += "&fromDate=" + inDate.Date.ToString("yy-MM-dd") + "&days=1&getQuota=true"; HttpClientHandler handler = new HttpClientHandler(); handler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(handler); // Limit the max buffer size for the response so we don't get overwhelmed httpClient.MaxResponseContentBufferSize = ;
ParameterObject po = new ParameterObject();
po.result = await httpClient.GetStringAsync(url);
po.date = inDate;
Frame.Navigate(typeof(InnStatus), po);
}
catch (Exception)
{ } }
Frame.Navigate(typeof(InnStatus), po);这句呢就是把数据传到第二个页面

第二个页面Innstatus 解析并且展示出来
 protected override void OnNavigatedTo(NavigationEventArgs e)
{
innstatus = (ParameterObject)e.Parameter;
try
{
List<InnItem> innItems = new List<InnItem>();
JObject jobjectAllInns = JObject.Parse(innstatus.result);
JArray jaRooms = (JArray)jobjectAllInns["content"]; for (int i = ; i < jaRooms.Count; i++)
{
innItems.Add(new InnItem
{
innId = (string)jaRooms[i]["innId"],
name = (string)jaRooms[i]["name"],
firstCharsOfPinyin = (string)jaRooms[i]["firstCharsOfPinyin"],
cityId = (string)jaRooms[i]["cityId"],
cityName = (string)jaRooms[i]["cityName"],
districtId = (string)jaRooms[i]["districtId"],
districtName = (string)jaRooms[i]["districtName"],
address = (string)jaRooms[i]["address"],
score = (string)jaRooms[i]["score"],
orderWeight = (string)jaRooms[i]["orderWeight"],
thumbnailPath = (string)jaRooms[i]["thumbnailPath"],
lowestPrice = (string)jaRooms[i]["lowestPrice"],
hasRoom = (string)jaRooms[i]["hasRoom"],
hasWifi = (string)jaRooms[i]["hasWifi"],
hasPark = (string)jaRooms[i]["hasPark"],
canUseCashCoupon = (string)jaRooms[i]["canUseCashCoupon"],
brandId = (string)jaRooms[i]["brandId"],
//currentActivities = (string)jaRooms[i]["currentActivities"],
}); } this.inncollectSource.Source = innItems;
ContentInnsList.ItemsSource = inncollectSource.View; }
catch (Exception)
{ } }

效果如下

 
第二个页面的xaml代码就这么写的
一个数据模板
<Page
x:Class="_7inns.InnStatus"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:_7inns"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid x:Name="LayoutRoot"> <Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!-- TitlePanel -->
<StackPanel Grid.Row="0" Margin="0" Orientation="Horizontal">
<TextBlock Text="酒店状态" Style="{ThemeResource TitleTextBlockStyle}" Typography.Capitals="SmallCaps" Margin="0,15,0,16.5" RenderTransformOrigin="1.473,0.155" Width="300" TextAlignment="Center"/>
</StackPanel> <!--TODO: Content should be placed within the following grid-->
<Grid Grid.Row="1" x:Name="ContentRoom">
<Grid.Resources>
<CollectionViewSource x:Name="inncollectSource" IsSourceGrouped="False" ItemsPath="" />
</Grid.Resources>
<ListView x:Name="ContentInnsList" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" IsItemClickEnabled="True" ItemClick="ContentInnsList_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding name}" Height="40" FontSize="30"></TextBlock>
<TextBlock Text="{Binding address}" Height="30" FontSize="14" Foreground="#FFECAE0A"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> </Grid> </Grid>
</Page>

好啦 要介绍的就这么多

工程我上传上来好了方便新手调试

http://files.cnblogs.com/files/emsoro/7inns.zip

 

UWP入门一 7天酒店客户端源码及说明的更多相关文章

  1. swift实现饭否应用客户端源码

    swift 版 iOS 饭否客户端 源码下载:http://code.662p.com/view/13318.html 饭否是中国大陆地区第一家提供微博服务的网站,被称为中国版Twitter.用户可通 ...

  2. android版高仿淘宝客户端源码V2.3

    android版高仿淘宝客户端源码V2.3,这个版本我已经更新到2.3了,源码也上传到源码天堂那里了,大家可以看一下吧,该应用实现了我们常用的购物功能了,也就是在手机上进行网购的流程的,如查看产品(浏 ...

  3. C#中国象棋+游戏大厅 服务器 + 客户端源码

    来源:www.ajerp.com/bbs C#中国象棋+游戏大厅 服务器 + 客户端源码 源码开源 C#版中国象棋(附游戏大厅) 基于前人大虾的修改版 主要用委托实现 服务器支持在线人数,大厅桌数的设 ...

  4. FileZilla客户端源码解析

    FileZilla客户端源码解析 FTP是TCP/IP协议组的协议,有指令通路和数据通路两条通道.一般来说,FTP标准命令TCP端口号是21,Port方式数据传输端口是20. FileZilla作为p ...

  5. vs2008编译FileZilla客户端源码

    vs2008编译FileZilla客户端源码 下载FileZilla客户端源码,下载地址https://download.filezilla-project.org/. FileZilla客户端解决方 ...

  6. Netty5客户端源码解析

    Netty5客户端源码解析 今天来分析下netty5的客户端源码,示例代码如下: import io.netty.bootstrap.Bootstrap; import io.netty.channe ...

  7. python编程从入门到实践 alien invasion 项目源码

    现在上传一个 python编程从入门到实践 alien invasion 项目源码 以供大家学习参考 跟官方版本可能不太一样,因为是自己写的 也算是给新手一个参考 我用的环境是pycharm 可能需要 ...

  8. MQTT再学习 -- MQTT 客户端源码分析

    MQTT 源码分析,搜索了一下发现网络上讲的很少,多是逍遥子的那几篇. 参看:逍遥子_mosquitto源码分析系列 参看:MQTT libmosquitto源码分析 参看:Mosquitto学习笔记 ...

  9. Zookeeper 源码(三)Zookeeper 客户端源码

    Zookeeper 源码(三)Zookeeper 客户端源码 Zookeeper 客户端主要有以下几个重要的组件.客户端会话创建可以分为三个阶段:一是初始化阶段.二是会话创建阶段.三是响应处理阶段. ...

随机推荐

  1. 注意EntityFramework.extended中的坑

    EntityFramework.extended 的好处就不用多说了 详情:https://github.com/loresoft/EntityFramework.Extended 但是使用时还是要注 ...

  2. POJ 2502 Subway dij

    这个题的输入输出注意一下就好 #include<cstdio> #include<cstring> #include<queue> #include<cstd ...

  3. java基础全套

    这是我自己早前听课时整理的java基础全套知识  使用于初学者 也可以适用于中级的程序员 我做成了chm文档的类型  你们可以下载  笔记是比较系统全面,可以抵得上市场上90%的学习资料.讨厌那些随便 ...

  4. makefile 中 $@ $^ %< 使用

    这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将会学到以下内容: 源程序编译 Makefile的编写 程序库的链接 程序的调试 头文件和系统求助 1.源程序的编译 在L ...

  5. 枚举在c与c++中定义的不同

    众所周知的,枚举是在运行期才决定枚举变量的值的,而不是像宏一样在预编译的时候就进行值得替换. 而且c标准规定: size(int) <= size(enum)<=系统所能表示的最大范围的值 ...

  6. Web Services and C# Enums -摘自网络

    Web Service Transparency .NET support for web services is excellent in creating illusion of transpar ...

  7. poj1149--PIGS(最大流)

    题意: 有m个猪圈 每个猪圈有不同数量的猪 [0, 1000]有n个顾客 每个顾客需要Bi头猪 有Ai个钥匙 能打开Ai个不同的猪圈顾客按顺序来买猪 只能买他有钥匙的猪 买完之后 这几个猪圈的猪可以相 ...

  8. PL/SQL基础

    打印  hi set serveroutput on   --打开输出开关 declare           --说明部分(变量说明,光标申明或者例外说明) begin           --程序 ...

  9. Delphi- 内置数据库的使用例子BDE

    以前开发时经常使用一些大型的数据库,像这样小的数据库还是前段时间才看到.看看Delphi怎么使用内置的数据库, 先在BDE里拉两个数据库控件.DataBase和Table,然后再拉两个数据库控件Dat ...

  10. chrmoe debug

    一.右击,点击审查元素 二.打开后,每个tab是干什么用的呢? 三.调试样式 选中Elemes后,右边有个窗口,这里是CSS的样式,可以直接选中元素,并且在这里修改样式.这样调试很方便,效率也很高.当 ...