c# - XAML 绑定作为参数
问题描述
我正在尝试将视图模型作为 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 错误。
解决方案
推荐阅读
- elasticsearch - 在 Kibana 中使用脚本字段作为时间变量
- django - How to restore sqlite3 database file programmatically in django
- javascript - 在html-select中选择选项后如何避免onclick再次工作?
- python - 来自 SavedModel 的 TensorFlow 推理:期望 attr strides 的 int64_t 值,得到 numpy.int32
- ios - 在后台处理 dataTask GET 请求
- reactjs - useRef 在 DOM 中存储值和显示值 [React]
- migration - Flex 到 Apache Royale 的迁移
- java - 如何从 UTC 日期时间到日期修改小数部分
- r - 删除R中向量中没有空格的重复单词
- sql-server - 无法更新新创建的列