在我的xaml中,我使用q转换器检查AlarmStatus
true
或false
。当用户编辑现有警报时,它可以正常工作,但是当用户创建新警报时,属性AlarmStatus
不存在,并且未设置背景颜色。
如果属性AlarmStatus
存在,是否可以签入xaml?如果没有,则应将颜色设置为红色。
public class AlarmModel
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(255)]
public string Title { get; set; }
[MaxLength(255)]
public string Content { get; set; }
public DateTime TimeCreate { get; set; }
public DateTime AlarmTime { get; set; }
public bool AlarmStatus { get; set; }
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AlarmView : TabbedPage
{
private AlarmModel CurrentAlarm;
//if user create a new alarm then no argument pass in constructor
public AlarmView (AlarmModel alarm = null)
{
InitializeComponent ();
if (alarm != null)
{
this.CurrentAlarm = alarm;
}
BindingContext = CurrentAlarm;
}
}
<StackLayout VerticalOptions="FillAndExpand">
<Entry x:Name="titleInput" Text="{Binding Title}" HeightRequest="50" FontSize="20"/>
<Editor x:Name="contentInput" Text="{Binding Content}" VerticalOptions="FillAndExpand" />
<Button Text="Start" BackgroundColor="{Binding AlarmStatus,Converter={StaticResource ColorConverter}}" Clicked="Button_Clicked"/>
</StackLayout>
class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value==null)return Color.Black;
bool b = ((bool)value);
return b ? Color.Green : Color.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
答案 0 :(得分:0)
将AlarmModel.AlarmStatus
属性更新为可为空的布尔值,即bool?
,以允许您描述的三种状态。
public class AlarmModel {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(255)]
public string Title { get; set; }
[MaxLength(255)]
public string Content { get; set; }
public DateTime TimeCreate { get; set; }
public DateTime AlarmTime { get; set; }
public bool? AlarmStatus { get; set; } //<-- needs to be nullable for 3 states
}
转换器预计会bool?
public class ColorConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var status = (bool?) value;
if (status == null) return Color.Black;
return status.Value ? Color.Green : Color.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您还需要确保在创建新警报时有一个要绑定的模型。
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AlarmView : TabbedPage {
private AlarmModel CurrentAlarm;
//if user create a new alarm then no argument pass in constructor
public AlarmView (AlarmModel alarm = null) {
InitializeComponent ();
if (alarm != null) {
this.CurrentAlarm = alarm;
}
BindingContext = CurrentAlarm ?? new AlarmModel(); //Must have a model to bind to.
}
}
您没有分配绑定上下文,这意味着视图无法绑定到任何内容。如果没有提供模型,则必须为要绑定的视图创建一个模型并获得所需的行为。
答案 1 :(得分:0)
每按一次按钮实现警报状态的更改。真 - 绿色,假 - 红色,不存在 - 黑色。
<强> XML:强>
<?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:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
x:Class="ButtonRendererDemo.ValueConverterPage">
<ContentPage.Resources>
<ResourceDictionary>
<local:ColorConverter x:Key="colorConvert"></local:ColorConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Button Text="Click me" BackgroundColor="{Binding AlarmStatus, Converter={StaticResource colorConvert}}" Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
代码背后:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ButtonRendererDemo
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ValueConverterPage : ContentPage
{
public ValueConverterPage()
{
InitializeComponent();
BindingContext = this;
}
bool? alarmStatus = null;
public bool? AlarmStatus
{
get
{
return alarmStatus;
}
set
{
alarmStatus = value;
OnPropertyChanged("AlarmStatus");
}
}
private void Button_Clicked(object sender, EventArgs e)
{
switch (AlarmStatus)
{
case true:
AlarmStatus = false;
break;
case false:
AlarmStatus = null;
break;
case null:
AlarmStatus = true;
break;
}
}
}
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch (value as bool?)
{
case true:
return Color.Green;
case false:
return Color.Red;
case null:
default:
return Color.Black;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}