未显示来自新页面基类的.Net Maui Xaml

tjrkku2a  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(149)

我想创建一个基于contentpage类的新的应用程序特定的页面基类。它就像一个超级页面,本身就有一些内容。我想从应用程序的页面,使用新的应用程序特定的网页基类的所有内容,被添加后的新的应用程序特定的基类的内容。
我有一个新的基类创建,我有一些XAML在它。问题是我从来没有在我的应用程序页面上看到在应用程序特定基类中定义的标签控件。我知道我错过了一些愚蠢的东西-可能是处理contentpage类的content属性,但我一直没能弄清楚。任何建议/想法将非常感谢。
下面是我的应用程序特定基类的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:localapp="clr-namespace:GBarScene_Maui;assembly=GBarScene-Maui"
         x:Class="GBarScene_Maui.ContentPageTypes.GBSPage">

<ContentPage.Background>
    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFF3F3"
                   Offset="0" />
        <GradientStop Color="#FFF3F3"
                   Offset=".16666" />
        <GradientStop Color="#FFF8F3"
                   Offset=".16667" />
        <GradientStop Color="#FFF8F3"
                   Offset="0.33332" />
        <GradientStop Color="#FFFDF3"
                   Offset="0.33333" />
        <GradientStop Color="#FFFDF3"
                   Offset="0.49999" />
        <GradientStop Color="#F3F8F3"
                   Offset="0.50000" />
        <GradientStop Color="#F3F8F3"
                   Offset="0.66666" />
        <GradientStop Color="#F2F5FF"
                   Offset="0.66667" />
        <GradientStop Color="#F2F5FF"
                   Offset="0.83332" />
        <GradientStop Color="#F7F3F8"
                   Offset="0.83333" />
        <GradientStop Color="#F7F3F8"
                   Offset="1.0" />
    </LinearGradientBrush>

</ContentPage.Background>
<ContentPage.Content>
    <VerticalStackLayout HorizontalOptions="Fill" VerticalOptions="Fill">
    <Label Text="This is a test" />
    </VerticalStackLayout>
</ContentPage.Content>

下面是C#代码:
命名空间GBarScene_Maui.ContentPageTypes;

public abstract partial class GBSPage : ContentPage
{
    public GBSPage()
    {
        InitializeComponent();
    }

}

下面是基于新应用特定内容页面基类的新应用页面的XAML:

<?xml version="1.0" encoding="utf-8" ?>
<local:GBSPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:GBarScene_Maui.ContentPageTypes"
             Shell.NavBarIsVisible="False"
             Shell.BackgroundColor="Transparent"               
             x:Class="GBarScene_Maui.MainPage">

    <ContentPage.Content>
        <ScrollView>
            <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

                <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />

                <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />

                <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                FontSize="18"
                HorizontalOptions="Center" />

                <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

            </VerticalStackLayout>
        </ScrollView>
    </ContentPage.Content>

</local:GBSPage>

下面是新应用程序页面的C#代码:

using GBarScene_Maui.ContentPageTypes;

namespace GBarScene_Maui
{
    public partial class MainPage : GBSPage
    {
        int count = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        private void OnCounterClicked(object sender, EventArgs e)
        {
            count++;

            if (count == 1)
                CounterBtn.Text = $"Clicked {count} time";
            else
                CounterBtn.Text = $"Clicked {count} times";

            SemanticScreenReader.Announce(CounterBtn.Text);
        }
    }
}
z9ju0rcb

z9ju0rcb1#

XAML是将在编译时自动生成代码的标记,因此是不可继承的。
为此,您可以使用ContentPresenter来实现这一点。
ContentPresenter可以放置在控件模板中,以标记模板化自定义控件或模板化页显示内容的位置。使用控件模板的自定义控件或页将定义ContentPresenter显示的内容。
您可以参考以下代码:

BasePage.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"
             x:Class="MauiApp821.BasePage"
             Title="BasePage">
    <ContentPage.ControlTemplate>
        <ControlTemplate>
            <VerticalStackLayout HorizontalOptions="Fill" VerticalOptions="Fill">
                
                <Label Text="This is a test" />

                <!-- Content from child pages -->
                <ContentPresenter />
                
            </VerticalStackLayout>
        </ControlTemplate>
    </ContentPage.ControlTemplate>
</ContentPage>

TestPage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<mauiapp:BasePage 
             xmlns:mauiapp="clr-namespace:MauiApp821"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp821.TestPage"
            >
    <ContentPage.Content>
            <VerticalStackLayout BackgroundColor="Purple"
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">
                <Label  Text="this is the content of ChildPage"/>

            </VerticalStackLayout>
    </ContentPage.Content>
</mauiapp:BasePage>

TestPage.xaml.cs

public partial class TestPage: BasePage 
{
      public TestPage()
      {
            InitializeComponent();

      }
}

有关详细信息,请检查将文档内容替换到ContentPresenter中。

相关问题