XAML in Xamarin.Forms 基礎篇 電子書

XAML in Xamarin.Forms 基礎篇 電子書
XAML in Xamarin.Forms 基礎篇 電子書

Xamarin.Forms 快速入門 電子書

Xamarin.Forms 快速入門 電子書
Xamarin.Forms 快速入門 電子書

2016/11/12

Xamarin.Forms 動態產生檢視項目的 WrapView

在現在的 Xamarin.Forms 內建的控制項中,並沒有辦法針對動態產生的控制項,使用 XAML & ViewModel 來實現這樣的效果,因此,在這裡,參考了 XLabs 內提供的 WrapLayout,將其相關原始碼抽取出來,就可以實踐這樣的效果。
範例專案原始碼
XFWrapView
在這個範例中,第1個 Row 中,將會顯示 View Model 內的35五筆資料,並且使用 DataTemplate 方式定義其要顯示的樣貌;不過,在這裡將會使用水平排列的方式來顯示。
在這個範例中,第2個 Row 中,將會顯示 View Model 內的35五筆資料,並且使用 DataTemplate 方式定義其要顯示的樣貌;不過,在這裡將會使用垂直排列的方式來顯示。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:local="clr-namespace:XFWrapLayout"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="XFWrapLayout.Views.MainPage"
             Title="MainPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <local:WrapView Grid.Row="0" ItemsSource="{Binding MyItems}" Orientation="Horizontal">
            <local:WrapView.ItemTemplate>
                <DataTemplate>
                    <StackLayout BackgroundColor="Blue" WidthRequest="40" HeightRequest="20" VerticalOptions="Fill" Padding="6, 6, 6, 6">
                        <Label Text="{Binding Name}" FontSize="Small" XAlign="Start" TextColor="White" VerticalOptions="EndAndExpand"/>
                    </StackLayout>
                </DataTemplate>
            </local:WrapView.ItemTemplate>
        </local:WrapView>
        <local:WrapView Grid.Row="1" ItemsSource="{Binding MyItems}" Orientation="Vertical">
            <local:WrapView.ItemTemplate>
                <DataTemplate>
                    <StackLayout BackgroundColor="Red" WidthRequest="40" HeightRequest="20" VerticalOptions="Fill" Padding="6, 6, 6, 6">
                        <Label Text="{Binding Name}" FontSize="Small" XAlign="Start" TextColor="White" VerticalOptions="EndAndExpand"/>
                    </StackLayout>
                </DataTemplate>
            </local:WrapView.ItemTemplate>
        </local:WrapView>
    </Grid>
</ContentPage>

2016/11/11

Xamarin.Forms App 使用 HocKeyApp 測試者端的安裝與設定

想要透過 HocKeyApp 進行測試的用戶端,可以參考這篇文章

了解更多關於 [Xamarin.Android] 的使用方式
了解更多關於 [Xamarin.iOS] 的使用方式
了解更多關於 [Xamarin.Forms] 的使用方式
了解更多關於 [Hello, Android:快速入門] 的使用方式
了解更多關於 [Hello, iOS – 快速入門] 的使用方式
了解更多關於 [Xamarin.Forms 快速入門] 的使用方式

打開測試通知郵件,並且安裝 HocKeyApp 用戶端程式與要測試程式

當您被選擇為App測試人員之後,您會收到一封郵件,此時,請透過您的手機,打開這封郵件。
HocKeyAppUser
在打開通知郵件之後,請捲動郵件到最下方,點選 Register your Android device 連結
HocKeyAppUser2
當網頁打開之後,請點選 Download HocKeyApp for Android 這個連結。此時,將會開始下載 HocKeyApp 用戶端 App。
HocKeyAppUser3
當下載完成後,請安裝這個 HocKeyApp 用戶端程式。
HocKeyAppUser4
在安裝完成這個 HocKeyApp 應用程式之後,請打開這個應用程式。此時,您將會看到如下圖畫面,若您尚未註冊 HocKeyApp 帳號,請點選下方的 Sign up here 連結,進行註冊一個 HocKeyApp 帳號。
若您已經擁有 HocKeyApp 帳號,請點選 SIGN IN 這個按鈕,進行登入 HocKeyApp。
HocKeyAppUser5
登入完成之後,參考下圖,點選 Authorize 按鈕
HocKeyAppUser6
完成之後,就會進入到 HocKeyApp 應用程式,而您也會看到您可以測試的應用程式,例如,在這裡準備要測試 多奇集團 行動儀表板 這隻應用程式,點選這個應用程式。
HocKeyAppUser7
在右下方有個下載按鈕,點選這個按鈕,就可以開始進行下載這個應用程式到您的手機上。
HocKeyAppUser8
下載完成後,請將這個應用程式安裝起來,您就可以開始進行測試工作了。
HocKeyAppUser9

應用程式閃退回報

當您在進行應用程式測試過程中,若有遇到閃退的情況,可以再度開啟您正在測試的應用程式,此時,會出現底下畫面,請點選 Send Report 按鈕,將剛剛測試過程中的閃退原因,傳送到後端給開發者,這樣,就可以針對您遇到的問題進行程式修正了。
發現到有異常例外產生.png

有新版本的應用程式要更新

當開發者根據您的需求進行程式修正後,就會發布新的版本後 HocKeyApp 後台上;若您重新打開要測試的應用程式,應該會看到如下圖畫面,請點選 Show 按鈕。
UpdateAvailable.png
當看到下圖畫面,您可以點選 Update 按鈕,進行升級正在測試的應用程式。
AppUpdateInfo.png

2016/11/10

Xamarin.Forms 使用具有 GridView 特性的 ListView

傳統上 Xamarin.Forms 並沒有提供類似 GridView 這樣的控制項,也就是可以使用多 Column 的方式來顯示集合資料,但是其他的行為與 ListView 一樣。
在這份筆記中,將會測試與使用 DLToolkit.Forms.Controls.FlowListView 這個 NuGet 套件,就可以達成使用 GridView 控制項的效果。
要使用這個功能,您需要在方案中加入這個 DLToolkit.Forms.Controls.FlowListView NuGet 套件到所有的專案內;下面螢幕截圖為實際執行結果。
XFHListView
範例專案

需要安裝 NuGet 套件

要完成這個筆記中的效果,你需要在方案中安裝底下的 NuGet 套件
Xamarin.FFImageLoading.Forms
DLToolkit.Forms.Controls.FlowListView

範例說明

首先,需要定義這個集合的資料模型。
在這個,建立了一個 SampleItems1 資料模型,裡面有 Title / Color / ImageUrl 這三個屬性。
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

namespace XFHListView.Models
{
    public class SampleItems1 : BindableBase
    {

        #region Title
        private string _Title;
        /// <summary>
        /// Title
        /// </summary>
        public string Title
        {
            get { return this._Title; }
            set { this.SetProperty(ref this._Title, value); }
        }
        #endregion


        #region Color
        private Color _Color;
        /// <summary>
        /// Color
        /// </summary>
        public Color Color
        {
            get { return this._Color; }
            set { this.SetProperty(ref this._Color, value); }
        }
        #endregion


        #region ImageUrl
        private string _ImageUrl;
        /// <summary>
        /// ShowImage
        /// </summary>
        public string ImageUrl
        {
            get { return this._ImageUrl; }
            set { this.SetProperty(ref this._ImageUrl, value); }
        }
        #endregion

        public SampleItems1()
        {

        }
    }
}
接著需要在 ViewModel 內定義所有資料綁定的集合資料,並且要進行集合資料的初始化,可以參考底下程式碼。
在這個 ViewModel 內,集合資料將會使用 ObservableCollection<SampleItems1> 型別,並且儲存在 MyDatas 這個物件內。
在建構式內,呼叫了 Init() 方法,針對 MyDatas 進行產生實際物件並且進行資料初始化。
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using XFHListView.Models;

namespace XFHListView.ViewModels
{
    public class SimpleSamplePageViewModel : BindableBase
    {
        #region MyDatas
        private ObservableCollection<SampleItems1> _MyDatas;
        /// <summary>
        /// MyDatas
        /// </summary>
        public ObservableCollection<SampleItems1> MyDatas
        {
            get { return _MyDatas; }
            set { SetProperty(ref _MyDatas, value); }
        }
        #endregion

        #region 使用者點選項目
        private SampleItems1 _使用者點選項目;
        /// <summary>
        /// 使用者點選項目
        /// </summary>
        public SampleItems1 使用者點選項目
        {
            get { return this._使用者點選項目; }
            set { this.SetProperty(ref this._使用者點選項目, value); }
        }
        #endregion

        public DelegateCommand 使用者點選Command { get; set; }

        public readonly IPageDialogService _dialogService;
        public SimpleSamplePageViewModel(IPageDialogService dialogService)
        {

            _dialogService = dialogService;
            Init();

            使用者點選Command = new DelegateCommand(() =>
              {
                  _dialogService.DisplayAlertAsync("訊息", $"你選取的是 {使用者點選項目.Title}", "OK");
              });
        }

        public void Init()
        {
            MyDatas = new ObservableCollection<SampleItems1>();

            var howMany = new Random().Next(100, 500);

            for (int i = 0; i < howMany; i++)
            {
                MyDatas.Add(new SampleItems1() { Title = string.Format("項目 {0}", i) });
            }
        }
    }
}
有的 Model / ViewModel,接下來,就是要定義 View,可以參考底下 XAML 。
這個頁面 XAML 內容相當的簡單,根項目為 ContentPage,並且其兒子為 FlowListView;不過,由於這個 FlowListView 控制項並不是 Xamarin.Forms 預設命名空間所提供的,因此,需要在跟項目內另外定義一個命名空間 xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView",這樣,就可以使用前置詞 flv 來引用 FlowListView 這個控制項。
FlowListView 這個控制項內,使用了 FlowColumnCount="3" 宣告了這個 GridView 效果共有三個 Column,並且每筆紀錄並沒有使用分隔線,這是使用了這個宣告:SeparatorVisibility="None"
另外,使用了 HasUnevenRows="False" 設定每筆紀錄都是等高;當使用者點選了清單內的某個項目,可以使用 FlowItemTappedCommand 來綁定 ViewModel 內的某個 DelegateCommand ,就可以在 ViewModel 內撰寫相關點擊後的處理邏輯;而使用者點選後的項目,也會透過 FlowLastTappedItem 這個屬性,將其綁定到 ViewModel 內的某個屬性,也就是說,當使用者點選某個項目,就可以透過該 ViewModel 內的屬性,取得使用者當時點選的項目內容;最後,該 FlowListView 要顯示的資料清單內容,將會透過 FlowItemsSource屬性來進行資料綁定。
最後,與 ListView 相同,每筆資料,可以透過 FlowListView.FlowColumnTemplate 來設定這些資料項目要使用何種格式來顯示。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="XFHListView.Views.SimpleSamplePage"
             xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
             Title="簡單範例">

    <flv:FlowListView
        FlowColumnCount="3"
        SeparatorVisibility="None"
        HasUnevenRows="False"
        FlowItemTappedCommand="{Binding 使用者點選Command}"
        FlowLastTappedItem="{Binding 使用者點選項目}"
        FlowItemsSource="{Binding MyDatas}" HeightRequest="100"
        >
        <flv:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <Label HorizontalOptions="Fill" VerticalOptions="Fill" 
                        XAlign="Center" YAlign="Center" Text="{Binding Title}"/>
            </DataTemplate>
        </flv:FlowListView.FlowColumnTemplate>

    </flv:FlowListView>
</ContentPage>

2016/11/09

Xamarin.Forms 旋轉木馬 Carousel 控制項效果

在 Xamarin.Forms 可以使用 旋轉木馬 Carousel 控制項在頁面上,如此,可以讓使用者使用手勢滑動選取想要的項目,如同下圖動畫效果。
Xamarin.Forms Carousel
範例專案原始碼
要能夠做到這樣的效果,需要使用到 CarouselView 這個第三方控制項
在方案中加入這個 NuGet 套件 CarouselView.FormsPlugin 到所有的專案內,
實際使用方是可以參考底下的 XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:controls="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="XFFlip.Views.MainPage"
             Title="MainPage">
    <StackLayout HorizontalOptions="Fill" VerticalOptions="Center">
        <Label Text="{Binding Title}" />
        <StackLayout
            HorizontalOptions="Center"
            Orientation="Horizontal">
            <!--https://msdn.microsoft.com/en-us/library/aa970677(v=vs.110).aspx-->
            <Label Text="&lt;" FontSize="24" VerticalOptions="Center" />
            <controls:CarouselViewControl 
            Orientation="Horizontal" 
            InterPageSpacing="10" Position="0" 
            ItemsSource="{Binding Zoos}" 
                Arrows="True"
            HeightRequest="100" WidthRequest="250"
            VerticalOptions="FillAndExpand" HorizontalOptions="Center">
                <controls:CarouselViewControl.ItemTemplate>
                    <DataTemplate>
                        <Grid
                        >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="30"/>
                            </Grid.RowDefinitions>
                            <Image Grid.RowSpan="2" Aspect="AspectFill" Source="{Binding ImageUrl}"/>
                            <Grid Grid.Row="1" BackgroundColor="#80000000" >
                                <Label 
                                TextColor="White" 
                                Text="{Binding Name}" 
                                FontSize="16" FontAttributes="Bold"
                                />
                            </Grid>
                        </Grid>

                    </DataTemplate>
                </controls:CarouselViewControl.ItemTemplate>
            </controls:CarouselViewControl>
            <Label Text="&gt;" FontSize="24" VerticalOptions="Center" />
        </StackLayout>
    </StackLayout>
</ContentPage>