在xamarin.forms MVVM模式中进行绑定的最佳实践是什么?

时间:2019-04-15 09:14:43

标签: xaml mvvm xamarin.forms binding

在以前的程序中,我始终使用x:name进行绑定,但是在这种情况下,我必须避免这种方式

这是我的查看代码(HomePage.xaml)

    <ListView 
        //x:Name="MyList"
        //ItemsSource="{Binding MyList}"
        HasUnevenRows="False"
        Grid.Row="2"
        SelectionMode="None"
        RowHeight="190"
        Margin="0,0,0,20"
        SeparatorVisibility="None"
        HeightRequest="0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame OutlineColor="Accent"
                           HasShadow="True"
                           Grid.ColumnSpan="5"
                           HorizontalOptions="FillAndExpand"
                           VerticalOptions="FillAndExpand"
                           BackgroundColor="#f4f4f4" 
                           BorderColor="LightGray"
                           CornerRadius="10"
                           Margin="25,10,25,0"
                           Padding="0">
                        <Grid
                            VerticalOptions="FillAndExpand"
                            HorizontalOptions="FillAndExpand"
                            >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="5"/>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition Width="5"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="13"/>
                                <RowDefinition Height="28"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="30"/>
                            </Grid.RowDefinitions>

                            <BoxView Color="#33A8F7" 
                                     Grid.Row="0" 
                                     Grid.ColumnSpan="6" 
                                     HorizontalOptions="FillAndExpand" 
                                     VerticalOptions="StartAndExpand"/>

                            <Image Source="service_irrig_img.png"
                                   Grid.Column="1"
                                   Grid.Row="2"
                                   Grid.RowSpan="2"
                                   Margin="0"/>

                            <Image Source="location.png"
                                   Grid.Column="2"
                                   Grid.Row="1"
                                   HorizontalOptions="Start"
                                   VerticalOptions="EndAndExpand"
                                   HeightRequest="16"
                                   WidthRequest="16"/>
                            <Label Text="irrigNET"
                                   FontFamily="{StaticResource BalooBhai}"
                                   FontSize="16"
                                   Grid.Column="3"
                                   Grid.Row="1"
                                   Grid.ColumnSpan="2"
                                   VerticalTextAlignment="Center"
                                   HorizontalTextAlignment="Start"
                                   Margin="0,8,0,0"
                                   TextColor="#262f41"/>

                            <Image Source="service_arrow.png"
                                   Grid.Column="4"
                                   Grid.Row="2"
                                   VerticalOptions="Center"
                                   HorizontalOptions="Center"
                                   HeightRequest="18"
                                   WidthRequest="18"/>

                            <Image Source="clock.png"
                                   Grid.Column="2"
                                   Grid.Row="3"
                                   HorizontalOptions="Center"
                                   VerticalOptions="Start"
                                   Margin="0,0,0,5"/>


                            <Label Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
                                   Grid.Column="3"
                                   Grid.Row="2"
                                   VerticalTextAlignment="Start"
                                   HorizontalOptions="FillAndExpand"
                                   VerticalOptions="FillAndExpand"
                                   TextColor="#262f41"
                                   Margin="0,10,0,0"/>
                        </Grid>
                    </Frame>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

仅出于测试目的,在我的HomePage.cs中 我用了 List<string> MylistItems = new List<string> { "element1", "element2", "element3" }; MyList.ItemsSource = MylistItems; 并且一切正常,ListView会以正确的方式填充。  但是,现在我想使用MVVM模式获得相同的输出,避免使用x:name。

0 个答案:

没有答案