Xamarin.Forms Shell 将数据传递到弹出项视图 [英] Xamarin.Forms Shell Passing Data to Flyout Item Views

查看:39
本文介绍了Xamarin.Forms Shell 将数据传递到弹出项视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将项目从主 shell 页面传递到弹出页面.或者,我想知道我是否可以在其弹出页面中简单地使用一个对象(它已经存在于主 shell 的范围内).我猜在代码隐藏中有一些方法可以做到这一点.

I would like to know how to pass an item from the main shell page to flyout pages. Or, alternatively, I would like to know if I can simply use an object (which already exists in the scope of the main shell) in its flyout pages. I'm guessing there's some way to do this in the code-behind.

具体来说,我有一个登录页面(它不是 shell 的一部分),一旦用户登录,它就会导航到主 shell.它将 Jason Web 令牌传递给主 shell.所以我在主 shell 中有我需要的东西,我只是不知道如何在弹出页面中使用它!

Specifically, I have a login page (which is not a part of the shell) which navigates to the main shell once a user logs in. It passes a Jason web token to the main shell. So I have what I need in the main shell, I just don't know how to use it in the flyout pages!

这里有一些代码来演示:

Here is some code to demonstrate:

我有一个登录页面,其中的函数接受用户名并将其传递给 shell:

I have a login page with a function that takes a username and passes it to the 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 的构造函数中有字符串:

And of course, I have the string in the constructor for the 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",即简单地在页面上显示该名称.

Now I'm not sure how to use that string 'name' in flyout items 'page1' and 'page2', i.e. to simply display that name on the page.

<?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>

非常感谢任何帮助!

推荐答案

您可以使用 Xamarin.Essentials: Preferences 将数据存储在 Main Shell 并在page1"和page2"中获取/使用它:

You can use Xamarin.Essentials: Preferences to store the data at Main Shell and get/use it in the 'page1' and 'page2':

在 Main Shell 中,存储数据:

In Main Shell, store the data:

public FlyoutMainShell(string name)
{
    InitializeComponent();

    Preferences.Set("uesr_name", name);

}

在其他页面,获取值:

public Page1()
{
    InitializeComponent();

    var myValue = Preferences.Get("uesr_name", "default_value");

}

并且您可以在用户注销时删除该值:

And you can remove the value when the user logout:

Preferences.Remove("uesr_name");

这篇关于Xamarin.Forms Shell 将数据传递到弹出项视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆