c# - Xamarin 表单绑定(bind)上下文和绑定(bind)

标签 c# xamarin xamarin.forms

我要实现的目标:

我有一个通过 API 调用检索数据的 Xamarin 表单应用程序,该数据被设置为 GamesResultModel 并绑定(bind)到 .APP 中的公共(public)属性,然后数据显示在 ListView 中。现在,当我使用多个页面时,每个页面都绑定(bind)到他们需要的数据,但我只想使用一个被多次实例化的页面,并且在实例化时设置绑定(bind)源和页面标题。

问题:

当页面从根页面实例化时,我不确定如何设置页面的绑定(bind)源?我尝试了以下但它没有用

public static GamesPageModel Future { get; set; }
FuturePage.SetBinding(ListView.ItemsSourceProperty, "Future");

代码示例:

public class RootPage : TabbedPage
{

    public static GamesPageModel History { get; set; }

    public static GamesPageModel Current { get; set; }

    public static GamesPageModel Future { get; set; }

 public RootPage()
    {
        ShowLoginDialog();
        buildTabs();
    }

我从根页面为每个时间跨度实例化一个新的 GamesPage 实例:

    private void buildTabs()
    {
        var historyPage = new GamesPage();
        historyPage.Title = "History";
        historyPage.SetBinding(ListView.ItemsSourceProperty, "History");

        var todayPage = new GamesPage();
        todayPage.Title = "Current";
        todayPage.SetBinding(ListView.ItemsSourceProperty, "Today");

        var FuturePage = new GamesPage();
        FuturePage.Title = "Future";
        FuturePage.SetBinding(ListView.ItemsSourceProperty, "Future");

        this.Children.Add(todayPage);
        this.Children.Add(historyPage);
        this.Children.Add(futurePage);
}

设置API调用的游戏结果模型:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class GamesResult
  {
[System.Xml.Serialization.XmlArrayItemAttribute("item", IsNullable = false)]
public xmlItem[] previous { get; set; }

[System.Xml.Serialization.XmlArrayItemAttribute("item", IsNullable = false)]
public xmlItem[] current { get; set; }

[System.Xml.Serialization.XmlArrayItemAttribute("item", IsNullable = false)]
public xmlItem[] future { get; set; }
 }

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class xmlItem
 {
public ushort field_id { get; set; }

public string field_desc { get; set; }

public ushort fk_round_id { get; set; }

public string field_start_time { get; set; }

public byte tee_interval { get; set; }

public byte fk_course_id { get; set; }

public uint field_pin { get; set; }

public ushort fourball_id { get; set; }

public ushort fk_field_id { get; set; }

public ushort fk_course_hole_id_tee_off { get; set; }

public string tee_off_time { get; set; }

public ushort fourball_team_id { get; set; }

public byte team_desc { get; set; }

public ushort fk_fourball_id { get; set; }

public ushort fourball_player_id { get; set; }

public ushort fk_player_mem_id { get; set; }

public byte player_hc { get; set; }

public ushort fk_player_team { get; set; }

public byte fk_player_tee { get; set; }

public ushort round_id { get; set; }

public string round_desc { get; set; }

public ushort fk_tour_id { get; set; }

public string round_date { get; set; }

public ushort tour_id { get; set; }

public string tour_desc { get; set; }

public xmlItemTour_start tour_start { get; set; }

public xmlItemTour_end tour_end { get; set; }

public ushort fk_member_id { get; set; }

public xmlItemCreate_date create_date { get; set; }

public byte fk_comp_typ_id { get; set; }

public byte tour_status { get; set; }
}

游戏页面:

public class GamesPage : ContentPage
{
    #region Private Properties

    private GamesPageModel viewModel
    {
        get { return BindingContext as GamesPageModel; }
    }

    private ListView listView;
    #endregion
    this.Content = new StackLayout()
        {       
            Children ={
                 listView
            }
        };
    }
    public GamesPage()
    {
        this.SetBinding(TitleProperty, "Title");
        listView = new ListView();
        var viewTemplate = new DataTemplate(typeof(GameViewCell));  
        listView.ItemTemplate = viewTemplate;
        listView.SetBinding(ListView.ItemsSourceProperty, "Games");

游戏页面模型:

public class GamesPageModel
{
    public GamesPageModel()
    {
        Games = new List<xmlItem>();
    }

    public string Title { get; set; }

    public List<xmlItem> Games { get; set; }

}

游戏 View 单元格:

public class GameViewCell : ViewCell
{
    public GameViewCell()
    {
        Label tour_Desc = new Label()
            {
                Font = Font.BoldSystemFontOfSize(15)
            };

          Label round_Desc = new Label();
          Label field_Desc = new Label();

        tour_Desc.SetBinding(Label.TextProperty, "tour_desc");
        round_Desc.SetBinding(Label.TextProperty, "round_desc");
        field_Desc.SetBinding(Label.TextProperty, "field_desc");

        var layout = new StackLayout();
        layout.Children.Add(tour_Desc);
        layout.Children.Add(round_Desc);
        layout.Children.Add(field_Desc);

        this.View = layout;
    }

}

Singleton 用于设置 API 调用返回的数据,以便在应用程序的任何地方使用:

public class App
{
    private static readonly App instance = new App();
    private LoginResult loginResult;
    private GamesResult gamesResult;

    public static App Instance
    {
        get
        {
            return instance;
        }
    }

    public LoginResult LoginResult
    {
        get
        {
            return loginResult;
        }
        set
        {
            loginResult = value;
        }
    }

    public GamesResult GamesResult
    {
        get
        {
            return gamesResult;
        }
        set
        {
            gamesResult = value;
        }
    }
}

通过一个简单的多页面实现,如下例所示:

        var todayPage = new CurrentGamesPage();
        var historyPage = new HistoryGamesPage();
        var futurePage = new FutureGamesPage();

        this.Children.Add(todayPage);
        this.Children.Add(historyPage);
        this.Children.Add(futurePage);


public class HistoryGamesPage : ContentPage
{
    private ListView listView;

    public HistoryGamesPage()
    {
        Title = "Current Games";

        listView = new ListView();

        BindData();

        this.Content = new StackLayout()
       {
          Children = {
              listView
          }
       };
    }

    public void BindData()
    {
        if (App.Instance.GamesResult != null)
        {
            try
            {
                var viewTemplate = new DataTemplate(typeof(GameViewCell));
                listView.ItemTemplate = viewTemplate;
                listView.ItemsSource = App.Instance.GamesResult.previous;
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }

}

最佳答案

我认为在 BuildTabs 中你想要更像的东西:

FuturePage.BindingContext = RootPage.Future;

代替

FuturePage.SetBinding(ListView.ItemsSourceProperty, "Future");

因为您的“FuturePage”不是 ListView 而是 ContentPage,并且您正在设置 BindingContext 而不是分配绑定(bind)。

关于c# - Xamarin 表单绑定(bind)上下文和绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24865706/

相关文章:

c# - 我可以给我的本地主机应用程序一个 'web address format'

c# - 从 WindowsService 我如何从 C# 中找到当前登录的用户?

c# - ListView 项目文本在滚动时发生变化

android - Xamarin.Forms:单击后按钮文本对齐错误 (Android)

c# - 使用绑定(bind)作为 ConverterParameter

c# - Xamarin.Forms 动态布局取决于屏幕方向或大小

c# - 什么都不抛出时的异常效率

c# - System.IO.Path.GetFileNameWithoutExtension 不修剪文件路径

ios - MT5212 : Native linking failed, 重复符号: '_OBJC_IVAR_$_FIRInstanceID._tokenManager'

android - Xamarin Android服务崩溃