为什么我不能在.xaml中使用标准控件?

7rtdyuoh  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(117)

我试图为我的MAUI项目创建一个自定义控件,并遵循以下示例:https://www.mfractor.com/blogs/news/custom-controls-in-xamarin-forms
但是当我使用给定的xaml时,它是无效的:

<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="MyProject.Controls.MyActivityIndicator"
                xmlns:local="clr-namespace:MyProject"
                HorizontalOptions="FillAndExpand" 
                x:Name="this">

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label x:Name="LoadingLabel" FontAttributes="Bold" Grid.Row="0" HorizontalOptions="CenterAndExpand" />
    <ActivityIndicator x:Name="ActivityIndicator" Grid.Row="1" HorizontalOptions="CenterAndExpand" />
</Grid>

我得到错误消息:
未找到类型“Grid”。请验证没有缺少程序集引用,并且已生成所有引用的程序集。
Visual Studio建议添加maui命名空间:

xmlns:maui="http://schemas.microsoft.com/dotnet/2021/maui/design"

然后它将识别所有使用的控件,但我不能使用Grid.Row属性:
在类型“Grid”中未找到可附加属性“Row”。

egmofgnx

egmofgnx1#

你的代码中有两个问题:
1.错误的命名空间(您已经修复了)
1.未在代码隐藏中修复继承的类
下面是自定义控件的完整代码,使用Grid作为根元素是没有问题的

<?xml version="1.0" encoding="utf-8" ?>
<Grid
  x:Class="MauiApp2.Controls.MyActivityIndicator"
  xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:local="clr-namespace:MauiApp2.Controls"
  HorizontalOptions="FillAndExpand"
  RowDefinitions="*,*">
  <Label
    Grid.Row="0"
    FontAttributes="Bold"
    HorizontalOptions="CenterAndExpand"
    Text="{Binding Source={RelativeSource AncestorType={x:Type local:MyActivityIndicator}}, Path=Text, Mode=OneWay}" />
  <ActivityIndicator
    Grid.Row="1"
    HorizontalOptions="CenterAndExpand"
    IsRunning="{Binding Source={RelativeSource AncestorType={x:Type local:MyActivityIndicator}}, Path=IsRunning, Mode=OneWay}" />
</Grid>
namespace MauiApp2.Controls;

public partial class MyActivityIndicator : Grid // <-- you have to change that manually
{
    public MyActivityIndicator()
    {
        InitializeComponent();
    }

    public static BindableProperty TextProperty = BindableProperty.Create( nameof( Text ), typeof( string ), typeof( MyActivityIndicator ), default( string ), BindingMode.OneWay );
    public string Text { get => (string)GetValue( TextProperty ); set => SetValue( TextProperty, value ); }

    public static BindableProperty IsRunningProperty = BindableProperty.Create( nameof( IsRunning ), typeof( bool ), typeof( MyActivityIndicator ), false, BindingMode.OneWay );
    public bool IsRunning { get => (bool)GetValue( IsRunningProperty ); set => SetValue( IsRunningProperty, value ); }
}
35g0bw71

35g0bw712#

错误消息的根本原因是xaml中来自maui和xamarin的混合命名空间。
在清理命名空间使其只包含maui命名空间之后,这些控件被识别出来。
但是错误“The attachable property 'Row' was not found in type 'Grid'.”仍然存在。似乎你不能使用Grid作为根元素。在ContentView控件中包含网格后,一切正常。

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyProject.Controls.MyActivityIndicator">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label x:Name="LoadingLabel" FontAttributes="Bold" Grid.Row="0" HorizontalOptions="CenterAndExpand" />
    <ActivityIndicator x:Name="ActivityIndicator" Grid.Row="1" HorizontalOptions="CenterAndExpand" />
</Grid>

相关问题