[英]Xamarin Forms Passing Data to Tabbed Pages
I'm collecting some data from a page and when the user taps a 'Next' button it navigates them to a tabbed container page, passing data that they input on the current to the tabbed container page.我正在从一个页面收集一些数据,当用户点击“下一步”按钮时,它会将他们导航到一个选项卡式容器页面,将他们在当前输入的数据传递到选项卡式容器页面。 But how can I access this data on the children tabbed pages of the container?
但是我怎样才能在容器的子标签页上访问这些数据呢?
The container page Xaml file:容器页面 Xaml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:me="clr-namespace:MyApp.Views"
x:Class="MyApp.Views.TransactionDetailsView">
<TabbedPage.Children>
<me:Page1TabView Title="Page 1" BindingContext="{Binding viewModel}"/>
<me:Page2TabView Title="Page 2"/>
</TabbedPage.Children>
</TabbedPage>
The first tabbed page Xaml file:第一个标签页 Xaml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyApp.Views.Page1">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding viewModel.Name}" TextColor="Black" FontSize="Large"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
You can create each ViewModel for Children Page, then contain them in the ViewModel of TabbedPage .您可以为子页面创建每个 ViewModel,然后将它们包含在TabbedPage的 ViewModel 中。
For example, create a MainTabbedPage and Xaml as follow:例如,创建MainTabbedPage和 Xaml 如下:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AppTabbedPageModelPass"
xmlns:local1="clr-namespace:AppTabbedPageModelPass.Views"
x:Class="AppTabbedPageModelPass.MainTabbedPage">
<!--Pages can be added as references or inline-->
<local:TabChildrenPage1 Title="Tab 1" BindingContext="{Binding ChildrenfirstViewModel}"/>
<local1:TabChildrenPage2 Title="Tab 2" BindingContext="{Binding ChildrensecondViewModel}" />
<local1:TabChildrenPage3 Title="Tab 3" BindingContext="{Binding ChildrenthirdViewModel}" />
</TabbedPage>
Then the ViewModel
of MainTabbedPage as follow:然后MainTabbedPage的
ViewModel
如下:
public class ViewModel
{
public ChildrenOneViewModel ChildrenfirstViewModel { set; get; }
public ChildrenTwoViewModel ChildrensecondViewModel { set; get; }
public ChildrenThreeViewModel ChildrenthirdViewModel { set; get; }
public ViewModel()
{
ChildrenfirstViewModel = new ChildrenOneViewModel();
ChildrensecondViewModel = new ChildrenTwoViewModel();
ChildrenthirdViewModel = new ChildrenThreeViewModel();
}
}
The ChildrenOneViewModel,ChildrenTwoViewModel,ChildrenThreeViewModel
is sample as follow: ChildrenOneViewModel,ChildrenTwoViewModel,ChildrenThreeViewModel
示例如下:
public class ChildrenOneViewModel
{
public string Text { get; set; }
public ChildrenOneViewModel()
{
Text = "First Childeren Page";
}
}
The Xaml of TabChildrenPage1,TabChildrenPage2,TabChildrenPage3
is: TabChildrenPage1 、
TabChildrenPage1,TabChildrenPage2,TabChildrenPage3
为:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppTabbedPageModelPass.TabChildrenPage1">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding Text}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Now when navigating to MainTabbedPage , pass viewmodel as follow:现在导航到MainTabbedPage时,按如下方式传递 viewmodel:
ViewModel viewModel = new ViewModel();
MainTabbedPage mainTabbedPage = new MainTabbedPage();
mainTabbedPage.BindingContext = viewModel;
Navigation.PushAsync(mainTabbedPage);
The effect:效果:
Here is the sample link .这是示例链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.