将平台特定控件设置为xamarin表单中的一个平台

时间:2016-09-13 08:09:11

标签: xaml xamarin xamarin.forms

我有一个包含listviewSearch bar控件的页面。 android有一个自定义listview。我的代码如下:

<ContentPage.Content>
            <ContentView>
            <OnPlatform x:TypeArguments="View">
                    <OnPlatform.iOS>
                        <StackLayout Spacing="0">
                            <SearchBar x:Name="IosSearch"
                                   Placeholder="Search"
                                   TextChanged="IosSearchBar_OnTextChanged"
                                   SearchButtonPressed="OnSearch" BackgroundColor="#19588F">
                            </SearchBar>   
                            <ListView x:Name="EmpListViewIos"
                                      ItemsSource="{Binding EmpModel.GroupedItems}"
                                      ItemSelected="OnItemSelected"
                                      SeparatorVisibility="Default">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout Padding="8">
                                                <StackLayout Spacing="4" Orientation="Horizontal">
                                                    <Label Text="{Binding FullName}" FontSize="15"  TextColor="Gray" LineBreakMode="NoWrap"/>
                                                </StackLayout>
                                                <StackLayout Spacing="4" Orientation="Horizontal">
                                                    <Label Text="{Binding Department}" FontSize="Small" TextColor="#F68933" LineBreakMode="WordWrap"/>
                                                </StackLayout>
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>            
                            </ListView>
                        </StackLayout>
                    </OnPlatform.iOS>
                    <OnPlatform.Android>
                        <StackLayout x:Name="androidListView">  
                            <SearchBar x:Name="DroidSearch"
                                   Placeholder="Search"
                                   TextChanged="DroidSearchBar_OnTextChanged"
                                   SearchButtonPressed="OnSearch" BackgroundColor="#19588F">
                            </SearchBar>
                            <local:CustomListView x:Name="customListView"
                                  Items="{Binding EmpModel.Items}"
                                  VerticalOptions="FillAndExpand" MyListItemSelected="nativeListView_ItemSelected"
                                  SeparatorColor="Black">
                            </local:MyListView>
                        </StackLayout>
                    </OnPlatform.Android>
                    <OnPlatform.WinPhone>
                        <StackLayout Spacing="0">
                        <SearchBar x:Name="WinSearch"
                               Placeholder="Search"
                               TextChanged="WinSearchBar_OnTextChanged"
                               SearchButtonPressed="OnSearch" BackgroundColor="#19588F">
                        </SearchBar>
                             <ListView x:Name="EmpListViewWin"
                                      ItemsSource="{Binding EmpModel.GroupedItems}"
                                      ItemSelected="OnItemSelected"
                                      SeparatorVisibility="Default">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout Padding="8">
                                                <StackLayout Spacing="4" Orientation="Horizontal">
                                                    <Label Text="{Binding FullName}" FontSize="15"  TextColor="Gray" LineBreakMode="NoWrap"/>
                                                </StackLayout>
                                                <StackLayout Spacing="4" Orientation="Horizontal">
                                                    <Label Text="{Binding Department}" FontSize="Small" TextColor="#F68933" LineBreakMode="WordWrap"/>
                                                </StackLayout>
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>                
                            </ListView>
                        </StackLayout>
                    </OnPlatform.WinPhone>
                </OnPlatform>
            </ContentView>
        </ContentPage.Content>
    </ContentPage>

因为数据的表示是不同的,因为根据它的布局定义,我需要custom listview用于android。上面的代码工作正常。唯一的问题是冗余,搜索栏控件对于所有平台都是通用的,但是当我将搜索栏控件放在<ContentView>之上时它不起作用会抛出异常。同样地,ListViewios共有windowscustom listview只有android,但我不得不重复与{android相同的代码1}}。

任何人都可以提出一种更有效的方法来实现这一目标。如何仅针对android应用特定于平台的条件,针对windowsios应用此特定条件。

1 个答案:

答案 0 :(得分:0)

您可以使用静态资源分享iOSWinPhone个观看次数:

<ContentPage>
  <ContentPage.ResourceDictionary>
    <StackLayout x:Key="iosOrWpView">
      ...
    </StackLayout>
  </ContentPage.ResourceDictionary>
  <ContentView>
    <OnPlatform x:TypeArguments="View" iOS="{StaticResource iosOrWpView}" 
                WinPhone="{StaticResource iosOrWpView}">
      <OnPlatform.Android>
        <StackLayout x:Name="androidListView">  
          ...
        </StackLayout>
      </OnPlatform.Android>
    </OnPlatform>
  </ContentView>
<ContentPage>