数据绑定不显示任何内容

时间:2019-06-19 13:11:57

标签: xamarin xamarin.forms

我正在尝试将可观察的集合打印到listview中。 它用于自定义导航。它应该输出2个带图标的按钮,一个按钮应该具有活动元素。

由于某种原因,我什么都看不到。当我在Main.xaml / Main.xaml.cs中使用相同的代码时,它可以正常工作。也许对Application类有一些限制,可以防止数据绑定?

App.Xaml

                        <!--test-->
                        <ListView  ItemsSource="{Binding NavigationItemss}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <StackLayout>
                                            <Label Text="{Binding Icon}"></Label>
                                            <Label Text="Testing"></Label>
                                        </StackLayout>

                                    </ViewCell>

                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                        <!--test-->

App.xaml.cs

public partial class App : Application
{

    //TODO: Replace with *.azurewebsites.net url after deploying backend to Azure
    public static string AzureBackendUrl = "http://localhost:5000";
    public static bool UseMockDataStore = true;
    Navigation AppNavigation = new Navigation();
    public App()
    {
        InitializeComponent();

        if (UseMockDataStore)
            DependencyService.Register<MockDataStore>();
        else
            DependencyService.Register<AzureDataStore>();
        this.BindingContext = AppNavigation;
        MainPage = new NavigationPage(new Main());


    }

Navigation.cs

using HOT_App.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using Xamarin.Forms;

namespace HOT_App.ViewModels
{
    class Navigation
    {
        //public List<NavigationItem> NavigationItems;
        public ObservableCollection<NavigationItem> NavigationItemss { get; set; }

        public Navigation()
        { 
            NavigationItemss = new ObservableCollection<NavigationItem>();
            //NavigationItems = new List<NavigationItem>();
            NavigationItem Home = new NavigationItem("Home","NavigationHome.png",true);
            NavigationItem Trends = new NavigationItem("Trends","NavigationTrend.png",false);
            NavigationItemss.Add(Home);
            NavigationItemss.Add(Trends);
            //NavigationItems.Add(Home);
            //NavigationItems.Add(Trends);
        }


        public void ShowActiveNavigation()
        {
            System.Diagnostics.Debug.WriteLine(string.Join<NavigationItem>("\n", NavigationItemss));
        }

        public void SetActiveNavigation(string activeNavigationName)
        {
            ChangeActiveValue(activeNavigationName);
            switch (activeNavigationName)
            {
                case "Home":
                    //activeNavigation = new NavigationItem("Home");
                    Application.Current.MainPage.Navigation.PushAsync(new Main(), false);
                    System.Diagnostics.Debug.WriteLine("Home");
                    break;
                case "Trends":
                    //activeNavigation = new NavigationItem("Trends");
                    Application.Current.MainPage.Navigation.PushAsync(new Trends(), false);
                    System.Diagnostics.Debug.WriteLine("Trends");
                    break;
                default:
                    //activeNavigation = new NavigationItem("Home");
                    Application.Current.MainPage.Navigation.PushAsync(new Main(), false);
                    System.Diagnostics.Debug.WriteLine("Home");
                    break;

            }

        }
        public void ChangeActiveValue(string activeNavigationName)
        {
            foreach(NavigationItem navigationItem in NavigationItemss)
            {

                if (navigationItem.NavigationItemName == activeNavigationName)
                {
                    navigationItem.Active = true;
                }
                else { navigationItem.Active = false; }
            }
        }

    }
}

2 个答案:

答案 0 :(得分:2)

通常我们不会在App.xaml

中创建页面

我认为 MasterDetailPage 是解决问题的最快方法,您只需要配置 Master Detail 页,此处为Master页是使用ListView的导航页面,详细信息页面是主页和主页。

您可以参考MasterDetails Page

ps:如果您的Xamarin.Forms为4.0及更高版本,则也可以使用Shell

答案 1 :(得分:0)

DataBinding也可以在controlTemplate内部使用。您可以在此处找到有关它的更多信息。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding