首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将xamarin.forms.shell重置为“后退”按钮上的初始页

将xamarin.forms.shell重置为“后退”按钮上的初始页
EN

Stack Overflow用户
提问于 2020-10-05 09:53:11
回答 2查看 5.9K关注 0票数 3

很短的版本是,我想,在代码中,让shell回到原来的位置,以及它的外观,当它第一次显示在app start上时。

我有一个xamarin.forms.shell页面,它显示了许多内容页面,使用的是跳出和选项卡。当shell最初加载时,它将显示第一个反出项内容页,这是我的具有最常见UI的“主活动/页”。

如果我转到另一个反飞项目内容页面,我希望返回按钮导航到“主飞出”,并且只有当后退按钮点击这个“主飞出”时,如果它离开应用程序的话。

虽然我可以拦截后退按钮并导航到其他页面,但它并没有点击“主页”的效果。如果我做了

代码语言:javascript
复制
Shell.Current.GoToAsync(name of route to my main flyout);

我确实进入了主页,但随着全屏幕和飞出菜单、工具栏和选项卡的消失。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-06 07:02:38

为此,您需要在所需的内容页中重写后退按钮行为,方法是定义一个命令,当用户按下该页面中的后退按钮时,该命令将被触发,然后通过它定义的根导航到您的根页面:

CatsPage.Xaml

代码语言:javascript
复制
<Shell.BackButtonBehavior>
   <BackButtonBehavior Command="{Binding BackCommand}"/>
</Shell.BackButtonBehavior>

代码隐藏或ViewModel

代码语言:javascript
复制
public ICommand BackCommand { get; private set; }

public CatsPage()  //or VM constructor
{
    ...
BackCommand = new Command(async (x) => await Shell.Current.GoToAsync("///MainRoute"));
}

如果在多个Tab中将此内容页用作FlyoutItem内容,但只希望此行为在驻留在第一个FlyoutItem中的ContentPage上工作,则可以根据页面绝对路由或Title属性添加条件测试:

江的Xaml实例回答:

代码语言:javascript
复制
<FlyoutItem Route="MainRoute"
            Title="Animals"
            FlyoutDisplayOptions="AsMultipleItems">
    <Tab Title="MainPage"
         Route="MainPage"
         Icon="paw.png">
        <ShellContent Route="cats"
                      Style="{StaticResource DomesticShell}"
                      Title="Cats"
                      Icon="cat.png"
                      ContentTemplate="{DataTemplate views:CatsPage}" />
        <ShellContent Route="dogs"
                      Style="{StaticResource DomesticShell}"
                      Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" />
    </Tab>

    <ShellContent Route="PageTwo"
                  Style="{StaticResource MonkeysShell}"
                  Title="Monkeys"
                  Icon="monkey.png"
                  ContentTemplate="{DataTemplate views:MonkeysPage}" />

    <ShellContent Route="cats"
                  Style="{StaticResource DomesticShell}"
                  Title="Cats2"
                  Icon="cat.png"
                  ContentTemplate="{DataTemplate views:CatsPage}" />
</FlyoutItem>

代码隐藏或ViewModel

代码语言:javascript
复制
public CatsPage() //or VM constructor
{
    ...
    BackCommand = new Command(async (x) => {
    if (Shell.Current.CurrentState.Location.ToString().Equals("//MainRoute/MainPage/Cats"))
      //popup to quit
else if (Shell.Current.CurrentState.Location.ToString().Equals("//MainRoute/Cats"))
        await Shell.Current.GoToAsync("///MainRoute");
    // or use to go back to previous: await Shell.Current.GoToAsync("..");
   });
}

再次,您需要正确定义和使用您的Tab**s和** FlyoutItem 路由层次结构,否则将导致异常

票数 3
EN

Stack Overflow用户

发布于 2020-10-06 02:36:46

如果我转到另一个反飞项目内容页面,我希望返回按钮导航到“主飞出”,并且只有当后退按钮点击这个“主飞出”时,如果它离开应用程序的话。

当每个页面显示以确定它是否是主页时,您可以存储一个页面标志类型。页面标志类型只需要定义两种类型:--0(意为主页),--另一种1(其他页面)。然后,当按下物理后退按钮时,您可以确定是否需要显示退出消息才能退出应用程序。

例如,使用Xamarin.Essentials:首选项在每个页面的OnAppearing方法中存储标志:

MainPage:

代码语言:javascript
复制
public partial class MainPage: ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Preferences.Set("PageType", "0");
    }
}

OtherPage:

代码语言:javascript
复制
public partial class OtherPage : ContentPage
{
    public OtherPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Preferences.Set("PageType", "1");
    }
}

然后,当单击“后退”按钮时,我们可以确定我们需要做什么。

代码语言:javascript
复制
private async void Button_Clicked(object sender, EventArgs e)
{
    var pageType = Preferences.Get("PageType", "0");
    if (pageType == "0")
    {
        // Exit application method
        ExitApplication();
    }
    else
    {
        // back to the Main Page
        await Shell.Current.GoToAsync($"//MainRoute/MainPage");
    }
}

下面是Xaml代码FlyoutItem

代码语言:javascript
复制
<FlyoutItem Route="MainRoute"
            Title="Animals"
            FlyoutDisplayOptions="AsMultipleItems">
    <Tab Title="MainPage"
         Route="MainPage"
         Icon="paw.png">
        <ShellContent Route="cats"
                      Style="{StaticResource DomesticShell}"
                      Title="Cats"
                      Icon="cat.png"
                      ContentTemplate="{DataTemplate views:CatsPage}" />
        <ShellContent Route="dogs"
                      Style="{StaticResource DomesticShell}"
                      Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" />
    </Tab>

    <ShellContent Route="PageTwo"
                  Style="{StaticResource MonkeysShell}"
                  Title="Monkeys"
                  Icon="monkey.png"
                  ContentTemplate="{DataTemplate views:MonkeysPage}" />
    <ShellContent Route="PageThree"
                  Style="{StaticResource ElephantsShell}"
                  Title="Elephants"
                  Icon="elephant.png"
                  ContentTemplate="{DataTemplate views:ElephantsPage}" />
    <ShellContent Route="PageFour"
                  Style="{StaticResource BearsShell}"
                  Title="Bears"
                  Icon="bear.png"
                  ContentTemplate="{DataTemplate views:BearsPage}" />
</FlyoutItem>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64206312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档