Search code examples
c#mauiwxmaximamaui-community-toolkit

I'm try to send object in maui to function with XAML and RealCommand when i used in Binding


I'm trying to insert text into Entry and when I try to run this code I get an error message:

"System.ArgumentException: 'Parameter "parameter" (object) cannot be of type Pension.ViewsModels.MainViewModel, as the command type requires an argument of type Pension.Models.Order. (Parameter 'parameter')'"

but I in the VerticalStackLayout say x:DataType="model:Order" and send in CommandParameter="{Binding .}" to send the Order Object. So i not understand the problem

Thanks !

and I get the error here

{
   using Pension.ViewsModels;

    namespace Pension.View;

    public partial class MainPage : ContentPage
    {
       public MainPage(MainViewModel vm)
       {
          InitializeComponent();
          BindingContext = vm;//here the error
       }

    }
}

the function i want to send the object

{
[RelayCommand]
        async Task Continue(Order order)
        {
            //Order order = new Order()
            //{
            //    DateStart = dateStart,
            //    DateEnd = dateEnd,
            //    DogName = dogName
            //};
            await Shell.Current.GoToAsync(nameof(ShowOrderPage), true, new Dictionary<string, object>
            {
                {"Order", order}
            });
        }
}
{<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:model="clr-namespace:Pension.Models"
             xmlns:viewmodel="clr-namespace:Pension.ViewsModels"
             x:DataType="viewmodel:MainViewModel"
             x:Class="Pension.Views.MainPage"
             BackgroundColor="#F2F2F2">

    <VerticalStackLayout Margin="0,20,0,0" Spacing="10" FlowDirection="RightToLeft">
        <Label Text="הזמנת פנסיון לכלב" FontSize="40" HorizontalOptions="Center" Margin="5"/>
        <Label Text="לפני שנתחיל נשמח לדעת מה התאריך בו תרצו להתארח אצלנו" HorizontalOptions="Center" FontSize="20" FontFamily="op-light"/>
        <Grid Padding="20" Background="#FFFFFF" Margin="54">
            <Grid.Shadow>
                <Shadow Brush="#000"
                Offset="5,0"
               
                Opacity="0.26"/>
            </Grid.Shadow>
            <VerticalStackLayout Spacing="10" x:DataType="model:Order">
                <Label Text="בחירת תאריך" FontSize="30" HorizontalOptions="Center" Margin="0,0,0,15"/>
                <Frame x:Name="DateStart" CornerRadius="0" Padding="10,0">
                    <DatePicker MinimumDate="01/01/2022"
                MaximumDate="12/31/2025"
                Date="{Binding DateStart}"/>
                </Frame>
                <Frame CornerRadius="0" Padding="10,0">
                    <DatePicker x:Name="DateEnd" MinimumDate="01/01/2022"
                MaximumDate="12/31/2025"
                Date="{Binding DateEnd}" />
                </Frame>
                <Frame CornerRadius="0" Padding="10,0">
                    <Entry x:Name="dogName" Placeholder="שם הכלב/ה" Text="{Binding DogName}"/>
                </Frame>
                <Button Text="המשך" BackgroundColor="#EEBF3E"
                        TextColor="Black" CornerRadius="0"
                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}},Path=ContinueCommand}"
                        CommandParameter="{Binding .}">
                   
                    <Button.Shadow>
                        <Shadow Brush="#ccccd0"
                            Offset="3,6"
                            Opacity="1"/>
                    </Button.Shadow>
                </Button>
            </VerticalStackLayout>
        </Grid>
    </VerticalStackLayout>
</ContentPage>}

Solution

  • You want pass the model Order to the ShowOrderPage through the RelayCommand from the Button then you need specify the CommandParameter like below:

    CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type model:Order}}}"