列表视图标题数据绑定不起作用:Xamarin表单跨平台应用程序

时间:2017-08-19 03:19:04

标签: listview xamarin header xamarin.forms

我正在使用XamarinForms开发iOS / Android组合应用程序,并在ListView I中尝试放置一些数据。我在列表视图中使用了一个数据模板来尝试完成此任务:

<ListView x:Name="listView" Header="{Binding BindPrayUpPointMessage}" SelectedItem="Handle_ItemSelected" ItemTapped="Handle_ItemSelected">
     <ListView.Header>
     <DataTemplate>
    <StackLayout Padding="10,5,0,5" BackgroundColor="#cccccc">
        <Label Text="Welcome, Logixologist" />
        <Label Text="{Binding .}" FontSize="Small"/>
    </StackLayout>
    </DataTemplate>
  </ListView.Header>

我在底层数据模型中有一个名为BindPrayUpPointMessage的属性,它返回一个值。我将消息绑定到列表视图数据模板。使用一些在线资源但我觉得我有些不对劲。

以下是模型:

public PrayUpNavigationItems()
        {

        }

        public string MainPages { get; set; }
        public string CategoryPages { get; set; }
        public static string BindPrayUpPointMessage { get { return PrayUpPointsMessage; } }
        public static string PrayUpPointsMessage;

在带有列表视图的页面后面的代码中,我有这个来设置点:

PrayUpPoints = 1765;
        prayupapp.PrayUpNavigationItems.PrayUpPointsMessage = "Total PrayUpPoints: " + PrayUpPoints.ToString();

当我运行它时,标题如下所示:

  

Android Version

     

iOS Version

如何正确地将字符串绑定到ListView中的标头。

2 个答案:

答案 0 :(得分:1)

我会这样做首先删除标头标签并直接用BindPrayUpPointMessage绑定标签。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="stackoverflow.YourPage" xmlns:vm="clr-namespace:stackoverflow;">
<ContentPage.BindingContext>
    <vm:YourViewModel/>
</ContentPage.BindingContext>
<ListView x:Name="listView" SelectedItem="Handle_ItemSelected">
<ListView.Header>
    <StackLayout Padding="10,5,0,5" BackgroundColor="#cccccc">
        <Label Text="Welcome, Logixologist" />
        <Label Text="{Binding BindPrayUpPointMessage}" FontSize="Small"/>
    </StackLayout>               
</ListView.Header>
</ListView>

现在告诉您的视图,您的媒体资源BindPrayUpPointMessage已更改

public class YourViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    string bindPrayUpPointMessage;
    public string BindPrayUpPointMessage
    {
        get { return bindPrayUpPointMessage; }
        set
        {
            bindPrayUpPointMessage = value;

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BindPrayUpPointMessage)));
        }
    }
}
在代码隐藏中

,您可以直接设置BindPrayUpPointMessage

public partial class YourPage : ContentPage
{
    public YourPage()
    {
        InitializeComponent();
        var ViewModel = new YourViewModel();
        BindingContext = ViewModel;

        var PrayUpPoints = 1765;
        ViewModel.BindPrayUpPointMessage = "Total PrayUpPoints: " + PrayUpPoints.ToString();
    }
}

答案 1 :(得分:0)

只需删除DataTemplate标记即可开始使用

<ListView x:Name="listView"SelectedItem="Handle_ItemSelected" ItemTapped="Handle_ItemSelected">
    <ListView.Header>
        <StackLayout Padding="10,5,0,5" BackgroundColor="#cccccc">
            <Label Text="Welcome, Logixologist" />
            <Label Text="{Binding PrayUpPointsMessage}" FontSize="Small"/>
        </StackLayout>
    </ListView.Header>
</ListView>

另请注意,我直接在标签中绑定PrayUpPointsMessage

<强>更新

您的PrayUpNavigationItems课程必须更新为以下内容:

public class PrayUpNavigationItems : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string MainPages { get; set; }
    public string CategoryPages { get; set; }

    private string prayUpPointsMessage;
    public string PrayUpPointsMessage
    {
        get
        {
            return prayUpPointsMessage;
        }
        set
        {
            prayUpPointsMessage = value;
            RaisePropertyChanged(nameof(PrayUpPointsMessage));
        }
    }

    private void RaisePropertyChanged([CallerMemberName] string propertyName="")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public PrayUpNavigationItems()
    {
    }
}

如您所见,您需要实现INotifyPropertyChanged接口,只包含一个事件PropertyChanged。每当属性值发生更改时,必须调用此方法,前提是您希望通知UI此类更改。在上面的示例中,只会通知PrayUpPointsMessage上的更改,如果您需要其他属性执行相同操作,则必须遵循相同的模式。

请注意,该属性不能标记为static这是因为您需要类实例来访问PropertyChanged,因此您需要在XAML代码中实际创建和实例才能够分配价值。

var PrayUpPoints = 1765;

var prayUpNavigationItems = new PrayUpNavigationItems();
prayUpNavigationItems.PrayUpPointsMessage = $"Total PrayUpPoints: {PrayUpPoints}";

查看课程我还注意到你不再需要BindPrayUpPointMessage属性,直接绑定到PrayUpPointsMessage应该可以完成这项工作。如果你真的需要它,你只需要为此实现通知。

希望这会有所帮助.-