gpt4 book ai didi

c# - Xamarin Forms 找不到如何将对象与详细信息 View 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:13 25 4
gpt4 key购买 nike

我正在使用 Xamarin Forms,但我不想在 ListView 中单击某个元素后显示它的详细信息。它将打开一个包含详细信息的新 View 。我不知道如何将整个对象绑定(bind)到 View ,以便轻松使用他的属性。

这是我的代码:

   public partial class ContactDetailPage : ContentPage
{
public ContactDetailPage(Agency agencyItem)
{
InitializeComponent();
// The agencyItem is get from a list view
// HERE I suppose I need to bind the agencyItem to the view ?
}
}

这是我显示代理机构的详细 View :

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.ContactDetailPage">
<ContentPage.Content>
<StackLayout Spacing="0">
<Label Text="{Binding agency.name}" FontSize="Micro" LineBreakMode="TailTruncation" />
<Label Text="{Binding agency.address}" FontSize="Micro" LineBreakMode="TailTruncation" />
</StackLayout>

</ContentPage.Content>
</ContentPage>

如何将 View 绑定(bind)到 agencyItem 以显示它?

最佳答案

查看 Xamarin documentation对此。

简短的回答是:

BindingContext = agencyItem;

BindingContext是每个 Page 的属性它负责绑定(bind)等。请注意,您不需要执行 <Label Text="{Binding agency.name}" ... />如果名称直接在 agencyItem 中.你可以只使用 <Label Text="{Binding name}" ... />直接地。所以从根对象开始。如果你的agencyItem有一个 String属性(property)Name , 只需使用 Name直接地。如果agencyItem具有复杂的属性 Agency它有一个 String属性(property)Name , 那么您当前的绑定(bind)是正确的。

您也可以选择在 XAML 中执行此操作。然后您必须创建一个属性并实现 INotifyPropertyChanged 接口(interface)。

我还注意到您没有使用像 MVVM 这样的模式。这可能也值得研究!

关于c# - Xamarin Forms 找不到如何将对象与详细信息 View 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38740136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com