我在工具栏中有一个轮播页面和一个主页图标。单击主页图标时,将显示错误。
Xaml中的代码
<ContentPage.ToolbarItems>
<ToolbarItem Icon="home.png" Command="{Binding HomeCommand}"/>
</ContentPage.ToolbarItems>
ViewModel中的代码
public class InfoItemViewModel : ObservableObject, INavigatedAware
{
public DelegateCommand HomeCommand { get; private set; }
private INavigationService mNavigationService;
public INavigationService navigationService;
public InfoItemViewModel(int productId, INavigationService navigationService)
: base(listenCultureChanges: true)
{
_productId = productId;
mNavigationService = navigationService;
HomeCommand = new DelegateCommand(HomeAction);
LoadData();
}
private async void HomeAction()
{
Debug.WriteLine("Button : Home");
await mNavigationService.NavigateAsync("DashboardMenuPage");
}
Xaml.cs中的代码
public partial class InfoItem : ContentPage
{
public InfoItem()
: **this**(DataMyDOF.Products[0].Id)
{
}
public InfoItem(int productId, INavigationService navigationService)
{
InitializeComponent();
BindingContext = new InfoItemViewModel(productId, navigationService);
}
}
}
错误&#39;此&#39;
没有任何论据符合所要求的形式 'InfoItem.InfoItem的参数'navigationService'(int, INavigationService)”
在构造函数中添加navigationService之后,&#39;这个&#39;出现错误。实际上我将grial的旋转木马代码与棱镜结合起来。产品[0] .Id是数组ID。我应该将导航添加为参数吗?
我尝试使用Xamarin Forms Navigation。但仍然存在错误。
using Xamarin.Forms;
public INavigation Navigation { get; }
private void HomeAction()
{
Debug.WriteLine("Button : Home");
Navigation.PushAsync(new NavigationPage(new DashboardMenuPage()));
}
错误
System.NullReferenceException已抛出对象引用未设置 到对象的实例