Xamarin.Form 实例: Discuz BBS 客户端 源码分享

时间:2023-03-09 06:25:43
Xamarin.Form 实例: Discuz BBS 客户端 源码分享

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

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

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

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

源码:

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

上几张图:

Andorid

Xamarin.Form 实例: Discuz BBS 客户端 源码分享Xamarin.Form 实例: Discuz BBS 客户端 源码分享Xamarin.Form 实例: Discuz BBS 客户端 源码分享Xamarin.Form 实例: Discuz BBS 客户端 源码分享

WP

Xamarin.Form 实例: Discuz BBS 客户端 源码分享Xamarin.Form 实例: Discuz BBS 客户端 源码分享Xamarin.Form 实例: Discuz BBS 客户端 源码分享

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
这个登陆一直没有成功过!不知道是啥原因..所以和登陆相关的东西都没办法做下去...

项目结构

Xamarin.Form 实例: Discuz BBS 客户端 源码分享

基于

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 完