Listview 列表视图标题数据绑定不工作:Xamarin表单跨平台应用程序
我正在使用XamarinForms和ListView开发一个iOS/Android组合应用程序,并尝试放置一些数据。我在列表视图中使用了一个数据模板来尝试实现这一点:Listview 列表视图标题数据绑定不工作:Xamarin表单跨平台应用程序,listview,xamarin,header,xamarin.forms,Listview,Xamarin,Header,Xamarin.forms,我正在使用XamarinForms和ListView开发一个iOS/Android组合应用程序,并尝试放置一些数据。我在列表视图中使用了一个数据模板来尝试实现这一点: <ListView x:Name="listView" Header="{Binding BindPrayUpPointMessage}" SelectedItem="Handle_ItemSelected" ItemTapped="Handle_ItemSelected"> <ListView.Hea
<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>
在列表视图页面的代码隐藏中,我有以下设置点:
PrayUpPoints = 1765;
prayupapp.PrayUpNavigationItems.PrayUpPointsMessage = "Total PrayUpPoints: " + PrayUpPoints.ToString();
运行时,标题如下所示:
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()
{
}
}
如何将字符串正确绑定到ListView中的标题 只要删除
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>
正如您所看到的,您需要在一个事件PropertyChanged
中实现INotifyPropertyChanged
接口。每次属性值更改时都必须调用此函数,前提是您希望UI收到此类更改的通知。在上面的示例中,仅会通知PrayUpPointsMessage
上的更改,如果需要其他属性执行相同的操作,则必须遵循相同的模式
请注意,属性不能标记为静态
,这是因为您需要类实例的一部分来访问属性更改
,因此您需要在XAML代码中实际创建并隐藏实例,以便能够赋值
var PrayUpPoints = 1765;
var prayUpNavigationItems = new PrayUpNavigationItems();
prayUpNavigationItems.PrayUpPointsMessage = $"Total PrayUpPoints: {PrayUpPoints}";
查看该类,我还注意到您不再需要BindPrayUpPointMessage
属性,直接绑定到prayupointsmessage
应该可以完成这项工作。如果你真的需要它,你只需要为它实现notify
希望这有帮助。-我会这样做,首先删除标题标签,并直接用
bindprayappointmessage
绑定标签
<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 partial class YourPage : ContentPage
{
public YourPage()
{
InitializeComponent();
var ViewModel = new YourViewModel();
BindingContext = ViewModel;
var PrayUpPoints = 1765;
ViewModel.BindPrayUpPointMessage = "Total PrayUpPoints: " + PrayUpPoints.ToString();
}
}
谢谢那是我第一次尝试的。直接绑定到该字符串或任何其他字符串都不会返回任何结果。静态标签显示,但绑定标签不显示。@logixologist它没有更新,因为您需要告诉它更新。使用绑定ViewModels时,在您的情况下,
PrayUpNavigationItems
必须实现INotifyPropertyChanged
界面,并通知UI进行更改。更新我的原始答案。因此,如果我在另一个页面上更新了总分,如果我希望刷新数据,我如何为此触发PropertyChanged事件。或者我应该以另一种方式刷新。我希望在出现()时向受保护的覆盖无效添加一些代码这似乎是完全不同的问题。按回答的方式签署此文件,然后打开一个新文件。还考虑使用MVVM模式。简言之,这意味着尽可能愚蠢地离开这个观点!但以后会更多!
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)));
}
}
}
public partial class YourPage : ContentPage
{
public YourPage()
{
InitializeComponent();
var ViewModel = new YourViewModel();
BindingContext = ViewModel;
var PrayUpPoints = 1765;
ViewModel.BindPrayUpPointMessage = "Total PrayUpPoints: " + PrayUpPoints.ToString();
}
}