不確定是否適當地設置了問題的格式,請讓我知道如果沒有。但我試圖簡單地將背景顏色綁定到我的視單元中的值。實際上,我有這個工作。問題是當我更新一個值時,我看不到背景顏色的變化。實現有點複雜,但這是我的代碼。嘗試在OnBindingContextChanged中使用ListView(回收緩存策略)的值轉換器
ViewCell(OnBindingContextChanged)
...
ShowReadOverlay.SetBinding(Xamarin.Forms.VisualElement.BackgroundColorProperty, new Xamarin.Forms.Binding(".", Xamarin.Forms.BindingMode.TwoWay, new XamarinMobile.Converters.GridCellBackgroundColorConverter(), null, null, null));
...
所以基本上我只是建立我的佈局。我決定只發布在我的OnBindingContextChanged
方法中設置綁定的相關代碼。如果任何人需要任何其他代碼,我很樂意添加它,只是不知道它是否相關。我的ViewCell類是一個簡單的類,它繼承了ViewCell
。
這裏是我的轉換器:
public class GridCellBackgroundColorConverter : Xamarin.Forms.IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
try
{
var cell = (XamarinMobile.ViewModels.GridCellViewModel)value;
if(cell.HasRead)
{
//return with shadow
return Xamarin.Forms.Color.FromRgba(0,0,0,0.6);
} else
{
//return no shadow
return Xamarin.Forms.Color.FromRgba(0, 0, 0, 0.0);
}
} catch(System.Exception ex)
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
簡單。有用。現在,這是棘手的部分。所以我描述的網格是一個包含故事單元的列表視圖。用戶將點擊一個圖像,將他們帶到故事頁面。當用戶在故事頁面中時,他們可以回到網格去看另一個故事,或者向左或向右滑動,然後他們可以通過這種方式獲得另一個故事。當用戶從我們的網格轉到故事頁面時,單元格會正常更新。但是如果用戶不是從網格滑動到另一個故事,那就是我的問題所在。在我的故事頁面中,我有邏輯遍歷網格單元格,並找到您當前所在的故事(您所滑動的故事)並查看它是否在網格中,如果它在網格中,我更新單元格的HasRead
屬性。因此:
//find the cell in the grid (if exists)
ViewModels.GridCellViewModel cell = App.GridCells.Where(x => x.StoryId == App.Story.StoryId).FirstOrDefault();
if (cell != null)
{
cell.HasRead = true;
}
這可以工作,但是...它不會觸發值轉換器來更改屬性。我究竟做錯了什麼?我怎樣才能得到它,以便我可以更新一個屬性,並讓它觸發我的價值轉換器?
什麼?該死的..甚至沒有關於如何通過onbindingcontext更改綁定視單元的一點見解? – jdmdevdotnet