我知道這已經很長一段時間前答覆,但想通我對於任何可能在這個問題中碰到的人尋找更多的MVVM友好的方式來爲此添加更多信息。我最終得出了以下結論,如果他們如此傾向,有人會希望有用。
您將需要一個值轉換器,如以下幾點:
public class UseColorIfConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? parameter : Color.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
轉換器只是如果提供的參數計算結果爲true返回相應Color
。我也註冊了該轉換器在我的App.xaml(這不得不手動創建)一個靜態資源,即:
<?xml version="1.0" encoding="utf-8" ?>
<forms:FormsApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:Caliburn.Micro.Xamarin.Forms;assembly=Caliburn.Micro.Platform.Xamarin.Forms"
x:Class="Path.To.Application.App"
xmlns:converters="clr-namespace:Path.To.Converters.Namespace;assembly=Converter.Assembly">
<Application.Resources>
<converters:UseColorIfConverter x:Key="UseColorIf"></converters:UseColorIfConverter>
</Application.Resources>
</forms:FormsApplication>
請注意,我用的卡利微,但同樣會默認Xamarin合作.Forms.Application類也是如此。然後
轉換器和潛在的綁定的使用將是以下:
<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView BackgroundColor="{Binding Path=Selected, Converter={StaticResource UseColorIf}, ConverterParameter={x:StaticResource ListSelectionColor}}" ...>
<!--Display-->
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
這然後允許你綁定針對其指示它是否被選中或未被每一視圖模型的屬性。此屬性必須保持同步,但很輕鬆地通過訂閱屬性來完成改變的事件。例如:
public class MenuViewModel : Screen
{
public BindableCollection<SectionViewModel> Items { get; }
public MenuViewModel(IEnumerable<SectionViewModel> sections)
{
Items = new BindableCollection<SectionViewModel>(sections);
PropertyChanged += OnPropertyChanged;
}
private SectionViewModel _selectedItem;
public SectionViewModel SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem == value)
return;
_selectedItem = value;
NotifyOfPropertyChange(nameof(SelectedItem));
}
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
if (propertyChangedEventArgs.PropertyName == nameof(SelectedItem))
{
foreach (var item in Items)
{
item.Selected = item == SelectedItem;
}
}
}
}
也有這樣做後,剩下的一個瑣碎的事情,那就是在每個平臺上使用的默認渲染。我沒有檢查Android,但至少在IOS你仍然會得到一個灰色邊框所以下面只是刪除它:
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomListViewCellRenderer))]
namespace My.Awesome.Ios.Client.Renderers
{
class CustomListViewCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
你見過這樣的:http://forums.xamarin.com/discussion/19500/how-to-change-background-color-on-a-listview –
多數民衆贊成不是什麼即時通訊尋找我想要改變顏色上藍色的默認是藍色 –
我相信你將不得不在風格的實際平臺代碼。 – Johan