在.NET MAUI中为导航添加背景图像

huangapple go评论104阅读模式
英文:

Add background image to navigation in .net maui

问题

我想让背景图片铺满整个屏幕,并使导航到的页面显示为中心的块。目前,我拥有的背景图片代码不起作用,我不确定如何显示其他页面为块。

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="Nova.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Nova"
    xmlns:views="clr-namespace:Nova.View"
    Shell.FlyoutBehavior="Locked"
    BackgroundImageSource="Assets/BackgroundImage.png">

    <!--<ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage" />-->

    <Shell.FlyoutHeader>
        <Image Source="Assets/logo.png" HeightRequest="152"></Image>
    </Shell.FlyoutHeader>

    <Shell.FlyoutFooter>
        <Label Text="Username" Padding="20"></Label>
    </Shell.FlyoutFooter>

    <FlyoutItem Title="Dashboard" Icon="dotnet_bot.png">
        <Tab>
            <ShellContent 
                Title="Dashboard"
                ContentTemplate="{DataTemplate views:DashboardPage}"/>
        </Tab>

    </FlyoutItem>
    <FlyoutItem Title="Clients" Icon="dotnet_bot.png">
        <Tab>
            <ShellContent 
                Title="Clients"
                ContentTemplate="{DataTemplate views:ClientListPage}"/>
        </Tab>
    </FlyoutItem>
    <FlyoutItem Title="Staff" Icon="dotnet_bot.png">
        <Tab>
            <ShellContent 
                Title="Staff"
                ContentTemplate="{DataTemplate views:StaffListPage}"/>
        </Tab>
    </FlyoutItem>
    <FlyoutItem Title="Projects" Icon="dotnet_bot.png">
        <Tab>
            <ShellContent 
                Title="Projects"
                ContentTemplate="{DataTemplate views:ProjectsPage}"/>
        </Tab>
    </FlyoutItem>
    <FlyoutItem Title="Finances" Icon="dotnet_bot.png">
        <Tab>
            <ShellContent 
                Title="Finances"
                ContentTemplate="{DataTemplate views:FundsPage}"/>
        </Tab>
    </FlyoutItem>

</Shell>
英文:

I would like to make a background image spread across the entire screen, and make the pages navigated to, display as a block in the center. Currently the background image code I have is not working and I'm unsure of how to go about displaying the other pages in a block.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;Shell
x:Class=&quot;Nova.AppShell&quot;
xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
xmlns:local=&quot;clr-namespace:Nova&quot;
xmlns:views=&quot;clr-namespace:Nova.View&quot;
Shell.FlyoutBehavior=&quot;Locked&quot;
BackgroundImageSource=&quot;Assets/BackgroundImage.png&quot;&gt;
&lt;!--&lt;ShellContent
Title=&quot;Home&quot;
ContentTemplate=&quot;{DataTemplate local:MainPage}&quot;
Route=&quot;MainPage&quot; /&gt;--&gt;
&lt;Shell.FlyoutHeader&gt;
&lt;Image Source=&quot;Assets/logo.png&quot; HeightRequest=&quot;152&quot;&gt;&lt;/Image&gt;
&lt;/Shell.FlyoutHeader&gt;
&lt;Shell.FlyoutFooter&gt;
&lt;Label Text=&quot;Username&quot; Padding=&quot;20&quot;&gt;&lt;/Label&gt;
&lt;/Shell.FlyoutFooter&gt;
&lt;FlyoutItem Title=&quot;Dashboard&quot; Icon=&quot;dotnet_bot.png&quot;&gt;
&lt;Tab&gt;
&lt;ShellContent 
Title=&quot;Dashboard&quot;
ContentTemplate=&quot;{DataTemplate views:DashboardPage}&quot;/&gt;
&lt;/Tab&gt;
&lt;/FlyoutItem&gt;
&lt;FlyoutItem Title=&quot;Clients&quot; Icon=&quot;dotnet_bot.png&quot;&gt;
&lt;Tab&gt;
&lt;ShellContent 
Title=&quot;Clients&quot;
ContentTemplate=&quot;{DataTemplate views:ClientListPage}&quot;/&gt;
&lt;/Tab&gt;
&lt;/FlyoutItem&gt;
&lt;FlyoutItem Title=&quot;Staff&quot; Icon=&quot;dotnet_bot.png&quot;&gt;
&lt;Tab&gt;
&lt;ShellContent 
Title=&quot;Staff&quot;
ContentTemplate=&quot;{DataTemplate views:StaffListPage}&quot;/&gt;
&lt;/Tab&gt;
&lt;/FlyoutItem&gt;
&lt;FlyoutItem Title=&quot;Projects&quot; Icon=&quot;dotnet_bot.png&quot;&gt;
&lt;Tab&gt;
&lt;ShellContent 
Title=&quot;Projects&quot;
ContentTemplate=&quot;{DataTemplate views:ProjectsPage}&quot;/&gt;
&lt;/Tab&gt;
&lt;/FlyoutItem&gt;
&lt;FlyoutItem Title=&quot;Finances&quot; Icon=&quot;dotnet_bot.png&quot;&gt;
&lt;Tab&gt;
&lt;ShellContent 
Title=&quot;Finances&quot;
ContentTemplate=&quot;{DataTemplate views:FundsPage}&quot;/&gt;
&lt;/Tab&gt;
&lt;/FlyoutItem&gt;
&lt;/Shell&gt;

答案1

得分: 1

你想要做的事情与 Shell 的设计不符合,也不符合任何其他导航方案(如 NavigationPage);应用程序导航是“页面对页面”,每个页面填充整个屏幕。

相反,创建一个单一页面。在该页面内,使用 ContentView。将该 ContentView 更改为指向(包含)不同的内容。

App.xaml.cs 中,将以下行更改为:

MainPage = new AppShell();

更改为:

MainPage = new MainPage();

其中 MainPage.xaml 大致如下:

<ContentPage ...
x:Class="MyProject.MainPage">
<!-- 数字控制“中间区域”周围的区域 -->
<Grid RowDefinitions="50,*,50" ColumnDefinitions="50,*,50">
<!-- 占据整个屏幕 -->
<Image x:Name="backgroundImage" Grid.Row="0" Grid.RowSpan="3"
Grid.Column="0" Grid.ColumnSpan="3" Source=... />
<!-- “中间区域”。它位于backgroundImage之上 -->
<ContentView x:Name="content" Grid.Row="1" Grid.Column="1" />
</Grid>
</ContentPage>

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
  public MainPage()
  {
    InitializeComponent();
    content.Content = new MyView1();
  }

  public void GoView2()
  {
    content.Content = new MyView2();
  }
}

MyView1.xaml:

<ContentView ...
x:Class="MyProject.MyView1" />
...
</ContentView>

等等。

注意:MyView1(等等)不必是 ContentView。每个都可以是任何布局类,如 GridVerticalStackLayout



可选:要重用视图,请使用服务提供程序而不是 new MyView1()

请参阅 https://stackoverflow.com/a/76741424/199364 中的“AppServiceProvider: 使用服务提供程序避免 'new MyPage();'”部分。

随着您对 Maui 的了解越来越深入,您将看到许多建议使用依赖注入

我使用 content.Content = new MyView1(); 来回答问题,因为这更容易理解和入门。

应用上面链接中的答案部分,将其更改为:

content.Content = AppServiceProvider.GetService<MyView1>();

该部分显示了您需要执行的其他编码,以利用这一技术。

我不会在这里讨论为什么您可能想要这样做。
现在不必担心这个问题,首先让 new MyView1() 起作用。

英文:

What you want to do does not fit with Shells design. Nor any other navigation scheme (such as NavigationPage); app navigation is done "page to page", and a page fills the screen.

Instead, create a single page. Inside it, have a ContentView. Change that ContentView to point to (contain) different content.

In App.xaml.cs, change line:

MainPage = new AppShell();

to:

MainPage = new MainPage();

Where MainPage.xaml is something like:

&lt;ContentPage ...
x:Class=&quot;MyProject.MainPage&quot;&gt;
&lt;!-- numbers control the area surrounding the &quot;middle area&quot; --&gt;
&lt;Grid RowDefinitions=&quot;50,*,50&quot; ColumnDefinitions=&quot;50,*,50&quot;&gt;
&lt;!-- covers the whole screen --&gt;
&lt;Image x:Name=&quot;backgroundImage&quot; Grid.Row=&quot;0&quot; Grid.RowSpan=&quot;3&quot;
Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;3&quot; Source=... /&gt;
&lt;!-- &quot;middle area&quot;. It is &quot;on top of&quot; backgroundImage --&gt;
&lt;ContentView x:Name=&quot;content&quot; Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot; /&gt;
&lt;/Grid&gt;
&lt;/ContentPage&gt;

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
  public MainPage()
  {
    InitializeComponent();
    content.Content = new MyView1();
  }

  public void GoView2()
  {
    content.Content = new MyView2();
  }
}

MyView1.xaml:

&lt;ContentView ...
x:Class=&quot;MyProject.MyView1&quot; /&gt;
...
&lt;/ContentView&gt;

etc.

NOTE: MyView1 (etc.) don't have to be ContentView. Each can be any layout class, such as Grid or VerticalStackLayout.



OPTIONAL: To re-use views, use Service Provider instead of new MyView1()

See section "AppServiceProvider: USE SERVICE PROVIDER TO AVOID "new MyPage();"" of https://stackoverflow.com/a/76741424/199364.

As you become more familiar with Maui, you will see many suggestions to use Dependency Injection.

I've answered the question using content.Content = new MyView1();, because that is easier to understand, and get started with.

Applying the answer-section linked above, change this to:

content.Content = AppServiceProvider.GetService&lt;MyView1&gt;();

That section shows other coding you need to do, to make use of this technique.

I won't discuss here why you might want to do this.
Don't worry about this yet. Get new MyView1() working first.

huangapple
  • 本文由 发表于 2023年7月27日 19:19:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定