传递所选项目时出现 Bindingcontext 错误
Bindingcontext error when passing selected item
我正在尝试使用 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));
}
}
这是我的底部导航栏 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
应该通过所选项目,还是我错了?
什么可能导致错误?
您正在像这样创建一个新的 BottomNavPage
:new BottomNavPage(BindingContext = e.SelectedItem)
,但是没有接受一个参数的构造函数。
您似乎想直接设置新页面的 BindingContext
属性。将您选择的项目作为参数传递,这需要您更改或向 BottomNavPage
对象添加构造函数:
public BottomNavPage (object yourItem)
{
InitializeComponent ();
BindingContext = yourItem;
}
或者,像这样声明第一行:
await Navigation.PushAsync(new BottomNavPage()
{
BindingContext = e.SelectedItem
});
这会使用无参数构造函数实例化一个新的 BottomNavPage
,但会在创建新实例后立即将您选择的项目分配给 BindingContext
。
我正在尝试使用 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));
}
}
这是我的底部导航栏 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
应该通过所选项目,还是我错了?
什么可能导致错误?
您正在像这样创建一个新的 BottomNavPage
:new BottomNavPage(BindingContext = e.SelectedItem)
,但是没有接受一个参数的构造函数。
您似乎想直接设置新页面的 BindingContext
属性。将您选择的项目作为参数传递,这需要您更改或向 BottomNavPage
对象添加构造函数:
public BottomNavPage (object yourItem)
{
InitializeComponent ();
BindingContext = yourItem;
}
或者,像这样声明第一行:
await Navigation.PushAsync(new BottomNavPage()
{
BindingContext = e.SelectedItem
});
这会使用无参数构造函数实例化一个新的 BottomNavPage
,但会在创建新实例后立即将您选择的项目分配给 BindingContext
。