在视图模型中未找到.net maui绑定属性

8xiog9wr  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(176)

我想使用视图模型将FAB按钮的命令绑定到OnFabTapped命令,而不是使用隐藏的代码(.xaml.cs),但似乎不起作用。
我有一个会话详细信息页面.xaml:

<?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:ScanApp.Models"
             x:Class="ScanApp.Views.SessionDetailPage"
             Title="{Binding Session.Name}"
             xmlns:viewmodel="clr-namespace:ScanApp.ViewModels"
             x:DataType="viewmodel:SessionDetailViewModel">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" />
        <ToolbarItem Text="Save" />
    </ContentPage.ToolbarItems>

    <ContentPage.Resources>
        <Style TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="LightSkyBlue" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>

    <Grid>
        <CollectionView BackgroundColor="Transparent"
                        ItemsSource="{Binding Session.Equipment}"
                        SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="model:Equipment">
                    <Grid Padding="10">
                        <Frame HeightRequest="75">
                            <VerticalStackLayout>
                                <Label Text="{Binding ArticleName}"/>
                                <Label Text="{Binding EquipmentId}"/>
                            </VerticalStackLayout>
                        </Frame>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

        <Button x:Name="actionButton"
                Text="+"
                Command="{Binding OnFABTappedCommand}"
                BackgroundColor="#2196F3"
                TextColor="White"
                CornerRadius="28"
                WidthRequest="56"
                HeightRequest="56"
                HorizontalOptions="End"
                VerticalOptions="End"
                Margin="0,0,16,3" 
                FontSize="24"
                FontAttributes="Bold"
                BorderWidth="0"/>
    </Grid>
</ContentPage>

我有一个会话详细视图模型.cs:

using CommunityToolkit.Mvvm.ComponentModel;
using ScanApp.Models;
using ScanApp.Services;
using System.Collections.ObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;

namespace ScanApp.ViewModels
{

    [QueryProperty("Session", "Session")]
    public partial class SessionDetailViewModel : ObservableObject
    {
        [ObservableProperty]
        string title = "SessionDetailPage";

        public ObservableCollection<Equipment> ScannedItems { get; set; } = new();

        [ObservableProperty]
        Session session;

        [ObservableProperty]
        Equipment selectedEquipment = null;

        Database dbContext;
        public SessionDetailViewModel(Database dbContext)
        {
            this.dbContext = dbContext;
        }

        [RelayCommand]
        async Task OnFABTapped()
        {
            await Shell.Current.DisplayAlert("Alert", "You have been alerted", "OK");
        }
    }
}

OnFabTappedCommand的绑定不起作用,当我在另一个页面上做同样的事情时,它确实起作用了。
为什么它在我的SessionDetailPage上不起作用?

3xiyfsfu

3xiyfsfu1#

是否报告此错误:一个月一个月
您可以向SessionDetailViewModel的构造函数添加参数:

public partial class SessionDetailViewModel : ObservableObject
{
    .....
    Page _page;
    public SessionDetailViewModel(Database dbContext, Page page)
    {
        this.dbContext = dbContext;
        _page = page;

    }

    [RelayCommand]
    async Task OnFABapped()
    {
        await _page.DisplayAlert("Alert", "You have been alerted", "OK");
    }
}

SessionsDetailPage.xaml.cs:

public partial class SessionDetailPage : ContentPage
{
    public SessionDetailPage()
    {
        InitializeComponent();
        BindingContext = new SessionDetailViewModel(dbContext, this);
    }
}

相关问题