在TableView中绑定TextCell

时间:2017-06-27 13:31:22

标签: c# ios xamarin.forms tableview

我已将详细信息传递给下一个Form stringsintegers并将其存储在Class's构造函数中,现在我如何绑定构造函数中的值(参数化值)到TextCell内的TableView

最初,这就是我发布另一种形式的方式。

await Navigation.PushAsync(new LoginSalesDetails(Usnmae, AgentNumber, AgentName, AgentPhone, AgentEmail, Agentrating)); 

这是我的班级代码:

public LoginSalesDetails(string Username, string AgentNo, string AgentName, string AgentPhone, string AgentEmail, int AgentRating)
        {
            InitializeComponent();



            accountname_label.Text = AgentName;
            agent_no_label.Text = AgentNo;
            rating_label.Text = "" + AgentRating;
            mobileno_label.Text = AgentPhone;
            email_label.Text = AgentEmail;

            Name_A(AgentName);


        //  DisplayAlert("Alert ", ""+AgentName, "OK");
        }




        public string Name_A(string a) 
        {


            return a;
        }

然后,这是由TableViewTexCells组成的XAML代码。

<TableView Intent="Form">
            <TableView.Root>
                <TableSection Title="Agent Sales Details">
                    <TextCell Text="Agent Number" Detail="{Binding Name_A}" />
                    <TextCell Text="Agent Rating " Detail="4" />
                    <TextCell Text="Agent Phone " Detail="0474774747475" />
                    <TextCell Text="Agent Email " Detail="huxy@gmail.com" />
                </TableSection>
            </TableView.Root>
        </TableView>

修改

我的Class.cs代码:

public partial class LoginSalesDetails : ContentPage
    {
        public string Name_A { get; set; }
        public string Email_A { get; set;}
        public string Phone_A { get; set;}
        public string Rating_A { get; set;}
        public string No_A { get; set;}

        public LoginSalesDetails(string Username, string AgentNo, string AgentName, string AgentPhone, string AgentEmail, int AgentRating)
        {
            InitializeComponent();



            accountname_label.Text = AgentName;
            agent_no_label.Text = AgentNo;
            rating_label.Text = "" + AgentRating;
            mobileno_label.Text = AgentPhone;
            email_label.Text = AgentEmail;

            Name_A = AgentName;
            Email_A = AgentEmail;
            Phone_A = AgentPhone;
            Rating_A = ""+ AgentRating;



            //  DisplayAlert("Alert ", ""+AgentName, "OK");
        }


    }

我的XAML代码:

<TableView Intent="Form">
            <TableView.Root>
                <TableSection Title="Agent Sales Details">
                    <TextCell Text="Agent Number" Detail="{Binding Name_A}" />
                    <TextCell Text="Agent Rating " Detail="{Binding Email_A}" />
                    <TextCell Text="Agent Phone " Detail="{Binding Phone_A}" />
                    <TextCell Text="Agent Email " Detail="huxy@gmail.com" />
                </TableSection>
            </TableView.Root>
        </TableView>

2 个答案:

答案 0 :(得分:1)

您现在正在尝试绑定到方法,这是不可能的。这应该始终对公共财产进行。例如,像这样更改代码:

Name_A

请注意,我删除了该方法,并将BindingContext添加为属性。

此外,如果您还没有这样做,则需要将包含TableView的页面的LoginSalesDetails设置为{{1}}对象的实例。

答案 1 :(得分:0)

好吧,我有另一个选择,给每个TextCell一个id,然后只需简单地给id分配字符串数据:

如下所示

XAML:

<TableView Intent="Form">
            <TableView.Root>
                <TableSection Title="Agent Sales Details">
                    <TextCell Text="Agent Number"  x:Name="agentNumber_txtcell" />
                    <TextCell Text="Agent Rating "  x:Name="agentRating_txtcell" />
                    <TextCell Text="Agent Phone "  x:Name="agentPhone_txtcell" />
                    <TextCell Text="Agent Email "  x:Name="agentEmail_txtcell" />
                </TableSection>
            </TableView.Root>
        </TableView>

然后只需在您的C#类中将ID(x:Name)分配给字符串:

public LoginSalesDetails(string Username, string AgentNo, string AgentName, string AgentPhone, string AgentEmail, int AgentRating)
{
    InitializeComponent();



    agentNumber_txtcell.Detail= AgentNo;
    agentRating_txtcell.Detail= ""+AgentRating;
    agentPhone_txtcell.Detail= "" + AgentPhone;
    agentEmail_txtcell.Detail= AgentEmail;





//  DisplayAlert("Alert ", ""+AgentName, "OK");
}

快乐编码