【问题标题】:How to split TabbedPage ContentPage into different file如何将 TabbedPage ContentPage 拆分为不同的文件
【发布时间】:2021-07-09 23:14:45
【问题描述】:

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

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

我正在考虑这样做,因为每个 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>

【问题讨论】:

    标签: xaml xamarin xamarin.forms tabbedpage


    【解决方案1】:

    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>
    

    【讨论】:

    • 谢谢杰森。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2019-10-16
    • 2021-07-26
    • 2018-01-07
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多