首先,您可以创建一个名为Name.xaml
的新.xaml文件。您可以在其中编写一些代码。
<?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="CusComponents.Name">
<ContentView.Content>
<StackLayout Padding="10">
<Label Text="Name" FontAttributes="Bold" />
<Label Text="First name" />
<Entry x:Name="FirstName" Placeholder="First name" />
<Label Text="Last name" />
<Entry x:Name="LastName" Placeholder="Last name" />
</StackLayout>
</ContentView.Content>
</ContentView>
其次,您可以像这样在所需页面中使用它。您需要在 XML 文件顶部添加 xmlns 引用 - 这类似于 C# 文件中的 using 语句。使用示例项目的命名空间结构,这将是 xmlns:custom_components="clr-namespace:CusComponents"
。
<?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:custom_components="clr-namespace:CusComponents"
x:Class="CusComponents.MainPage">
<custom_components:Name />
</ContentPage>
xaml.cs
中创建 BindableProperty
。请参考 https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/bindable-properties?view=net-maui-7.0#create-a-bindable-property。 - yurislav
ContentView
或任何Layout
类继承。向其中添加BindableProperty
;这就是为您的视图提供“参数”的方式,因此可以在每次使用时进行自定义重用。现在,您可以像使用任何内置UI视图一样,在XAML或c#标记中使用它。这就是您所需要的全部内容。寻找Xamarin Forms教程- Maui的工作方式相同。查找任何子类化UI类并向其添加BindableProperty的教程,然后显示使用该自定义视图及其自定义属性的XAML。 - ToolmakerSteve