我想知道如何将项目从主外壳页面传递到弹出页面。或者,或者,我想知道我是否可以在其弹出页面中简单地使用一个对象(它已经存在于主 shell 的范围内)。我猜在代码隐藏中有一些方法可以做到这一点。
具体来说,我有一个登录页面(它不是 shell 的一部分),一旦用户登录,它就会导航到主 shell。它将 Jason Web 令牌传递给主 shell。所以我在主shell中有我需要的东西,我只是不知道如何在弹出页面中使用它!
下面是一些代码来演示:
我有一个登录页面,其中包含一个接受用户名并将其传递给 shell 的函数:
async void SignInProcedure(System.Object sender, System.EventArgs e)
{
string name = nameEntry.Text;
if (name == "sally" || name == "Sally")
Application.Current.MainPage = new FlyoutMainShell(name);
else
{
await DisplayAlert("Login", "Login Failed", "Ok");
}
}
当然,我在 shell 的构造函数中有字符串:
using System;
using System.Collections.Generic;
using System.Windows.Input;
using Xamarin.Forms;
namespace ShellTest
{
public partial class FlyoutMainShell : Shell
{
public FlyoutMainShell(string name)
{
InitializeComponent();
}
}
}
现在我不确定如何在弹出项“page1”和“page2”中使用该字符串“name”,即在页面上简单地显示该名称。
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ShellTest.FlyoutMainShell"
xmlns:pages="clr-namespace:ShellTest">
<Shell.FlyoutHeaderTemplate>
<DataTemplate>
<Grid BackgroundColor="Gray" HeightRequest="200">
<Label Text="Here's the header!"
TextColor="White"
FontAttributes="Bold"
HorizontalOptions="Center"
VerticalOptions="Center" />
</Grid>
</DataTemplate>
</Shell.FlyoutHeaderTemplate>
<FlyoutItem x:Name="page1" Title="Go to Page 1">
<ShellContent ContentTemplate="{DataTemplate pages:Page1}"/>
</FlyoutItem>
<FlyoutItem x:Name="page2" Title="Go to Page 2">
<ShellContent ContentTemplate="{DataTemplate pages:Page2}" />
</FlyoutItem>
</Shell>
任何帮助是极大的赞赏!