簡體   English   中英

我如何讓 MAUI 在所有頁面上顯示圖像?

[英]How do I get MAUI to show an image on all pages?

我正在為我的公司設計一個應用程序,我需要它在應用程序的每一頁上顯示公司的徽標。 我如何在 MAUI 中實現這一點?

我試過的:

AppShell.xaml:

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="FusionPortalDemo.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:FusionPortalDemo"
    Shell.FlyoutBehavior="Disabled">

    <ShellContent Title="RootPage" 
                  ContentTemplate="{DataTemplate local:GlobalLogoPage}"
                  Route="GlobalLogoPage" />

    <ShellContent Title="Home"
        ContentTemplate="{DataTemplate local:HomePage}"
        Route="HomePage" />

</Shell>

全球標志頁面:

<?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="FusionPortalDemo.GlobalLogoPage"
             Title="GlobalLogoPage">
    <Image Source="psl_logo.jpeg"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
</ContentPage>

但是,這只會顯示包含整個徽標的一頁。 我懷疑另一個頁面正在顯示,但隱藏了,因為它的高度為 0。 我究竟該如何前進?

#1 解決方法:

您可以創建一個包含logo的通用TitleView ,然后在您要顯示的Content pages中重復使用它。

1.為您的項目創建一個ContentViewTitle.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="MauiAppCustomTitleView.Title">
    <ContentView.Content>
        <Image Source="dotnet_bot.png" HorizontalOptions="Center" VerticalOptions="Center"> </Image>
    </ContentView.Content>
</ContentView>

  1. 在您的MainPage中使用TitleViewTitle.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:TitleView="clr-namespace:MauiAppCustomTitleView"
             x:Class="MauiAppCustomTitleView.MainPage">

    <Shell.TitleView>
        <TitleView:Title></TitleView:Title>
    </Shell.TitleView>



</ContentPage>

#2 解決方法:

或者您可以使用控件模板,使您能夠設計模板控件或模板頁面以在 MAUI 中reuse

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM