1

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>}
Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21

2 Answers2

0

But the error tells you everything! When you write {Binding} without parameters, it gets the object in the property BindingContext, which I see is of type MainViewModel. And you wrote x:DataType="model:Order", and it doesn't match the type {Binding} returns. You should specify the same type in x:DataType and in BindingContext, or the second should be derived from the first.

UPDATE:

BindingContext = vm.up(); // Write here your function that gets Order object
SNBS
  • 671
  • 2
  • 22
  • At the start of the code, I do x:DataType="viewmodel:MainViewModel" so in the construction he is MainViewModel Object, but I want when I click on the button send the object of Order so he knows this is the object I want to send. so I do x:DataType to Order and I try to send this object and he needs to know this is the object no? – Matan Fadida Nov 11 '22 at 12:59
  • Maybe I don't understand something, but you are sending MainViewModel object, not Order. The constructor of your class MainPage has a parameter named vm. You are writing it to the BindingContext. And it's of type MainViewModel. You said you want to send an Order object, not MainViewModel? Or is it a misunderstand? – SNBS Nov 11 '22 at 18:21
  • have a function that needs to get order object , and whan i click at the button I want send an Order object to the function, and the function is found the MainViewModel so I used a bindingContext of MainViewModel to used in that function. and whan I want to send the order I move to the x:DataType of Order to input date and name and i want send this object to the function. like form in HTML. maybe it's not too way to do this, so if you understood now and maybe can help me thank you – Matan Fadida Nov 12 '22 at 16:28
  • Can you get an Order object from a MainViewModel object? If I don't understand something again, I'd like you to express yourself more clearly. – SNBS Nov 12 '22 at 18:38
  • I want to pass the object of Order from the MainPage to another page, and in the MainViewModel I have the function (up) of continue and she gets the order object. so how can I do this? I thought for use in ContinueCommand I need to pass MainViewModel to the MainPage so I did this, but I need to send the object of Order to the function, so I change the x:DataType to Order in VerticalStackLayout so he knows I need to pasing the Order Object to the function when I click on the button. sorry about my English – Matan Fadida Nov 12 '22 at 20:39
  • So your whole page needs a MainViewModel object, and the function needs an Order object. Try calling the function that _gets_ Order object (you said it exists in MainViewModel) and passing _its result to BindingContext, not MainViewModel object_. – SNBS Nov 13 '22 at 13:32
  • but how to do this – Matan Fadida Nov 13 '22 at 20:18
  • Updated my answer, please have a look – SNBS Nov 14 '22 at 17:20
0

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}}}"
Alexandar May - MSFT
  • 6,536
  • 1
  • 8
  • 15
  • first thanks you, but I don't understand why I need to send the binding like this, if I used x: DateType on Order so he does not need to know this is the correct one? And I need to send the specified path as I do up in the Command? (and now it sends the object but it null its don't update what I enter in the Entry) – Matan Fadida Nov 11 '22 at 13:49
  • but it’s still moving object null of order.. how I can fill the details (name,date) and move this object? – Matan Fadida Nov 14 '22 at 14:26
  • I would write a sample for you if I get a chance! – Alexandar May - MSFT Nov 15 '22 at 09:26