Monotouch.Dialog 从数据库生成并保留值 [英] Monotouch.Dialog Generate from db and retain values

查看:23
本文介绍了Monotouch.Dialog 从数据库生成并保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置视图,我正在使用 MT.D 构建我的 UI.我只是让它从数据库中读取元素以填充部分中的元素.

I'm have a settings view where I'm using MT.D to build out my UI. I just got it to read elements from a database to populate the elements in a section.

我不知道如何访问每个元素的属性或值.我想根据它在数据库中的值为每个项目设置不同背景颜色的元素.我还希望能够获得选定的值,以便我可以在数据库中更新它.这是使用 MT.D 执行 UI 内容的代码的呈现.我可以让值显示出来并像他们应该的那样滑出......但是,为它们设计样式或添加代表来处理我丢失的点击.

What I don't know how to do is access each elements properties or values. I want to style the element with a different background color for each item based on it's value in the database. I also want to be able to get the selected value so that I can update it in the db. Here's the rendering of the code that does the UI stuff with MT.D. I can get the values to show up and slide out like their supposed to... but, styling or adding delegates to them to handle clicks I'm lost.

List<StyledStringElement> clientTypes = SettingsController.GetClientTypes ();

        public SettingsiPhoneView () : base (new RootElement("Home"), true)
        {
            Root = new RootElement("Settings") {
                new Section ("Types") {
                    new RootElement ("Types") {
                        new Section ("Client Types") {
                             from ct in clientTypes
                                select (Element) ct
                        }
                    },
                    new StringElement ("Other Types")
                }

推荐答案

以下是我的处理方式.基本上你必须在 foreach 循环中创建元素,然后用你想要在那里做的任何事情来填充委托.像这样:

Here's how I handled it below. Basically you have to create the element in a foreach loop and then populate the delegate with whatever you want to do there. Like so:

public static List<StyledStringElement> GetClientTypesAsElement ()
        {
            List<ClientType> clientTypes = new List<ClientType> ();
            List<StyledStringElement> ctStringElements = new List<StyledStringElement> ();

            using (var db = new SQLite.SQLiteConnection(Database.db)) {
                var query = db.Table<ClientType> ().Where (ct => ct.IsActive == true && ct.Description != "Default");

                foreach (ClientType ct in query)
                    clientTypes.Add (ct);
            }

            foreach (ClientType ct in clientTypes) {
                // Build RGB values from the hex stored in the db (Hex example : #0E40BF)
                UIColor bgColor = UIColor.Clear.FromHexString(ct.Color, 1.0f);
                var localRef = ct;
                StyledStringElement element = new StyledStringElement(ct.Type, delegate {
                    ClientTypeView.EditClientTypeView(localRef.Type, localRef.ClientTypeId);
                });

                element.BackgroundColor = bgColor;
                ctStringElements.Add (element);
            }

            return ctStringElements;
        }

这篇关于Monotouch.Dialog 从数据库生成并保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆