Share via

xamarin forms SwipeView command parameter null

UliAdmin 36 Reputation points
2021-03-15T10:16:26.623+00:00

Following strange behaviour when using swipe view with command and command parameter. The command is called, but the parameter is null. However, when I save the xaml view and the hot-reload is triggered (even without changing any code) - the parameter is assigned properly.

Code snippets:
Xaml:

<CollectionView Margin="0,0,0,0" x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                SelectionMode="None">
<CollectionView.ItemTemplate>
                <DataTemplate>
  <SwipeView >
      <SwipeView.RightItems>
        <SwipeItems Mode="Reveal">
            <SwipeItem Command="{Binding BindingContext.DeletePositionCommand, Source={x:Reference BrowsePosPage}}" CommandParameter="{Binding .}" Text="Delete" BackgroundColor="Red" />

        </SwipeItems>
      </SwipeView.RightItems>
                        <ScrollView Margin="0">
                        <StackLayout>

                            <Grid Margin ="0,0,0,0" Padding="0,0,0,0" RowSpacing="2" ColumnSpacing="2" x:DataType="model:GetPositions">

...

ViewModel

public Command DeletePositionCommand { get; }

public GetPositionsMasterViewModel() {

DeletePositionCommand = new Command<GetPositions>(async (position) => await ExecuteDeletePositionsCommand(position));

async Task ExecuteDeletePositionsCommand(GetPositions position)
{
// position is null. Only when xaml is hot reloaded the position gets set.

        if(position != null)
        { 
            ....
        }
    }
Developer technologies | .NET | Xamarin

2 answers

Sort by: Most helpful
  1. Charles 6 Reputation points
    2021-04-17T12:01:48.047+00:00

    Try this:

    CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}"

    Was this answer helpful?

    1 person found this answer helpful.

  2. UliAdmin 36 Reputation points
    2021-03-15T12:48:14.79+00:00

    Just deleted bin/obj, cleaned and rebuild. The error remains. The parameter is null and remains null even if I change pages within the app etc. But when I save this particular xaml file in visual studio (I have tried to save other xaml files but this does not have any effect) and the hot reload is complete - the parameter is correctly available. Obviously this all happens in a debug session. I will compile as a release version in the coming days and revert with the result.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.