我正在尝试在 .NET MAUI 中构建自定义控件,该控件应为其父级提供 ICommand 的 BindableProperty。这是我试图实现的目标的一个基本示例。
主页视图 (MainPage.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:views="clr-namespace:SampleApp.Views"
x:Class="SampleApp.MainPage">
<views:MyCustomControl DoSomething="{Binding DoSomethingCommand}"></views:MyCustomControl>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
MainPage 视图类 (MainPage.xaml.cs)
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
Run Code Online (Sandbox Code Playgroud)
MainPage 视图模型 (MainPageViewModel.cs)
public class MainPageViewModel : ObservableObject
{
public ICommand ProcessNewScoreCommand { get; }
public MainPageViewModel()
{
ProcessNewScoreCommand = new Command(DoSomething);
}
private void DoSomething()
{
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
MyCustomControl 视图类 (MyCustomControl.xaml.cs)
public partial class …Run Code Online (Sandbox Code Playgroud)