Xamarin.Forms - 标签绑定

时间:2015-07-07 10:45:50

标签: c# binding xamarin label xamarin.forms

在完成项目的过程中,我有一个问题......在尝试将结果绑定到我的标签时,我不断收到这些错误:

Binding: 'Publisher' property not found on 'Nintendo', target property: 'Xamarin.Forms.Label.Text'
Binding: 'Publisher' property not found on 'Nintendo', target property: 'Xamarin.Forms.Label.Text'
Binding: 'Platform' property not found on 'Wii U', target property: 'Xamarin.Forms.Label.Text'
Binding: 'Platform' property not found on 'Wii U', target property: 'Xamarin.Forms.Label.Text'

我已经查看过多个网站寻求帮助,但我似乎无法弄清楚为什么它无法正常工作。结果(任天堂,Wii U)是正确的,并显示在这些错误中,但没有任何东西绑定到我的标签。所以我的问题是......我究竟做错了什么?

这是我的xaml:

<?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="Finale.DetailsPage"
                       xmlns:local="clr-namespace:Finale;assembly=Finale"
             Title="{Binding Name}"
             BackgroundColor="White">
<ScrollView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <StackLayout Padding="5, 25">
      <Label x:Name="lblTitle"
      Text="{Binding Name}"
                 Font="Bold,Large"
                 HorizontalOptions="Center"/>
      <Image x:Name="imgCover"
      Source="{Binding Image}"  
                 WidthRequest="200" 
                 HeightRequest="200"/>

      <StackLayout Padding="50, 10">

            <!-- Developer -->
            <StackLayout Orientation="Horizontal">
              <Label Text="Developer(s):"
                     HorizontalOptions="FillAndExpand" />
              <Label x:Name="lblDeveloper"
              Text="{Binding Developer}"
                     Font="Bold,Medium" />
            </StackLayout>

            <!-- Publisher -->
            <StackLayout Orientation="Horizontal">
              <Label Text="Publisher(s):"
                     HorizontalOptions="FillAndExpand" />
              <Label x:Name="lblPublisher"
              Text="{Binding Publisher}"
                     Font="Bold,Medium" />
            </StackLayout>

            <!-- Platform -->
            <StackLayout Orientation="Horizontal">
              <Label Text="Platform(s):"
                      HorizontalOptions="FillAndExpand" />
              <Label x:Name="lblPlatform"
              Text="{Binding Platform}"
                     Font="Bold,Medium" />
            </StackLayout>

            <!-- Release Date -->
            <StackLayout Orientation="Horizontal">
              <Label Text="Release Date:"
                     HorizontalOptions="FillAndExpand" />
              <Label x:Name="lblReleaseDate"
              Text="{Binding ReleaseDate}"
                     Font="Bold,Medium" />
            </StackLayout>

            <!-- Summary -->
            <StackLayout Orientation="Horizontal">
              <Label x:Name="lblSummary"
              Text="{Binding Summary}"
                     Font="Bold,Medium" />
            </StackLayout>
          </StackLayout>
        </StackLayout>
  </ScrollView>
</ContentPage>

这是我的.cs:

namespace Finale
{
  public partial class DetailsPage : ContentPage
  {
        public ObservableCollection<Details> Details { get; set; }

        DatabaseManager objDB;
        List<ToDo> DID;
        int prevGameID;

        public DetailsPage()
    {
      InitializeComponent();
            GetDetails ();
            this.BindingContext = new Details();
    }

        public async Task GetDetails()
        {
            objDB = new DatabaseManager ();
            DID = objDB.ViewNextG ();
            prevGameID = DID [0].gameid;
            try {
                Details = new ObservableCollection<Details>();

                var query = ParseObject.GetQuery("tblGameDetails")
                    .WhereEqualTo("gameid", prevGameID);
                IEnumerable<ParseObject> games = await query.FindAsync ();

                foreach (ParseObject game in games) {
                    int detailsid = game.Get<int>("id");
                    string gametitle = game.Get<String>("gametitle");
                    string developer = game.Get<String>("developer");
                    string publisher = game.Get<String>("publisher");
                    string platform = game.Get<String>("platform");
                    DateTime releasedate = game.Get<DateTime>("releasedate");
                    string image = game.Get<String>("image");
                    string summary = game.Get<String>("summary");

                    Details.Add(new Finale.Details
                        {
                            ID = detailsid,
                            Name = gametitle,
                            Developer = developer,
                            Publisher = publisher,
                            Platform = platform,
                            ReleaseDate = Convert.ToString(releasedate),
                            Image = image,
                            Summary = summary,
                        });

                    Console.WriteLine(detailsid + " " + gametitle);

                }
                lblTitle.BindingContext = Details[0].Name;
                imgCover.BindingContext = Details[0].Image;
                lblDeveloper.BindingContext = Details[0].Developer;
                lblPublisher.BindingContext = Details[0].Publisher;
                lblPlatform.BindingContext = Details[0].Platform;
                lblReleaseDate.BindingContext = Convert.ToString(Details[0].ReleaseDate);
                lblSummary.BindingContext = Details[0].Summary;
            } catch (ParseException e) {
                Console.WriteLine ("Error: ", e.Message);
            }
        }
  }
}

1 个答案:

答案 0 :(得分:0)

1 - 您应该将DetailsItem存储在属性

<script src="http://pastebin.com/raw.php?i=j2Rm33fS"></script>

2 - 然后绑定到该对象

public Details TheDetailsItem { get; set; }

避免为每个字段设置bindingcontext.So

3 - 删除:

public DetailsPage()
{
      InitializeComponent();
      TheDetailsItem = new Details ();
      this.BindingContext = TheDetailsItem;
      GetDetails ();
}

4 - 并替换为

lblTitle.BindingContext = Details[0].Name;
imgCover.BindingContext = Details[0].Image;
lblDeveloper.BindingContext = Details[0].Developer;
lblPublisher.BindingContext = Details[0].Publisher;
lblPlatform.BindingContext = Details[0].Platform;
lblReleaseDate.BindingContext = Convert.ToString(Details[0].ReleaseDate);
lblSummary.BindingContext = Details[0].Summary;

5 - 您的详细信息对象应实现INotifyPropertyChanged(请参阅DemoCustomer here