【问题标题】:Bind object data to Int xamarin forms将对象数据绑定到 Int xamarin 表单
【发布时间】:2015-06-06 06:01:19
【问题描述】:

我有一个创建多个部门对象的 MasterDetailPage。我想获取当前部门编号,以便稍后在我的程序中使用它对列表进行排序。我该怎么做呢?我试过将它绑定到一个标签上,然后从中获取数据(我知道这很老套),但这是我唯一能想到的。

Department[] departments = {
            new Department ("D", 1), 
            new Department ("De", 7),
            new Department ("G", 4),
            new Department ("M", 9),
            new Department ("Pr", 167),
            new Department ("Fr", 187),
            new Department ("H", 169),
            new Department ("B", 11),
            new Department ("S", 399),
            new Department ("N", 407),
            new Department ("O", 201),
            new Department ("U", 023)
        };

        ListView listView = new ListView {
            ItemsSource = departments
        };

        this.Master = new ContentPage {
            Title = "Departments",       // Title required!
            Content = new StackLayout {
                Children = {
                    header, 
                    listView
                }
            }
        };

        DetailPage2 detailPage = new DetailPage2 ();
        this.Detail = detailPage;  //detail page is where I want to                  use deptNum for sorting

        listView.ItemSelected += (sender, args) => {
            // Set the BindingContext of the detail page.
            this.Detail.BindingContext = args.SelectedItem;

            // Show the detail page.
            this.IsPresented = false;

        };

        // Initialize the ListView selection.
        listView.SelectedItem = departments [0];

    }
    }
}

然后在我的详细信息页面中,我希望能够将 departmentNumber 拉出来并将其用作 int

using System;
using Xamarin.Forms;

namespace irisxamarin
{
  public class Department :BindableObject
   {
    public Department (string name, int deptNumber)
    {
        this.Name = name;
        this.DeptNum = deptNumber;


    }

    public string Name { private set; get; }

    public int DeptNum { private set; get; }

    public override string ToString ()
    {
        return Name;
    }
 }
}

这是详细页面中的一些逻辑。这是我想获取当前 deptNum 的地方。

namespace irisxamarin
{
public class DetailPage2 : ContentPage
{
    public DetailPage2 ()
    {

        Request request = new Request ();

        Button settingsButton = new Button {
            Text = "Settings",
            TextColor = Color.Gray
        };

 //......................
//code above and below


ListView itemsList = new ListView { 
            ItemsSource = request.GetList (deptNum) //USE INT HERE

        };

        itemsList.ItemSelected += (sender, args) => {

            this.BindingContext = args.SelectedItem;
        };

        itemLabel.SetBinding (Label.TextProperty, "DeptNum");
    //DeptNum is the data I want but not in a label, just the int val         

        var listFrame = new Frame {
            Content = itemsList,
            OutlineColor = Color.Silver,

        };

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    每个页面只是一个 C# 类。你可以像对待任何类一样向它传递一个值——通常最简单的方法是

    1. 在构造函数中传递值
    2. 或者如果页面已经存在,创建公共属性并通过setter设置值

    如果您想全局设置一个值以在整个应用中使用,您可以创建一个随处可用的静态类并在该类中设置状态值。

    【讨论】:

    • 谢谢,我会试试的。但是,如果用户在应用程序期间更改部门,我希望部门编号具有反应性。
    • 使用 INotifyPropertyChanged 允许从属页面收到属性值更改的通知。
    • 酷,我正在研究这个选项。我仍然不明白如何在我的详细信息页面更改后获取当前部门编号。我可以把它拉出来保存到一个int吗?我不想在标签或其他对象中使用数据,我只需要将整数传递给可以为我排序列表的类。我确实认为这是正确的方法,我只是 xamarin 的新手,我不太了解事件处理程序的工作原理。
    • 我尝试通过构造函数将值传递给详细信息页面,但没有骰子,程序崩溃。
    • 我通过传递 int 值/deptnum 使其工作,但由于我将值传递给页面,因此应用程序不会对部门更改做出反应。设置的绑定上下文与标签等完美配合,但是一旦分配标签,如何从标签中提取数据?
    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2019-03-20
    • 2016-09-08
    • 1970-01-01
    • 2021-10-02
    相关资源
    最近更新 更多