感谢台风, 这个十一长假让我好好的休息了一回, 睡觉到腰酸背疼, 看电影看到眼发红. 今天最后一天, 不敢出去逛, 不知道哪会还会下暴雨...

嗯嗯..这个项目其实在十一之前就开始了, 工作无聊,没有新任务, 我就搞起它.

至于为什么选 Discuz 的 BBS , 因为我常上的几个网站, 都有一堆的 APP , 官方的, 第三方的 . BBS 虽然已经没落了, 但是官方的 APP 居然用不了!

写这个东西之前, 本来想拿来看 1024 的, 但是 1024 要么不是最新版本, 要么禁用了 API, 我就只能哈哈哈, 拿"以前" 最常上的 BBS 的 API 开始了.

源码:

https://github.com/gruan01/Discuz.Mobi

上几张图:

Andorid

WP

IOS : 没有, 只顾睡觉看电影了, 没有搞.

API

Discuz! X3.x 及已上版本内置 API , 低版本中, 用插件的形式提供.

这里是几个主要的 API :

版块列表 : http://xxx/api/mobile/index.php?mobile=no&version=1&module=forumindex

版块的主题列表 : http://xxx/api/mobile/index.php?mobile=no&version=1&module=forumdisplay&fid=11  其中的 fid 是上个 API 中返回值中的 fid , 即版块ID, 还可以有 page, tpp (pageSize)

主题详细: http://xxx/api/mobile/index.php?mobile=no&version=1&module=viewthread&tid=3287083 tid 即主题ID, 还可以有 page, ppp (pageSize) , 这名字取的好蛋疼啊.

登陆:  POST /api/mobile/index.php?mobile=no&version=1&module=login&loginsubmit=yes&loginfield=auto&submodule=checkpost
password=密码&username=用户名&formhash=&answer=安全问题答案&questionid=5
 
这个登陆一直没有成功过!不知道是啥原因..所以和登陆相关的东西都没办法做下去...
 

项目结构

基于

1, Xamarin.Form (以下简称 XF)

2, Caliburn.Micro (以下简称 CM)

Discuz 是主项目, 那个 Droid, WinPhone 是用于编译生成app 的.

ViewModels / Views 是 CM 的默认约定方式, 字面意思大家都理解.

Discuz.Api 的入口是 ApiClient, 关键是 Methods 目录下面的对 api 方法的封装.

具体用法可以参考 Test 项目.

讲解:

App.xaml

写过 WPF 的, 都知道该文件的重要性.

但是在 XF 中, 新建项目中,没有该文件, 需要手动添加一个, 然后在 app.cs 的构造函数中添加:

 public App(SimpleContainer container) {
        this.InitializeComponent();

InitializeComponent 会在你添加 App.xaml 之后, 自动生成, 在它里面会去加载 app.xaml

Caliburn.Micro

之前发过一篇, 不在赘述:

Xamarin 的 MVVM 之 Caliburn.Micro

TabbedPage 数据源绑定

之前写的项目没用 MVVM, 直接这样写:

     public partial class BusPage : TabbedPage {

         public BusPage() {
             //InitializeComponent();
             //this.ItemsSource = this.Pages;

             this.Title = "LBC 业务通";

             this.BackgroundColor = Color.White;//在XAML中设置 Background 不起作用。

             this.Children.Add(new OrderListPage());
             this.Children.Add(new CustomerListPage());
             this.Children.Add(new TemplatesListPage());
             this.Children.Add(new SettingPage());
         }
     }
 <?xml version="1.0" encoding="utf-8" ?>
 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="LBC.Mobi.Pages.BusPage"
             Padding="10"
             Title="LBC"
             >

   <TabbedPage.ItemTemplate>
     <DataTemplate>
       <ContentPage
           Title="{Binding Title}" Content="{Binding Content}"
           />
     </DataTemplate>
   </TabbedPage.ItemTemplate>
 </TabbedPage>

即: 根本就没有用到 ItemsSource, 直接添加到 Children 中的.

这样是很省事, 但是在 MVVM 中, 行不通了, 因为 Children 没有对应的依赖属性, 无法通过 MVVM 绑定.

为解决这个问题, 先扩展 ContentPage

     public class ViewLocatorPage : ContentPage {

         public static readonly BindableProperty VMProperty = BindableProperty.Create<ViewLocatorPage, Screen>(p => p.VM, null, propertyChanged: VMChanged);

         public Screen VM {
             get {
                 return (Screen)this.GetValue(VMProperty);
             }
             set {
                 this.SetValue(VMProperty, value);
             }
         }

         private static void VMChanged(BindableObject bindable, object oldValue, object newValue) {
             var vm = (Screen)newValue;
             //var view = vm.GetView();
             var vmView = ViewLocator.LocateForModel(vm, null, null);
             if (vmView == null)
                 throw new Exception("没有找到视图");
             ViewModelBinder.Bind(vm, vmView, null);

             var activator = vm as IActivate;
             if (activator != null)
                 activator.Activate();

             var page = (ViewLocatorPage)bindable;
             if (null != (ContentPage)vmView) {
                 var vp = (ContentPage)vmView;
                 page.Content = vp.Content;
                 if (vp.ToolbarItems != null)
                     foreach (var t in vp.ToolbarItems)
                         page.ToolbarItems.Add(t);
             } else if (null != (Xamarin.Forms.View)vmView) {
                 page.Content = (Xamarin.Forms.View)vmView;
             }
         }

     }

VM做为依赖属性 (VMProperty), 在变化的时候会调用 VMChanged 方法.

在这个方法中, 会跟据 VM 去查询对应的视图, ViewLocator.LocateForModel(...)

将视图和模型绑定, ViewModelBinder.Bind(...)

并激活, activator.Activate()

然后做为 page 的 content 呈现在 TabbedPage 中. page.Content = vp.Content

需要注意的是 ViewLocator / ViewModelBinder 是 CM 中提供的, 所以用其它 MVVM 框架的, 请换成对应的 API 方法.

这样一来, 数据绑定就很简单了:

 <?xml version="1.0" encoding="utf-8" ?>
 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Discuz.Views.TabView"
             xmlns:cal="clr-namespace:Caliburn.Micro.Xamarin.Forms;assembly=Caliburn.Micro.Platform.Xamarin.Forms"
             xmlns:local="clr-namespace:Discuz;assembly=Discuz"
             ItemsSource="{Binding Datas}"
             Title="蓝色理想"
             BackgroundColor="#1e5263"
             >

   <TabbedPage.ItemTemplate>
     <DataTemplate>
       <local:ViewLocatorPage Title="{Binding DisplayName}" VM="{Binding}" />
     </DataTemplate>
   </TabbedPage.ItemTemplate>

 </TabbedPage>

Event & Command

WPF 中的 CM , 会跟据控件的类型自动选择默认的事件, 比如

<Button x:Name="Show" ... />

会在 Click 的时候, 自动去调用 VM中的 Show 方法,

或者

 <Button cal:Message.Attach="Show" />
 <xxx cal:Message.Attach="[Event click] = [Action Show()] ; [Event XXX] = [Action XXX()]" />

这样写多多少有点蛋疼.

在 CM For XF中, 无法通过 x:Name 获取到对应的控件, 所以第一种写法不能用, 第二种正如我说的, 蛋疼...

还好, XF 除了提供事件之外, 还会提供相应的 Command !

     <ListView.Footer>
       <StackLayout Padding="10">
         <Button Text="加载更多" Command="{Binding LoadMoreCmd}"
                 BindingContext="{Binding Source={x:Reference root}, Path=BindingContext}"
                 />
       </StackLayout>
     </ListView.Footer>

ContentControl 的等价物 ContentView

WPF 中有 ContentControl, 它在 MVVM 中的意义重大,  使你不用关心它如何展示出来, 只用给它一个 ViewModel, 已达到功能分解的目的.

在 XF 中,没有 ContentControl, 但是有 ContentView , 一般自定义用户控件都是从它继承.

有了 ContentView , 就可以把臃肿的主页面分解成不同的子模型与视图了.

     <ListView.ItemTemplate>
       <DataTemplate>
         <ViewCell>
           <ViewCell.View>
             <StackLayout Padding="5,1">
               <ctrls:Border Style="{StaticResource BlockBorder}">
                 <ContentView cal:View.Model="{Binding }" />
               </ctrls:Border>
             </StackLayout>
           </ViewCell.View>
         </ViewCell>
       </DataTemplate>
     </ListView.ItemTemplate>

手势 GestureRecognizers

在 ListView 中,  可以通过 ItemTapped 来判断哪一行数据被点击.

用 ContentView 进行分解之后, 我想把 tap 事件也给分解到具体的子模型中, 这样可以更多的自主选择, 但是在子模型中怎么响应 ItemTapped 呢? 显然是不可能的!

XF 提供的手势功能可以很好的解决这个问题:

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="Discuz.Views.ForumDetailView"
              xmlns:ctrls="clr-namespace:Discuz.Controls;assembly=Discuz"
              xmlns:cal="clr-namespace:Caliburn.Micro.Xamarin.Forms;assembly=Caliburn.Micro.Platform.Xamarin.Forms"
               >

   <ContentView.GestureRecognizers>
     <TapGestureRecognizer Command="{Binding TapCommand}" />
   </ContentView.GestureRecognizers>

意思就是说在这个ContentView 上 tap 的时候, 响应 TapCommand , 它和在 ListView 上响应 ItemTapped 是一样的效果!

消息传输中心 MessagingCenter

正如字面意思,它是 XF 中, 用来 在 不同的页面 之间 传输消息的, 是对观察者模式的封装.

消息传输, 需要两个元素, 消息标识符 和 消息内容

同时还要有消息的接收方法和发送方.

发送方:

 public static void Send<TSender>(TSender sender, string message) where TSender : class;
 public static void Send<TSender, TArgs>(TSender sender, string message, TArgs args) where TSender : class;

参数 message 其实是消息的标识符, 不要被字面意思给忽悠了, args 才是真正的消息内容.

 MessagingCenter.Send(this, "AddFavorite", new Tuple<int, string>(this.Data.ID, this.Data.Name));

接收方:

 public static void Subscribe<TSender, TArgs>(object subscriber, string message, Action<TSender, TArgs> callback, TSender source = null) where TSender : class;
 public static void Subscribe<TSender>(object subscriber, string message, Action<TSender> callback, TSender source = null) where TSender : class;

同样, message 指的是消息标识符, args 才是真正的消息内容.

 MessagingCenter.Subscribe<ForumDetailViewModel, Tuple<int, string>>(this, "AddFavorite", (sender, arg) => {
    if (!this.Favorites.ContainsKey(arg.Item1)) {
        this.ShowFavorite(arg.Item1, arg.Item2);
        this.AddToFavorite(arg.Item1, arg.Item2);
    }
 });

这样一来, 就可以在两个不同的页面之前传递消息了, 只不过有一点缺点: 发送方不能得到消息的反馈.

拖拽排序

很遗憾, XF 的 ListView 没有相关的事件..

要完成这个功能, 要自己实现 Render, 找了几个拖拽的示例, 都不是我心中的理想状态, 暂时搁浅.

Android 样式生成器:

http://jgilfelt.github.io/android-actionbarstylegenerator/

-------------

OK 完

Xamarin.Form 实例: Discuz BBS 客户端 源码分享的更多相关文章

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

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

  2. 业务类接口在TCP,HTTP,BLL模式下的实例 设计模式混搭 附源码一份

    业务类接口在TCP,HTTP,BLL模式下的实例 设计模式混搭 附源码一份 WinForm酒店管理软件--框架这篇随笔可以说是我写的最被大家争议的随笔,一度是支持和反对是一样的多.大家对我做的这个行业 ...

  3. FileZilla客户端源码解析

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

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

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

  5. Plupload上传实例《模仿微云上传实例》,带源码

    Plupload上传实例<模仿微云上传实例>,带源码,作者:鱼塘总裁 如有疑问,加群交流:646104701 一.实例截图 1.上传过程 2.上传成功 3.上传失败 4.最小化 二.所需文 ...

  6. Eureka 系列(04)客户端源码分析

    Eureka 系列(04)客户端源码分析 [TOC] 0. Spring Cloud 系列目录 - Eureka 篇 在上一篇 Eureka 系列(01)最简使用姿态 中对 Eureka 的简单用法做 ...

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

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

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

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

  9. 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了

    控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?

随机推荐

  1. JavaScript Patterns 4.2 Callback Pattern

    function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ...

  2. Js中获取对象的所有key值

    假如现在有一个对象 var obj = { A:2 ,B:"Ray" ,C:true ,D:function(){} } 如果想遍历对象obj中的所有键值,一般是用以下方式 for ...

  3. CentOS下搭建SVN服务器

    1.安装SVN SVN数据存储有两种方式,BDB(事务安全表类型)和FSFS(一种不需要数据库的存储系统),为了避免在服务器连接中断时锁住数据,FSFS是一种更安全也更多人使用的方式.SVN的运行方式 ...

  4. putExtra方法

    [开篇骂几句:fuck]1.扯淡intent.putExtra()怎么使用?2.胡说intent.putExtra(); [扯淡:其实你在问它怎么用的时候,你要明白,你知道不知道这是个什么东东,有必要 ...

  5. hdu 2196 Computer(树形DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. Python中的库使用之一 PIL

    先上代码:本文主要工给自己参考,在需要的时候直接搜索查找就行了,不想看没有实际运行例子的文档,当参考完这部分还哦未能解决问题在参考PIL的相关文档! Skip to content This repo ...

  7. OO的设计原则

    今天同事和我们一起讨论分享了OO的设计原则,讨论使人明晰,有人一起讨论学习是一件幸福的事情. 1.开闭原则 对功能的扩展是开放的,对修改是闭合的. 可以应用于类的设计,框架的设计等. 为什么?开闭原则 ...

  8. hiveserver2 with kerberos authentication

    Kerberos协议: Kerberos协议主要用于计算机网络的身份鉴别(Authentication), 其特点是用户只需输入一次身份验证信息就可以凭借此验证获得的票据(ticket-grantin ...

  9. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  10. java9-5 修饰符

    1. 修饰符: 权限修饰符:private,默认的,protected,public 状态修饰符:static,final 抽象修饰符:abstract 类: 权限修饰符:默认修饰符,public 状 ...