問題
有些 XAML 的屬性是個列舉,可以設定多個列舉值,例如, FontAttributes 就是其中一列,可是,當我需要在 XAML 中定義多個列舉值的時候,我該如何定義呢?
解答
您可以參考底下用法,多個列舉值,可以使用逗號
,
將其分開即可 <Label Text="多奇數位創意有限公司" FontAttributes="Italic,Bold"/>
,
將其分開即可 <Label Text="多奇數位創意有限公司" FontAttributes="Italic,Bold"/>


這樣的表示方式。 <Label Text="多奇數位創意有限公司
Xamarin" />
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="XFListView.Views.PullToRefreshPage"
x:Name="ThisPage"
Title="下拉更新" >
CommandParameter="{Binding}"
,將這筆紀錄回傳到 ICommand 命令內。 <BoxView
BackgroundColor="White" >
<BoxView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.點擊Command, Source={x:Reference ThisPage}}" CommandParameter="{Binding}" />
</BoxView.GestureRecognizers>
</BoxView>
<?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="XFListViewRefresh.Views.MainPage"
Title="ListView下拉更新">
<StackLayout
Margin="20"
HorizontalOptions="Fill" VerticalOptions="Fill">
<Label Text="{Binding Title}" />
<ListView
HorizontalOptions="Fill" VerticalOptions="FillAndExpand"
ItemsSource="{Binding DataItemList}"
IsPullToRefreshEnabled="True"
HasUnevenRows="True"
RefreshCommand="{Binding ListView更新資料Command}"
IsRefreshing="{Binding IsBusy}"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid
RowSpacing="0" ColumnSpacing="0"
>
<Label Text="{Binding DataVale}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
ListView更新資料Command = new DelegateCommand(() =>
{
IsBusy = true;
Refresh();
IsBusy = false;
});
<ListView
ItemsSource="{Binding People}"
>
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*" />
<ColumnDefinition Width="0.3*" />
<ColumnDefinition Width="0.3*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}" TextColor="{Binding ShowColor}" FontAttributes="Bold" />
<Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="{Binding ShowColor}" />
<Label Grid.Column="2" Text="{Binding Location}" TextColor="{Binding ShowColor}" HorizontalTextAlignment="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
OnNavigatedTo
方法會被執行,此時,我們將會進行要顯示在 ListView 內所有物件的值,就在此時,我們判斷每筆紀錄的生日欄位,若出生年份大於 1980,則會設定 ShowColor
這個屬性值為綠色,否則,設定為紅色。經過這樣處理,ListView 內的每筆紀錄文字,就會顯示不同的顏色了。 public void OnNavigatedTo(NavigationParameters parameters)
{
People = new ObservableCollection<Person> {
new Person ("Kath", new DateTime (1985, 11, 20), "France"),
new Person ("Steve", new DateTime (1975, 1, 15), "USA"),
new Person ("Lucas", new DateTime (1988, 2, 5), "Germany"),
new Person ("John", new DateTime (1976, 2, 20), "USA"),
new Person ("Tariq", new DateTime (1987, 1, 10), "UK"),
new Person ("Jane", new DateTime (1982, 8, 30), "USA"),
new Person ("Tom", new DateTime (1977, 3, 10), "UK")
};
foreach (var item in People)
{
if(item.DateOfBirth.Year >= 1980)
{
item.ShowColor = Color.Green;
}
else
{
item.ShowColor = Color.Red;
}
}
}
TextCell
/ ImageCell
這兩種預設的紀錄顯示方式,若想要自行定義每筆 ListView 的紀錄所顯示的資料樣式,例如,想要加入 Switch / 按鈕到每筆紀錄上,這個時候,有甚麼解法呢?ViewCell
來定義每筆紀錄要顯示的資料格式;例如,在底下的範例中,我們在 ItemTemplate
> DataTemplate
內,使用了 ViewCell
來定義每筆紀錄要出現的樣貌。 <ListView
ItemsSource="{Binding People}"
>
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*" />
<ColumnDefinition Width="0.3*" />
<ColumnDefinition Width="0.3*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}" TextColor="Green" FontAttributes="Bold" />
<Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="Green" />
<Label Grid.Column="2" Text="{Binding Location}" TextColor="Green" HorizontalTextAlignment="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="立即產生"
Command="{Binding 右鍵功能表Command}" CommandParameter="立即產生" />
<MenuItem Text="刪除"
Command="{Binding 右鍵功能表Command}" CommandParameter="刪除"
IsDestructive="True" />
</ViewCell.ContextActions>
ViewCell.ContextActions
Property Element 表示方式,使用 MenuItem
標記來宣告,當使用者要使用彈出功能錶的時候,要出現甚麼樣的選項內容。 <ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="立即產生"
Command="{Binding 立即產生Command}" CommandParameter="{Binding .}" />
<MenuItem Text="刪除"
Command="{Binding 刪除Command}" CommandParameter="{Binding .}"
IsDestructive="True" />
</ViewCell.ContextActions>