Привязки теряются внутри дочернего пользовательского контроля - PullRequest
0 голосов
/ 28 апреля 2020

Я пытался создать свой собственный вид вкладок в Xamarin.Forms. Все вкладки функциональны, но мои дочерние привязки ViewModel потеряны в дочернем элементе TabView.

Пример использования:

<views:MvxContentPage x:Class="LoginPage" 
                      ...>
<controls:TabView SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
    <controls:TabView.ItemSource>
        <controls:TabItem HeaderText="Sign Up">
            ...
        </controls:TabItem>
        <controls:TabItem HeaderText="Login">
            <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                 <!--  Email  -->            
                 <Entry Placeholder="Enter email address"
                        Text="{Binding LoginEmail, Mode=TwoWay}"
                        Keyboard="Email" />

                 <!--  Password -->
                 <Entry Placeholder="Enter password"
                        Text="{Binding LoginPassword, Mode=TwoWay}"
                        Keyboard="Email" />

                 <!--  Submit -->
                 <Button Text="Login"
                         Command="{Binding LoginCommand}" />
             </ScrollView>
        </controls:TabItem>
    </controls:TabView.ItemSource>
</controls:TabView>
</views:MvxContentPage>

TabView.xaml

<views:MvxContentView x:Class="ClassName"
                      xmlns="http://xamarin.com/schemas/2014/forms"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms">
    <StackLayout x:Name="Wrapper">
        <FlexLayout x:Name="Head" Direction="Row" />

        <CarouselView x:Name="CarouselView"
                      ItemsSource="{Binding ItemSource}"
                      Position="{Binding SelectedIndex}">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <ContentView Content="{Binding Content}" />
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </StackLayout>
</views:MvxContentView>

TabView.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TabView : MvxContentView
{
    public TabView()
    {
        InitializeComponent();
        Wrapper.BindingContext = this;             
    }

    public static readonly BindableProperty ItemSourceProperty = 
          BindableProperty.Create(nameof(ItemSource), typeof(ObservableCollection<TabItem>), typeof(TabView), null);
    public ObservableCollection<TabItem> ItemSource
    {
        get { return (ObservableCollection<TabItem>)GetValue(ItemSourceProperty); }
        set { SetValue(ItemSourceProperty, value); }
    }

    public static readonly BindableProperty SelectedIndexProperty = 
          BindableProperty.Create(nameof(SelectedIndex), typeof(int), typeof(TabView), 0, BindingMode.TwoWay);
    public int SelectedIndex
    {
        get { return (int)GetValue(SelectedIndexProperty); }
        set { SetValue(SelectedIndexProperty, value); }
    }
}

TabItem.cs

[ContentProperty(nameof(Content))]
public class TabItem : BindableObject
{
    public TabItem() { }

    public static readonly BindableProperty HeaderTextProperty = 
           BindableProperty.Create(nameof(HeaderText), typeof(string), typeof(TabItem), string.Empty);
    public string HeaderText
    {
        get { return (string)GetValue(HeaderTextProperty); }
        set { SetValue(HeaderTextProperty, value); }
    }

    public static readonly BindableProperty ContentProperty = 
           BindableProperty.Create(nameof(Content), typeof(View), typeof(TabItem));
    public View Content
    {
        get => (View)GetValue(ContentProperty);
        set { SetValue(ContentProperty, value); }
    }
}
...