我面临将字符串参数作为新页面的背景图像发送的问题。我要实现的就像一个对象列表,每个对象都有新的背景图像提示字符串,单击对象用户的图标后将重定向到带有背景图像的新页面。问题在于,发送参数时会出现未处理的异常
Unhandled Exception:
System.NullReferenceException:对象引用未设置为对象的实例。发生
这是我为达到这一目的而写的内容: MainPage xaml
<ListView x:Name="wasteListView"
ItemsSource="{Binding Wastes}"
HasUnevenRows="True"
Margin="10,20,5,150"
SeparatorVisibility="None"
BackgroundColor="Transparent"
ItemTapped="OnSelectedItem"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ProgressBar x:Name="progressBar"
Grid.RowSpan="2" Grid.Row="0" Grid.Column="0"
BackgroundColor="Transparent"
ProgressColor="#614c96"
Progress="{Binding ObjectCounter}"
/>
<ImageButton x:Name="iconButton"
Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"
WidthRequest="100"
HeightRequest="100"
Aspect="AspectFit"
BackgroundColor="Transparent"
Source="{Binding ObjcetIcon}"
Command="{x:Binding Source={x:Reference wasteListView}, Path=BindingContext.NavigationCommand}"
CommandParameter="{x:Binding .}">
</ImageButton>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
主页代码开始:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var pageServices = new PageServices();
BindingContext = new MainViewModel(pageServices);
}
private async void OnSelectedItem(object sender, ItemTappedEventArgs e)
{
var sampleDetails= e.Item as SampleModel;
await Navigation.PushAsync(new Page1(sampleDetails), true);
}
}
}
MainViewModel
public ObjectModel sample;
public MainViewModel(IPageServices pageServices)
{
this._pageServices = pageServices;
Wastes = GetWastes();
NavigationCommand= new Command(NavigateToObejctPage);
}
void NavigateToObjectPage(object obj)
{
_pageServices.PushAsync(new Page1(sample));
}
页面服务是我的导航服务
第1页代码隐藏:
public Page1(SampleModel sampleModel)
{
InitializeComponent();
//This is working when i tapped item from list view,
BackgroundImage = sampleModel.SampleBackgroundImage;
}
Page1 xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppDemo.Page1"
xmlns:vm="clr-namespace:AppDemo.ViewModels"
NavigationPage.HasNavigationBar="True"
BackgroundImage="{x:Binding sampleModel.SampleBackgroundImage}"
>
您在此处看到的内容运行正常,并且符合预期,但仅在轻按的listview项上有效。如何通过命令将背景图像作为参数传递?预先感谢您的任何想法。如果可能的话,我可以要求一些谴责。再次谢谢你:)