微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 TabbedPage ContentPage 拆分为不同的文件

如何解决如何将 TabbedPage ContentPage 拆分为不同的文件

我想遵循 SIP(​​单一职责原则)。 我创建了下面的 TabbedPage,分别带有 3 个标签/页面(Page1、Page2 和 Page3),效果很好。

我正在考虑将每个 ContentPage 拆分到它自己的 xaml 文件中,以便它的每个 C# 文件 (xaml.cs) 文件也可以处理它自己的 ContentPage,但最终他们需要表现得像一个普通的 3 页的 tabbedview 页面

我正在考虑这样做,因为每个 ContentPage 在一天结束时都必须有很多东西(代码),这就是为什么我想将每个 ContentPage 分成一个从头开始的单个文件

不确定在 Xamarin 中是否可行。

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TabbedPageApp.MainPage"
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
             android:TabbedPage.ToolbarPlacement="Bottom">
    
    <!--android:TabbedPage.BarItemColor="Black"
             android:TabbedPage.BarSelectedItemColor="Red">-->
    
    <ContentPage Title="Page 1" IconImageSource="outline_settings_black_24dp.png">
        <ContentPage.Content>
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                <Label Text="Welcome to Xamarin - TabbedPage 1" HorizontalTextAlignment="Center" TextColor="Yellow" FontSize="36"/>

                <Entry x:Name="entryB" />
                
                <Button x:Name="myButtonCancel" Text="Cancel" />
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    
    
    <ContentPage Title="Page 2" IconImageSource="outline_add_shopping_cart_black_24dp.png">
        <ContentPage.Content>
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                <Label Text="Welcome to Xamarin - TabbedPage 2" HorizontalTextAlignment="Center" TextColor="Yellow" FontSize="36"/>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    
    
    <ContentPage Title="Page 3" IconImageSource="outline_account_circle_black_24dp.png">
        <ContentPage.Content>
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                <Label Text="Welcome to Xamarin - TabbedPage 3" HorizontalTextAlignment="Center" TextColor="Yellow" FontSize="36"/>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
</TabbedPage>

解决方法

docs 明确显示了如何执行此操作。您只需声明您的 ContentPage XAML 文件所在的命名空间

PageOne 等只是您在项目中创建的 ContentPage XAML 文件

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage"
            x:Class="TabbedPageWithNavigationPage.MainPage">
    <local:PageOne />
    <local:PageTwo />
</TabbedPage>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。