您可以只隐藏未使用的面板(使用 IsVisible 属性) - 这会将它们从可视化树中拉出,但不会从内存中释放它们。
如果您为每个页面创建一个内容视图,那么您可以轻松地在主 UI 中使用它们,例如在本示例中。即使我们隐藏了各个面板,它们在隐藏时仍会保留在内存中:
MyView.cs(这可以是您面板中的任何内容):
using System;
using Xamarin.Forms;
namespace FormsSandbox
{
public class MyView : ContentView
{
public static BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MyView),
String.Empty, BindingMode.Default, null, TextChanged);
public string Text {
get {
return (string)GetValue (TextProperty);
}
set {
SetValue (TextProperty, value);
}
}
private Label _contentLabel;
public MyView ()
{
_contentLabel = new Label {
FontSize = 56,
FontAttributes = FontAttributes.Bold,
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center
};
Content = _contentLabel;
}
static void TextChanged (BindableObject bindable, object oldValue, object newValue)
{
var view = (MyView)bindable;
view._contentLabel.Text = (newValue ?? "").ToString ();
}
}
}
XamlPage.xaml(主 UI 页面):
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sandbox="clr-namespace:FormsSandbox"
x:Class="FormsSandbox.XamlPage">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" Android="0" WinPhone="0"/>
</ContentPage.Padding>
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Text="1" Clicked="ButtonClicked" x:Name="Button1" Grid.Column="0" />
<Button Text="2" Clicked="ButtonClicked" x:Name="Button2" Grid.Column="1" />
<Button Text="3" Clicked="ButtonClicked" x:Name="Button3" Grid.Column="2" />
<sandbox:MyView Text="1" x:Name="View1" Grid.Row="1" Grid.ColumnSpan="3" />
<sandbox:MyView Text="2" x:Name="View2" Grid.Row="1" Grid.ColumnSpan="3" />
<sandbox:MyView Text="3" x:Name="View3" Grid.Row="1" Grid.ColumnSpan="3" />
</Grid>
</ContentPage>
XamlPage.xaml.cs:
using System;
using Xamarin.Forms;
namespace FormsSandbox
{
public partial class XamlPage : ContentPage
{
public XamlPage ()
{
InitializeComponent ();
SelectButton (Button1);
}
void SelectButton(Button button)
{
View view = null;
if (button == Button1)
view = View1;
if (button == Button2)
view = View2;
if (button == Button3)
view = View3;
View1.IsVisible = View1 == view;
View2.IsVisible = View2 == view;
View3.IsVisible = View3 == view;
Button1.TextColor = (Button1 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
Button2.TextColor = (Button2 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
Button3.TextColor = (Button3 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue;
Button1.BackgroundColor = (Button1 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
Button2.BackgroundColor = (Button2 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
Button3.BackgroundColor = (Button3 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1);
}
void ButtonClicked (object sender, EventArgs e)
{
SelectButton ((Button)sender);
}
}
}