[英]Access controls in Xamarin Forms
I am a newbie to Xamarin Forms.我是 Xamarin Forms 的新手。
I want to enable a ShellSection
(tab) in my app once a specific stage has been completed.完成特定阶段后,我想在我的应用程序中启用ShellSection
(选项卡)。
Currently I use an AppShell
class to handle navigation controls (currently PageOneTab is not enabled) -目前我使用AppShell
class 来处理导航控件(当前未启用 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>
In the HomePage
ContentPage I have a button click -在HomePage
ContentPage 我有一个按钮单击 -
public HomePage()
{
InitializeComponent();
}
private async void HomePageButton_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new HandlingPage());
}
And in my HandlingPage
ContentPage based on the result, I want to enable the ShellSection
PageOneTab
.并且在基于结果的HandlingPage
ContentPage 中,我想启用ShellSection
PageOneTab
。
public HandlingPage()
{
InitializeComponent();
// do stuff....
// then set -
StatusTab.IsEnabled = true;
}
I thought by adding x:FieldModifier="public"
to the control I would be able to access it from the HandlingPage
however it is not available, in fact the only place I can access the control currently I believe is in the code behind of AppShell.xaml.cs
by doing StatusTab.IsEnabled = true;
我想通过将x:FieldModifier="public"
添加到控件中,我可以从HandlingPage
访问它但是它不可用,事实上,我认为目前唯一可以访问控件的地方是AppShell.xaml.cs
背后的代码AppShell.xaml.cs
通过执行StatusTab.IsEnabled = true;
. . I believe this may be down to BindingContext
however not sure.我相信这可能归结为BindingContext
但不确定。
If you want to change one ShellSection's IsEnable status, I suggest you can use MessageCenter to do this.如果你想改变一个 ShellSection 的 IsEnable 状态,我建议你可以使用MessageCenter来做到这一点。
My ShellItems:我的 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>
And subscribe to a message in AppShell.cs:并在 AppShell.cs 中订阅一条消息:
public AppShell()
{
InitializeComponent();
MessagingCenter.Subscribe<AppShell>(this, "Hi", (sender) =>
{
aboutpage.IsEnabled = true;
});
}
Sending one message in HandlingPage Contentpage:在HandlingPage Contentpage 中发送一条消息:
private void btn1_Clicked(object sender, EventArgs e)
{
MessagingCenter.Send<AppShell>(new AppShell(),"Hi");
}
This is the article about MessageCenter , you can take a look:这是关于MessageCenter的文章,你可以看看:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.