【问题标题】:Access controls in Xamarin FormsXamarin 表单中的访问控制
【发布时间】:2020-04-23 14:36:50
【问题描述】:

我是 Xamarin Forms 的新手。

我想在完成特定阶段后在我的应用中启用ShellSection(标签)。

目前我使用AppShell 类来处理导航控件(目前没有启用 PageOneTab)-

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:MyApp="clr-namespace:MyApp"
             x:Class="MyApp.AppShell"
             FlyoutBehavior="Disabled"
             Navigating="Handle_Navigating">
    <ShellItem>

        <ShellSection x:Name="HomeTab" Icon="home">
            <ShellContent>
                <MyApp:HomePage/>
            </ShellContent>
        </ShellSection>

        <ShellSection x:Name="PageOneTab" x:FieldModifier="public"  IsEnabled="False">
            <ShellContent>
                <MyApp:PageOnePage/>
            </ShellContent>
        </ShellSection>

    </ShellItem>
</Shell>

HomePage ContentPage 我有一个按钮点击-

public HomePage()
{
    InitializeComponent();

}

private async void HomePageButton_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new HandlingPage());
}

而在我的HandlingPageContentPage 基于结果,我想启用ShellSectionPageOneTab

public HandlingPage()
{
    InitializeComponent();

    // do stuff....

    // then set -
    StatusTab.IsEnabled = true;
}

我认为通过将x:FieldModifier="public" 添加到控件中,我可以从HandlingPage 访问它,但是它不可用,事实上,我认为目前我唯一可以访问控件的地方是在后面的代码中AppShell.xaml.cs 通过执行 StatusTab.IsEnabled = true;。 我相信这可能归结为BindingContext,但不确定。

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    如果你想改变一个ShellSection的IsEnable状态,我建议你可以使用MessageCenter来做到这一点。

    我的 ShellItems:

     <ShellItem>
        <ShellSection
            x:Name="itempage"
            Title="Browse"
            Icon="tab_feed.png">
            <ShellContent>
                <local:ItemsPage />
            </ShellContent>
        </ShellSection>
        <ShellSection
            x:Name="aboutpage"
            Title="About"
            Icon="tab_about.png"
            IsEnabled="False">
            <ShellContent>
                <local:AboutPage />
            </ShellContent>
        </ShellSection>
    </ShellItem>
    

    并在 AppShell.cs 中订阅消息:

     public AppShell()
        {
            InitializeComponent();
    
            MessagingCenter.Subscribe<AppShell>(this, "Hi", (sender) =>
            {
                aboutpage.IsEnabled = true;
            });
        }
    

    在HandlingPage Contentpage中发送一条消息:

     private void btn1_Clicked(object sender, EventArgs e)
        {
            MessagingCenter.Send<AppShell>(new AppShell(),"Hi");
        }
    

    这是关于MessageCenter的文章,你可以看看:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

    【讨论】:

    • 太棒了,这是一种享受。请问使用这种方法是好的设计实践吗?
    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多