依赖注入并通过 xaml .NET MAUI 传递 VIewModel 参数

问题描述 投票:0回答:1

我试图通过 xaml 将参数传递给我的 MainPage() 构造函数,但出现错误:“缺少“MyApp.View.MainPage”的默认构造函数

我知道我的 MainPage 代码中缺少无参数构造函数。

以下页面是从 App 类调用的根页面,因此我可以有一个弹出菜单。这是 xaml:

<?xml version="1.0" encoding="utf-8" ?>
<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:MyApp"
            xmlns:view="clr-namespace:MyApp.View"
            xmlns:viewmodel="clr-namespace:MyApp.ViewModel"
            x:Class="MyApp.View.FlyoutMenuRoot"
            Title="FlyoutMenuRoot">
    <FlyoutPage.Flyout>
        <view:FlyoutMenuPage x:Name="flyoutMenuRoot" />
    </FlyoutPage.Flyout>
    <FlyoutPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <view:MainPage>
                    <x:Arguments>
                        <viewmodel:MyAppViewModel />
                    </x:Arguments>
                </view:MainPage>
            </x:Arguments>
        </NavigationPage>
    </FlyoutPage.Detail>
</FlyoutPage>

我很确定

<viewmodel:MyAppViewModel />
行完全错误。我该怎么做才能将 MyAppViewModel 作为参数传递给 MainPage?

xaml mvvm arguments maui .net-maui
1个回答
0
投票

我遇到了同样的问题,我所做的就是删除构造函数注入并向 xaml 添加命名空间。突出显示的标记是解决问题的标记。希望这有帮助。顺便说一句,我正在学习,请原谅我的解释。

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.