Hi there!
i've made some docs research about how to pass parameters though command but end up with example below. My goal is to pass string parameter though command from list view to new page as new background image but i ended with Unhandled Exception while trying to achive this. I don't understand x:refenrece to well for now so for any time spend for explanation of this i would be very appreciate.
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Code for new page with parameter passed by
public partial class Page1 : ContentPage
{
public WasteModel wasteModel;
public Page1()
{
InitializeComponent();
//BackgroundImage = wasteModel.WasteMainBackground;
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.FromHex("#586d82");
InfoImageButton.Clicked += InfoImageButton_Clicked;
}
private async void InfoImageButton_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new PageInfo());
}
}
<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="{Binding WasteModel.WasteMainBackground}"
>
MainPage code
<ListView x:Name="wastesListView"
ItemsSource="{Binding Wastes}"
HasUnevenRows="True"
Margin="10,20,5,150"
SeparatorVisibility="None"
BackgroundColor="Transparent"
>
<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 WasteCounter}"
/>
<ImageButton x:Name="iconButton"
Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"
WidthRequest="100"
HeightRequest="100"
Aspect="AspectFit"
BackgroundColor="Transparent"
Source="{Binding WasteIcon}"
Command="{x:Binding Source={x:Reference wastesListView}, Path=BindingContext.NavigateCommand}"
CommandParameter="{x:Binding .}">
</ImageButton>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
MainViewModel code:
public class MainViewModel : BaseViewModel
{
public WasteModel waste;
readonly IPageServices _pageServices;
readonly IWasteService wasteService;
public ICommand NavigateCommand { get; }
public MainViewModel(IPageServices pageServices)
{
this._pageServices = pageServices;
Wastes = GetWastes();
NavigateCommand = new Command(NavigateToNextPage);
}
public string WasteBackroundImage => waste.WasteMainBackground;
public string WasteIcon => waste.WasteIcon;
public double WasteCounter
{
get { return waste.WasteCounter; }
set
{
if (WasteCounter != value) return;
WasteCounter = value;
OnPropertyChanged("WasteCounter");
}
}
public ObservableRangeCollection<WasteModel> Wastes { get; }
public ObservableRangeCollection<WasteModel> GetWastes()
{
var WasteList = new ObservableRangeCollection<WasteModel>()
{
new WasteModel()
{
WasteCounter=.2, WasteIcon="cup.png", WasteMainBackground="background1.png"
}
};
return WasteList;
}
void NavigateToNextPage(object obj)
{
var page = new Page1();
page.BindingContext = waste.WasteMainBackground;
_pageServices.PushAsync(page);
}
Again i would very appreciate for any help with this . Thank you in advance