Xamarin.forms 表单-为MasterDetailPage应用程序设置一致的背景
作为一个学习/爱好项目,我正在构建一个移动应用程序,我很好奇我能在xamarin应用程序中把设计推进多远。在主视图中使用MasterDetailPage的应用程序中,是否有方法显示一致的背板或图像?我试图达到的效果是,当显示每个页面时,都会有一个透明的背景,应用程序背景永远不会改变,或者是页面转换的一部分 由于从Xamarin.forms 表单-为MasterDetailPage应用程序设置一致的背景,xamarin.forms,Xamarin.forms,作为一个学习/爱好项目,我正在构建一个移动应用程序,我很好奇我能在xamarin应用程序中把设计推进多远。在主视图中使用MasterDetailPage的应用程序中,是否有方法显示一致的背板或图像?我试图达到的效果是,当显示每个页面时,都会有一个透明的背景,应用程序背景永远不会改变,或者是页面转换的一部分 由于从page派生的任何页面类型(Shell、NavigationPage、ContentPage等)都具有BackgroundImageSource属性,因此您可以在App.xaml中全局定
page
派生的任何页面类型(Shell、NavigationPage、ContentPage等)都具有BackgroundImageSource
属性,因此您可以在App.xaml中全局定义该值
<Application.Resources>
<Style ApplyToDerivedTypes="True" TargetType="Page">
<Setter Property="BackgroundImageSource" Value="your_image.png" />
</Style>
</Application.Resources>
如果希望项目中的所有页面具有相同的背景,可以创建一个BaseContentPage,该页面具有
背景图像
或背景颜色
,然后所有其他页面继承自BaseContentPage
:
BaseContentPage:
public class BasePage : ContentPage
{
public BasePage()
{
this.BackgroundImageSource = "logo.jpg";
}
}
项目中的其他页面:
public partial class Page1 : BasePage
{
public Page1()
{
InitializeComponent();
}
}
在Xaml中:
<?xml version="1.0" encoding="utf-8" ?>
<xaminals:BasePage xmlns:xaminals="clr-namespace:Xaminals"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Xaminals.Page1">
<xaminals:BasePage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</xaminals:BasePage.Content>
</xaminals:BasePage>
然后,所有的网页将有标志图像作为背景图像
<?xml version="1.0" encoding="utf-8" ?>
<xaminals:BasePage xmlns:xaminals="clr-namespace:Xaminals"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Xaminals.Page1">
<xaminals:BasePage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</xaminals:BasePage.Content>
</xaminals:BasePage>