当我在绑定中使用时,我尝试在 maui 中发送对象以与 XAML 和 RealCommand 一起运行



我正在尝试将文本插入条目,当我尝试运行此代码时,我得到一个错误消息:

"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')'"

但是我在VerticalStackLayout中说x:DataType="model:Order"并发送CommandParameter="{Binding .}"发送订单对象。所以我不明白这个问题

谢谢!

我在这里得到了错误

{
using Pension.ViewsModels;
namespace Pension.View;
public partial class MainPage : ContentPage
{
public MainPage(MainViewModel vm)
{
InitializeComponent();
BindingContext = vm;//here the error
}
}
}

我想发送对象的函数

{
[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>}

您希望通过按钮中的RelayCommand将模型Order传递给ShowOrderPage,然后您需要像下面这样指定CommandParameter:

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

但是错误告诉了你一切!当您编写不带参数的{Binding}时,它将获得属性BindingContext中的对象,我看到它的类型是MainViewModel。你写的是x:DataType="model:Order",它和{Binding}返回的类型不匹配。您应该在x:DataTypeBindingContext中指定相同的类型,或者第二个应该从第一个派生。

更新:

BindingContext = vm.up(); // Write here your function that gets Order object

最新更新