通过按钮单击事件刷新另一个内容页面上的ListView

时间:2017-11-13 03:36:54

标签: xamarin

我很难刷新listView。我已尝试使用ObservableCollection而不是List来表示联系人,但它没有帮助。有人能指出我正确的方向吗?非常感谢任何帮助。

ContactListPage.cs文件(这是默认页面。添加联系人按钮加载NewContactPage。)

public class ContactListPage : ContentPage
{
    public ListView listView = new ListView();
    public List<Contact> contacts { get; set; }

    public ContactListPage()
    {
        Button button = new Button
        {
            Text = "Add Contact",
            Margin = new Thickness(0, 0, 10, 0)
        };
        button.Clicked += OnButtonClicked;

        StackLayout buttonLayout = new StackLayout { Orientation = StackOrientation.Horizontal, VerticalOptions = LayoutOptions.Center,
            Children =
            {
                new Label {Text = "Contact List", FontSize = 30, FontAttributes = FontAttributes.Bold | FontAttributes.Italic, Margin = new Thickness(10,0,0,0)},
                new Label {Text = "", FontSize = 30, FontAttributes = FontAttributes.Bold | FontAttributes.Italic, HorizontalOptions = LayoutOptions.CenterAndExpand},
                button
            }
        };
        contacts = new List<Contact>
        {
            // Add some contacts
            new Contact("Jason", "Bourne", "Family"),
            new Contact("Sunny", "An", "Family Dog"),
            new Contact("Jenny", "Bahn", "Family"),
            new Contact("Joshua", "Brown", "Work")
        };

        // Set up listview 
        listView.ItemsSource = contacts;
        listView.ItemTemplate = new DataTemplate(typeof(TextCell));
        listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FullName");
        listView.ItemTemplate.SetBinding(TextCell.DetailProperty, "ContactType");
        listView.HeightRequest = (40 * contacts.Count);

        // Add things to the stacklayout
        StackLayout layout = new StackLayout();
        layout.Children.Add(buttonLayout);
        layout.Children.Add(listView);

        Content = layout;

        void OnButtonClicked(object sender, EventArgs args)
        {
            Navigation.PushAsync(new NewContactPage());
        };
    }
}

Contact.cs文件

public class Contact
{
    // constructor
    public Contact(string firstName, string lastName, string contactType)
    {
        FirstName = firstName;
        LastName = lastName;
        ContactType = contactType;
    }

    // properties
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ContactType { get; set; }
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
        set
        {
            FullName = value;
        }
    }
}

NewContactPage.cs文件(保存按钮似乎正在工作。我已经使用Debug.WriteLine查看添加新联系人之前和之后的联系人列表。返回按钮假设刷新contactPage.listView.ItemSource with联系人的最新内容,但它没有。)

public NewContactPage()
    {
        ContactListPage contactPage = new ContactListPage();

        // Setup tableview
        EntryCell firstName = new EntryCell {Label = "First Name:", Keyboard = Keyboard.Default};
        EntryCell lastName = new EntryCell {Label = "Last Name:", Keyboard = Keyboard.Default};
        EntryCell contactType = new EntryCell {Label = "Contact Type:", Keyboard = Keyboard.Default};
        Label firstLabel = new Label { Text = "", FontSize = 10, FontAttributes = FontAttributes.Bold | FontAttributes.Italic, HorizontalOptions = LayoutOptions.CenterAndExpand};
        Label secondLabel = new Label {Text = "", FontSize = 10, FontAttributes = FontAttributes.Bold | FontAttributes.Italic, HorizontalOptions = LayoutOptions.CenterAndExpand};
        TableView tableView = new TableView {VerticalOptions = LayoutOptions.Start, Intent = TableIntent.Form,
            Root = new TableRoot("Table Title") {
                new TableSection ("Add a New Contact") {
                    firstName,
                    lastName,
                    contactType
                }
            }
        };

        // Setup buttons
        Button saveContactButton = new Button {Text = "Save Contact", WidthRequest = 150, Margin = new Thickness(50, 0, 0, 0)};
        saveContactButton.Clicked += saveContactButtonClicked;
        Button returnButton = new Button {Text = "Return", WidthRequest = 150, Margin = new Thickness(10, 0, 50, 0)};
        returnButton.Clicked += returnButtonClicked;

        StackLayout buttonLayout = new StackLayout {Orientation = StackOrientation.Horizontal, VerticalOptions = LayoutOptions.Start,
            Children =
            {
                saveContactButton,
                new Label {Text = "", FontSize = 30, FontAttributes = FontAttributes.Bold | FontAttributes.Italic, HorizontalOptions = LayoutOptions.CenterAndExpand},
                returnButton
            }
        };

        Content = new StackLayout {Children = { tableView, buttonLayout, firstLabel, secondLabel }};

        void saveContactButtonClicked(object sender, EventArgs e)
        {
            try
            {
                foreach (var yeah in contactPage.contacts)
                {
                    Debug.WriteLine(yeah.FirstName.ToString());
                }
                // Add new contact to list
                contactPage.contacts.Add(new Contact(firstName.Text.ToString(), lastName.Text.ToString(), contactType.Text.ToString()));
                foreach (var yeah in contactPage.contacts)
                {
                    Debug.WriteLine(yeah.FullName.ToString());
                }

                firstName.Text = String.Empty;
                lastName.Text = String.Empty;
                contactType.Text = String.Empty;
                DisplayAlert("Success", "New contact has been added.", "OK");

            }
            catch(Exception ex)
            {
                Debug.WriteLine("Exception: {0}", ex);
            }
        };

        void returnButtonClicked(object sender, EventArgs e)
        {
            contactPage.listView.ItemsSource = null;
            contactPage.listView.ItemsSource = contactPage.contacts;
            Navigation.PopAsync();
        };
    }
}

1 个答案:

答案 0 :(得分:0)

正如Jason所说,在NewContactPage的构造函数中,您正在创建一个ContactListPage的新实例,它与您来自的页面完全不同(并将返回到PopAsync()

为了使您将新联系人添加到正确的页面,我建议您在ContactListPage的构造函数中传递NewContactPage

public NewContactPage(ContactListPage contactPage)
{
    //As a side note it's probably better practice to make
    //contactPage a private variable for the NewContactPage class
    //...
}

然后在ContactListPage更改按钮点击方法以传入页面:

void OnButtonClicked(object sender, EventArgs args)
{
    Navigation.PushAsync(new NewContactPage(this));
};

希望这有帮助!