在 .NET MAUI ContentView 中充当可绑定属性

问题描述 投票:0回答:1
ContentView 中的 Bindable 属性可以是

Func<bool>

 类型吗?也就是说,像这样:

public partial class MyControl : ContentView { public static readonly BindableProperty DoSomethingProperty = BindableProperty.Create( nameof(DoSomething), typeof(Func<bool>), typeof(MyControl); public Func<bool> DoSomething { get => (Func<bool>)GetValue(DoSomethingProperty); set => SetValue(DoSomethingProperty, value); } }
然后你可以在这样的页面中使用它:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:controls="clr-namespace:MauiPlayground.Controls" x:DataType="vm:MainPageViewModel" x:Class="MauiPlayground.MainPage"> <ContentPage.Content> <VerticalStackLayout> <controls:MyControl DoSomething="{Binding ViewModelDoSomething}" /> </VerticalStackLayout> </ContentPage.Content> </ContentPage>
MainPageViewModel 将包含此方法:

public bool ViewModelSoSomething() { return true; }
(这不是实际代码,只是想知道理论上是否可以完成。例如,如果您想将回调函数或方法传递给 ContentView)

我尝试做这样的事情,编译器没有抱怨,但 DoSomething 属性的值从未设置。也许还有另一种方法可以做到这一点?

.net function binding maui contentview
1个回答
0
投票
您可以通过将

BindableProperty

 类型的 
ICommand
 添加到 
ContentView
 来实现此目的,还可以通过添加 
BindableProperty
 类型的 
object
 将数据传递到命令。

我这边实现了这个功能,并且我还给我的ContentView添加了一个公共属性

YourName

请参考以下代码:

MyControl.xaml

public partial class MyControl : ContentView { public String YourName { get { String value = (String)GetValue(YourNameProperty); return value; } set { SetValue(YourNameProperty, value); } } public static readonly BindableProperty YourNameProperty = BindableProperty.Create(nameof(YourName) , typeof(String) , typeof(MyControl), defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnYourNameChanged); static void OnYourNameChanged(BindableObject bindable, object oldValue, object newValue) { Console.WriteLine("-----------------> " + newValue); } // add BindableProperty:ChildCommandProperty public static readonly BindableProperty ChildCommandProperty = BindableProperty.Create(nameof(ChildCommand), typeof(ICommand), typeof(MyControl)); public ICommand ChildCommand { get => (ICommand)GetValue(ChildCommandProperty); set => SetValue(ChildCommandProperty, value); } // add BindableProperty:ChildCommandParameterProperty public static BindableProperty ChildCommandParameterProperty = BindableProperty.Create(nameof(ChildCommandParameter), typeof(object), typeof(MyControl)); public object ChildCommandParameter { get => (object)GetValue(ChildCommandParameterProperty); set => SetValue(ChildCommandParameterProperty, value); } public MyControl()       {             InitializeComponent();       } }

MyControl.xaml.cs

<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiMyContentViewApp.Controls.MyControl" x:Name="TestControlView" > <ContentView.Content> <VerticalStackLayout> <Label Text="{Binding Source={x:Reference TestControlView}, Path=YourName}" x:Name="NameLabel" Margin="10" VerticalOptions="Center" HorizontalOptions="Center" /> <Button Text="Do Something" Command="{Binding Source={x:Reference TestControlView}, Path= ChildCommand}" CommandParameter="{Binding Source={x:Reference TestControlView}, Path= ChildCommandParameter}" /> </VerticalStackLayout> </ContentView.Content> </ContentView>
    
© www.soinside.com 2019 - 2024. All rights reserved.