英文:
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.
<?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>
答案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
。每个都可以是任何布局类,如 Grid
或 VerticalStackLayout
。
可选:要重用视图,请使用服务提供程序而不是 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 Shell
s 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:
<ContentPage ...
x:Class="MyProject.MainPage">
<!-- numbers control the area surrounding the "middle area" -->
<Grid RowDefinitions="50,*,50" ColumnDefinitions="50,*,50">
<!-- covers the whole screen -->
<Image x:Name="backgroundImage" Grid.Row="0" Grid.RowSpan="3"
Grid.Column="0" Grid.ColumnSpan="3" Source=... />
<!-- "middle area". It is "on top of" 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>
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<MyView1>();
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论