传递所选项目时出现绑定上下文错误

时间:2018-07-02 08:56:11

标签: c# xamarin.forms bottomnavigationview

我正在尝试使用NuGet:ThriveGmbH.BottomNavigationBar.XF

创建底部导航栏

我希望我的想法正确,但这是我的设置。

这是链接到我的个人资料页面的代码,在这里我得到了错误

Severity    Code Description    Project File    Line    Suppression State
Error   CS1729  'BottomNavPage' does not contain a constructor that takes 1 arguments   DoggaLogg.Android, DoggaLogg.iOS, DoggaLogg.UWP C:\Users\mawil3\source\repos\DoggaLogg\DoggaLogg\DoggaLogg\View\HomePage.xaml.cs    40  Active
Error   CS1729  'BottomNavPage' does not contain a constructor that takes 1 arguments   DoggaLogg.Android, DoggaLogg.iOS, DoggaLogg.UWP C:\Users\mawil3\source\repos\DoggaLogg\DoggaLogg\DoggaLogg\View\HomePage.xaml.cs    40  Active)
async void ProfileList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    if (e.SelectedItem != null)
    {
        await Navigation.PushAsync(new BottomNavPage(BindingContext = e.SelectedItem));
    }
}

这是我的bottomnavbar xaml页面:

<xf:BottomBarPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="DoggaLogg.View.BottomNavPage"
         xmlns:xf="clr-namespace:BottomBar.XamarinForms"
         xmlns:pages="clr-namespace:DoggaLogg.View">


<pages:ProfilePage Title="Profile" Icon="icon" />

<pages:GoalPage Title="Goalse" Icon="icon" /></xf:BottomBarPage>

这里是Bottomnavbar.cs

namespace DoggaLogg.View{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BottomNavPage : ContentPage
{
    public BottomNavPage ()
    {
        InitializeComponent ();
    }
}}

据我了解,e.selecteditem应该通过所选的项目,还是我错了?

什么可能导致错误?

1 个答案:

答案 0 :(得分:1)

您正在像这样创建新的BottomNavPagenew BottomNavPage(BindingContext = e.SelectedItem),但是没有构造函数接受一个参数。

似乎您正在尝试直接设置新页面的BindingContext属性。将您选择的项目作为参数传递,这要求您更改或向BottomNavPage对象添加构造函数:

public BottomNavPage (object yourItem)
{
    InitializeComponent ();

    BindingContext = yourItem;
}

或者,像这样声明第一行:

await Navigation.PushAsync(new BottomNavPage()
{
    BindingContext = e.SelectedItem
});

这会使用无参数构造函数实例化一个新的BottomNavPage,但在创建新实例后立即将您选择的项目分配给BindingContext