Xamarin.Forms學(xué)習(xí)歷程(六)——數(shù)據(jù)綁定

久等了凹蜈,今天講Xamarin.Forms里的核心——數(shù)據(jù)綁定,是將數(shù)據(jù)綁定到對應(yīng)的view上泼橘,實現(xiàn)cs文件與XML文件的數(shù)據(jù)流通饲宿。今天普通的數(shù)據(jù)綁定我不做講解厦酬,今天只講個最復(fù)雜的,就是將接口解析的數(shù)據(jù)為模型數(shù)組瘫想,再將模型數(shù)組映射到對應(yīng)的ListView上仗阅。下面就來逐一講解。

1国夜、新建數(shù)據(jù)模型##

根據(jù)接口的數(shù)據(jù)返回值新建對應(yīng)的模型减噪。

    public class JFCustomers
    {
        [JsonProperty("list")]
        public IEnumerable<JFCustomer> Items { get; set; }
    }

    public class JFCustomer
    {
        [JsonProperty("address")]
        public string address { get; set; }

        [JsonProperty("customerType")]
        public string customerType { get; set; }

        [JsonProperty("customerTypeView")]
        public string customerTypeView { get; set; }

        [JsonProperty("id")]
        public string id { get; set; }

        [JsonProperty("investProductCount")]
        public string investProductCount { get; set; }

        [JsonProperty("investShareAmountTotal")]
        public string investShareAmountTotal { get; set; }

        [JsonProperty("investShareTotal")]
        public string investShareTotal { get; set; }

        [JsonProperty("mobile")]
        public string mobile { get; set; }

        [JsonProperty("name")]
        public string name { get; set; }

    }

[JsonProperty("XXXX")]是為了解析數(shù)據(jù)能映射到模型對應(yīng)的屬性上去,所以盡量讓模型屬性名與Json里數(shù)據(jù)值對應(yīng)的“key”名字保持一致车吹。

2筹裕、調(diào)接口,獲取數(shù)據(jù)##

這里網(wǎng)絡(luò)使用的是httpclient窄驹,請不要使用request朝卒,因為request里有些方法已經(jīng)不再使用。

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        int currentPage = 1;
        String sequenceName = "investShareAmountTotal";
        String uri = JFGobalData.baseUrl + "/api/fund/clients?keyWords=" + searchBar.Text + "&page=" + currentPage + "&pageSize=10&sort=" + sequenceName;
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("X-Auth-Token",urlToken);
        var task = await client.GetAsync(uri);
        var jsonString = await task.Content.ReadAsStringAsync();
        var converted = JsonConvert.DeserializeObject<JFCustomers>(jsonString);
        customerListView.ItemsSource = converted.Items;
    }

這里是在界面剛開始加載時調(diào)用接口乐埠,我們只需要看最后兩行代碼

var converted = JsonConvert.DeserializeObject<JFCustomers>(jsonString);
customerListView.ItemsSource = converted.Items;

第一行是將json解析成模型數(shù)組抗斤,第二行是將模型數(shù)組給ListView的ItemsSource囚企,這樣我們就完成了宏觀的數(shù)據(jù)綁定,我們還需要將模型里的各個數(shù)據(jù)放到對應(yīng)的view里展示瑞眼。

3龙宏、模型屬性分發(fā)給View##

這里才是最后的重中之重,我們還是看代碼來分析

<ListView x:Name = "customerListView"
                    SeparatorVisibility = "None"
                        ItemSelected = "customerListViewItemSelected">
                <ListView.RowHeight>
                     <OnPlatform x:TypeArguments="x:Int32"
                                iOS="80"
                                Android="80"
                                WinPhone="90" />
                </ListView.RowHeight>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ContentView>
                                <Frame OutlineColor = "Accent"
                                        Padding = "5">
                                    <StackLayout Orientation = "Vertical">
                                            <Label Text = "{Binding name}"
                                                FontSize = "18"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                        <StackLayout Orientation = "Horizontal">
                                            <Label Text = "客戶類別"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "{Binding customerType}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "投資產(chǎn)品(個)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>
                                            <Label Text = "{Binding investProductCount}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>
                                        </StackLayout>

                                        <StackLayout Orientation = "Horizontal">
                                            <Label Text = "投資份額(份)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "{Binding investShareTotal}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "投資金額(元)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>
                                            <Label Text = "{Binding investShareAmountTotal}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>
                                        </StackLayout>
                                    </StackLayout>
                                </Frame>
                            </ContentView>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

其實這里真沒有什么可說而
我只說一點

ItemSelected = "customerListViewItemSelected"

這個用來給每一行元素提供點擊事件伤疙,或許你會問烦衣,我怎么知道我點擊的是哪一行,下面就給你解答掩浙。

4、獲取點擊的模型##

void customerListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    JFCustomerDetailPage customerDetailPage = new JFCustomerDetailPage();
    customerDetailPage.customerModel = (JFCustomer)e.SelectedItem;
    this.Navigation.PushAsync(customerDetailPage);
}

這樣你就可以拿到你所點擊那一行秸歧。

結(jié)語#

本人才疏學(xué)淺厨姚,如有描述不對的地方望大神指正。還有Xamarin目前似乎不在維護键菱,請小心跳坑谬墙,個人認為拿來做小應(yīng)用是可以的,大型應(yīng)用最好還是尋找別的解決方案经备。謝謝拭抬。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市侵蒙,隨后出現(xiàn)的幾起案子造虎,更是在濱河造成了極大的恐慌,老刑警劉巖纷闺,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件算凿,死亡現(xiàn)場離奇詭異,居然都是意外死亡犁功,警方通過查閱死者的電腦和手機氓轰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浸卦,“玉大人署鸡,你說我怎么就攤上這事∠尴樱” “怎么了靴庆?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長萤皂。 經(jīng)常有香客問我撒穷,道長,這世上最難降的妖魔是什么裆熙? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任端礼,我火速辦了婚禮禽笑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蛤奥。我一直安慰自己佳镜,他們只是感情好,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布凡桥。 她就那樣靜靜地躺著蟀伸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缅刽。 梳的紋絲不亂的頭發(fā)上啊掏,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天,我揣著相機與錄音衰猛,去河邊找鬼迟蜜。 笑死,一個胖子當著我的面吹牛啡省,可吹牛的內(nèi)容都是我干的娜睛。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼卦睹,長吁一口氣:“原來是場噩夢啊……” “哼畦戒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起结序,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤障斋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后笼痹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體配喳,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年凳干,在試婚紗的時候發(fā)現(xiàn)自己被綠了晴裹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡救赐,死狀恐怖涧团,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情经磅,我是刑警寧澤泌绣,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站预厌,受9級特大地震影響阿迈,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜轧叽,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一苗沧、第九天 我趴在偏房一處隱蔽的房頂上張望刊棕。 院中可真熱鬧,春花似錦待逞、人聲如沸甥角。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嗤无。三九已至,卻和暖如春怜庸,著一層夾襖步出監(jiān)牢的瞬間当犯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工割疾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留灶壶,地道東北人。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓杈曲,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胸懈。 傳聞我的和親對象是個殘疾皇子担扑,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容