【问题标题】:Include Dynamic Custom Xamarin Xaml View Control包括动态自定义 Xamarin Xaml 视图控件
【发布时间】:2020-10-05 16:01:40
【问题描述】:

您好,我为每个部门复制了一个重复的轮播,我想合并并制作成自定义的 xaml 视图控件。这是我在页面中多次复制的原始轮播视图。我想弄清楚如何用页面外部的东西来清理它。

<StackLayout Padding="10">

<Label Text="Human Resources" FontSize="Large"
        TextColor="#000000"></Label>
            
<CarouselView ItemsSource="{Binding HumanResourcesCollection}"
                PeekAreaInsets="75"
                IndicatorView="Indicator"
                HeightRequest="275">

    <CarouselView.ItemsLayout>

        <LinearItemsLayout Orientation="Horizontal" ItemSpacing="10"></LinearItemsLayout>

    </CarouselView.ItemsLayout>

    <CarouselView.EmptyView>

        <StackLayout>

            <Label Text="No results for this department"
                    FontSize="Large"
                    HorizontalOptions="Fill"
                    HorizontalTextAlignment="Center"></Label>

            <Label Text="Contact your manager for more information"
                    FontSize="Medium"
                    HorizontalOptions="Fill"
                    HorizontalTextAlignment="Center"></Label>

        </StackLayout>

    </CarouselView.EmptyView>

    <CarouselView.ItemTemplate>

        <DataTemplate>

            <StackLayout Padding="10">

                <helper:LayoutGradient StartColor="{StaticResource Secondary}"
                                        EndColor="{StaticResource Secondary}"
                                        OptionCorner="True"
                                        RadiusCorner="25"
                                        DirectionColor="False">

                    <Frame.GestureRecognizers>

                        <TapGestureRecognizer Tapped="OnDepartmentSelected></TapGestureRecognizer>

                    </Frame.GestureRecognizers>

                    <Frame.Content>

                        <Grid RowSpacing="0" ColumnSpacing="0">

                            <Grid.RowDefinitions>

                                <RowDefinition Height="*"></RowDefinition>

                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="*"></ColumnDefinition>

                            </Grid.ColumnDefinitions>

                            <Image Grid.Column="0" Grid.Row="0" Aspect="AspectFit" Opacity=".35" VerticalOptions="End" HorizontalOptions="End">

                                <Image.Source>

                                    <FontImageSource Size="148" Glyph="{Binding Comments}"
                                                        FontFamily="{DynamicResource FontIcons}"
                                                        Color="Blue"></FontImageSource>
                                </Image.Source>

                            </Image>

                            <StackLayout
                                            Grid.Row="0" Grid.Column="0">

                                <Label Text="{Binding Title}" FontSize="Medium" FontAttributes="Bold"
                                        ></Label>

                                <Label Text="{Binding Version}" FontSize="Small"></Label>

                            </StackLayout>
                        </Grid>

                                            
                                                
                    </Frame.Content>

                </helper:LayoutGradient>
                                    
                <Label Padding="0, 10, 0, 10" Text="{Binding Description}"
                        TextColor="#808080"
                        FontSize="Medium"></Label>

            </StackLayout>

        </DataTemplate>

    </CarouselView.ItemTemplate>

</CarouselView>

<IndicatorView x:Name="Indicator" IndicatorColor="LightBlue"
                SelectedIndicatorColor="DarkGray"></IndicatorView>

如何动态添加标题和收藏?我已显示轮播视图,但在涉及数据和属性时无法显示。

xmlns:control="clr-namespace:Program.Controls"

<control:CarouselControl Department="Human Resources" Collection="{Binding HumanResourcesCollection}"></control:CarouselControl>

<control:CarouselControl Department="Administration" Collection="{Binding AdministrationCollection}"></control:CarouselControl>

<control:CarouselControl Department="Operations" Collection="{Binding OperationsCollection}"></control:CarouselControl>

我的想法是否正确或离谱?一直在弄乱 ViewModel 中的属性,但对它的工作了解不足。谢谢大家。

public string Department {get; set;}

public ObservableCollectoin<DeparmentModel> Collection {get; set;}

【问题讨论】:

    标签: c# xaml xamarin mvvm


    【解决方案1】:

    由于您使用了 Custom ContentView ,您需要使用 bindable property 来绑定 ContentViewParent ContentPage 中的元素之间的值

    在 CarouselControl.xaml 中

    <?xml version="1.0" encoding="UTF-8" ?>
    <ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    //...
    x:Name="view" // set the name of CarouselControl
    >
    
    <StackLayout Padding="10">
    
    <Label Text="Human Resources" FontSize="Large"
            TextColor="#000000"></Label>
                
    <CarouselView ItemsSource="{Binding Source={x:Reference view}, Path=Collection}"
                    PeekAreaInsets="75"
                    IndicatorView="Indicator"
                    HeightRequest="275">
    
        <CarouselView.ItemsLayout>
    
            <LinearItemsLayout Orientation="Horizontal" ItemSpacing="10"></LinearItemsLayout>
    
        </CarouselView.ItemsLayout>
    
        <CarouselView.EmptyView>
    
            <StackLayout>
    
                <Label Text="No results for this department"
                        FontSize="Large"
                        HorizontalOptions="Fill"
                        HorizontalTextAlignment="Center"></Label>
    
                <Label Text="Contact your manager for more information"
                        FontSize="Medium"
                        HorizontalOptions="Fill"
                        HorizontalTextAlignment="Center"></Label>
    
            </StackLayout>
    
        </CarouselView.EmptyView>
    
    <CarouselView.ItemTemplate>
    
            <DataTemplate>
    
                <StackLayout Padding="10">
    
                    <helper:LayoutGradient StartColor="{StaticResource Secondary}"
                                            EndColor="{StaticResource Secondary}"
                                            OptionCorner="True"
                                            RadiusCorner="25"
                                            DirectionColor="False">
    
                        <Frame.GestureRecognizers>
    
                            <TapGestureRecognizer Tapped="OnDepartmentSelected></TapGestureRecognizer>
    
                        </Frame.GestureRecognizers>
    
                        <Frame.Content>
    
                            <Grid RowSpacing="0" ColumnSpacing="0">
    
                                <Grid.RowDefinitions>
    
                                    <RowDefinition Height="*"></RowDefinition>
    
                                </Grid.RowDefinitions>
    
                                <Grid.ColumnDefinitions>
    
                                    <ColumnDefinition Width="*"></ColumnDefinition>
    
                                </Grid.ColumnDefinitions>
    
                                <Image Grid.Column="0" Grid.Row="0" Aspect="AspectFit" Opacity=".35" VerticalOptions="End" HorizontalOptions="End">
    
                                    <Image.Source>
    
                                        <FontImageSource Size="148" Glyph="{Binding Comments}"
                                                            FontFamily="{DynamicResource FontIcons}"
                                                            Color="Blue"></FontImageSource>
                                    </Image.Source>
    
                                </Image>
    
                                <StackLayout
                                                Grid.Row="0" Grid.Column="0">
    
                                    <Label Text="{Binding Title}" FontSize="Medium" FontAttributes="Bold"
                                            ></Label>
    
                                    <Label Text="{Binding Version}" FontSize="Small"></Label>
    
                                </StackLayout>
                            </Grid>
    
                                                
                                                    
                        </Frame.Content>
    
                    </helper:LayoutGradient>
                                        
                    <Label Padding="0, 10, 0, 10" Text="{Binding Source={x:Reference view}, Path=Description}"
                            TextColor="#808080"
                            FontSize="Medium"></Label>
    
                </StackLayout>
    
            </DataTemplate>
    
        </CarouselView.ItemTemplate>
    
    </CarouselView>
    
    <IndicatorView x:Name="Indicator" IndicatorColor="LightBlue"
                    SelectedIndicatorColor="DarkGray"></IndicatorView>
    

    在 CarouselControl.xaml.cs 中

    public static readonly BindableProperty CollectionProperty =
                  BindableProperty.Create(nameof(Collection), typeof(ObservableCollection<YourModel>), typeof(CarouselControl));
    
            public ObservableCollection<YourModel> Collection
            {
                get => (ObservableCollection<YourModel>)GetValue(CollectionProperty );
                set => SetValue(CollectionProperty , value);
            }
    
    

    注意:这里的YourModel是包含CarouselView属性的mdoel,比如

    public class YourModel
        {
            public string Title { get; set; }
            public string Version { get; set; }
    
            //...other proerty in CarouselView
        }
    

    在内容页面中

    <control:CarouselControl Department="Human Resources" Collection="{Binding HumanResourcesCollection}"></control:CarouselControl>
    

    你只需要绑定Collection的来源,属性Title 在您初始化 HumanResourcesCollection 时已设置,因此您无需再次绑定它。

    这里有一些类似的案例,你可以参考一下

    How to bind a CommandParameter in a ContentView to an element in the parent ContentPage in Xamarin Forms

    When to use Xamarin bindings as opposed to passing objects to page constructors?

    【讨论】:

    • 好的有道理。几乎就像你有一个绑定上下文和一个私有和公共属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2011-10-17
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多