UWP入门一 7天酒店客户端源码及说明

  以前写过一个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 == 1)
            {
                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 <= 6)
                return;

            JArray jobjectAllDistricts = JArray.Parse(di);
            comboBoxDistricts.PlaceholderText = "";
            comboBoxDistricts.Items.Clear();
            if (DItem != null)
            {
                DItem.Clear();
                for (int i = 0; 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 = 256000;
            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 = 256000;
                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 = 0; 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

 
时间: 2024-12-23 04:19:00

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

开源中国安卓客户端源码之自定义控件---ScreenShotView

首先,感谢开源中国的开源精神.当初学者拿到客户端源码时,可能会对其中的项目结构和代码产生许多困惑,不知道该从何下手,当然我也是其中一员,接触安卓时间不长,也不是很精通,但是通过一段时间的琢磨,慢慢地领会到其中的一些编程方法,我只是想把我弄明白的这些知识通过博客的形式记录下来,以备以后查看,当然也可以帮助到更多的初学者.我的主要工作是给代码添加注释,理顺调用关系,让初学者更快.更深刻地理解代码的含义,领会其精神.首先大家从http://git.oschina.net/oschina/android

kbengine mmo源码(完整服务端源码+资源+完整客户端源码)

kbengine mmo源码(完整服务端源码+资源+完整客户端源码) PyConsole: display server information. PyConsole: Stop the server. Guiconsole: debug. Guiconsole: log. Demo: Ogre. Demo: Unity3d. demo视频:http://v.youku.com/v_show/id_XNjU5Nzc0MDQ4.html 下载地址: demo下载地址:http://sourcefo

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

来源:www.ajerp.com/bbs C#中国象棋+游戏大厅 服务器 + 客户端源码 源码开源 C#版中国象棋(附游戏大厅) 基于前人大虾的修改版 主要用委托实现 服务器支持在线人数,大厅桌数的设置 游戏互不干扰 这个象棋大厅程序完全可以当做是C#委托事件的教程,而且游戏大厅功能也很多,服务器还可设置人数和桌数,大厅客户端也实时更新相关数据. 源码下载地址:http://www.ajerp.com/bbs/forum.php?mod=viewthread&tid=29&extra=pa

H264音视频直播系统 服务器端+客户端源码 可用于视频聊天、视频会议

H264音视频直播系统  服务器端+客户端源码 可用于视频聊天.视频会议 RTP协议实现音视频网络实时直播,采用H.264和AAC编码,码率极低,同时有较高的视频清晰度和音频音质,可用于视频聊天.视频会议.摄像头监控等多种应用场景. 发布端,选择视频和音频设备,指定服务器发布直播源 接收端,接收发布者的音视频并播放.占用带宽很低,如图所示.采用动态码率,平时在4~5KB/s左右,动作幅度较大时在8KB/s. C++代码,VS2010工程,包含全部代码,不用其它依赖项,可直接编译 接口调用简单

春秋旅行安卓客户端源码项目

春秋旅行安卓客户端源码,这是一款高度模仿春秋旅行app开发的一款应用,通过抓包工具获取到的接口,希望大家能够喜欢,并且对大家的学习能够有所帮助. 源码下载:http://code.662p.com/view/12837.html <ignore_js_op> <ignore_js_op> 详细说明:http://android.662p.com/thread-6460-1-1.html

Android应用源码仿暴风影音安卓客户端源码

Android应用源码仿暴风影音安卓客户端源码 本项目是一个模仿暴风影音的UI项目源码,仿照的界面有菜单页,主页,分类页等,项目内的所有数据都使用的本地模拟数据,仿照度一般在大分辨设备上布局显示会有问题,480x800的分辨率应该正合适,默认编译版本4.2.2编码GBK,注释不多,需要的朋友可以下载看一下. 下载地址:http://www.devstore.cn/code/info/133.html 运行截图:     版权声明:本文为博主原创文章,未经博主允许不得转载.

Android源码下载之《Android新闻客户端源码》

介绍 Android新闻客户端源码,功能上分为:新闻.关注.读报.微博.里面比较有特色的就是读报功能,真正安装报纸的排版进行读报,给人得感觉就像是在读真实的报纸.其实即使首页的动态云标签非常有特色,可以随机显示出最新新闻热词,用手滑动还能动态切换.此Demo即可作为学习使用也可作为商业使用,本站保留原创权利! 测试环境 [Code4Apk]编译测试,测试环境:eclipse 3.7 , android 2.3.3 效果图 下载 更多精彩请关注Android源码下载:Android源码下载  (h

HTTP/HTTPS客户端源码示例

HTTP/HTTPS客户端源码示例 环境:  zlib-1.2.8  openssl-1.0.1g  curl-7.36 Author:  Kagula LastUpdateDate: 2014-04 阅读前提:CMake工具的基本使用.配置openssl-1.0.1g 开发环境 编译zlib库 下载zlib-1.2.8.tar.gz并解压缩到" D:\SDK\zlib-1.2.8",使用CMake工具生成zlib.sln,在Visual Studio2013中打开并编译即可. 编译c

仿糗事百科Android客户端源码

支持平台:Android      运行环境:Eclipse       开发语言:Java 下载地址:http://www.devstore.cn/code/info/201.html 源码简介 仿糗事百科Android客户端 源码运行截图