0

我正在尝试将视图模型作为 XAML 页面的参数传递。

这是基本页面:

public abstract class BaseContentPage<T> : ContentPage where T : BaseVm
{
    public BaseContentPage(T viewModel, string pageTitle)
    {
        BindingContext = ViewModel = viewModel;
        Title = pageTitle;
    }

    protected T ViewModel { get; }
}

public abstract class BaseTabbedPage<T> : TabbedPage where T : BaseVm
{
    public BaseTabbedPage(T viewModel, string pageTitle)
    {
        BindingContext = ViewModel = viewModel;
        Title = pageTitle;
    }

    protected T ViewModel { get; }
}

设置包含以这种方式定义的设备列表:

public partial class SettingsPage : BaseTabbedPage<SettingsVm>
{
    public SettingsPage(SettingsVm vm) : base(vm, "Settings")
    {
        InitializeComponent();
    }
}

public class SettingsVm : BaseVm
{
    public DeviceListVm Devices { get; set; }

    public SettingsVm(ILogger<SettingsVm> logger, MyConfig config) { }
}

public partial class DeviceListPage : BaseContentPage<DeviceListVm>
{
    public DeviceListPage(DeviceListVm vm) : base(vm, "Devices")
    {
        InitializeComponent();
    }
    public DeviceListPage() : base(null, "Devices")
    {
        InitializeComponent();
    }
}

然后困难的东西是在 XAML 中:

<p:BaseTabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:p="clr-namespace:MyProject.Pages"
    xmlns:vm="clr-namespace:MyProject.ViewModels"
    xmlns:vmd="clr-namespace:MyProject.ViewModels.Devices"
    xmlns:local="clr-namespace:MyProject"
    x:Class="MyProject.Pages.SettingsPage"
    x:TypeArguments="vm:SettingsVm">
    <NavigationPage Title="Schedule" IconImageSource="schedule.png">
        <x:Arguments>
            <p:DeviceListPage ViewModel="{Binding Devices}">
                <!--x:Arguments>
                    <vmd:DeviceListVm>{Binding Devices}</vmd:DeviceListVm>
                </x:Arguments-->
            </p:DeviceListPage>
        </x:Arguments>
    </NavigationPage>

</p:BaseTabbedPage>

我没有设法将设备作为 DeviceListPage 的参数传递。

我不想以编程方式执行此操作,因为模型视图是要注入的,除非在这种情况下,根视图模型包含一个子视图模型以供子页面使用。

如果我使用 ViewModel 属性,我会收到以下错误:

错误 XFC0009:没有为“ViewModel”找到属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。

编辑 我更改了基类以声明BindableProperty

public abstract class BaseContentPage<T> : ContentPage where T : BaseVm
{
    public BaseContentPage(T viewModel, string pageTitle)
    {
        ViewModel = viewModel;
        Title = pageTitle;
    }

    public T ViewModel
    {
        get => (T)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, BindingContext = value);
    }
    public BindableProperty ViewModelProperty = BindableProperty.Create(nameof(ViewModel), typeof(T), typeof(BaseContentPage<T>));
}

public abstract class BaseTabbedPage<T> : TabbedPage where T : BaseVm
{
    public BaseTabbedPage(T viewModel, string pageTitle)
    {
        ViewModel = viewModel;
        Title = pageTitle;
    }

    public T ViewModel
    {
        get => (T)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, BindingContext = value);
    }
    public BindableProperty ViewModelProperty = BindableProperty.Create(nameof(ViewModel), typeof(T), typeof(BaseTabbedPage<T>));
}

然后我仍然得到相同的 XFC0009 错误。

4

0 回答 0