我有一个控件,我在其中定义了一个BindableProperty
类型bool
。这BindableProperty
由以下人员使用ViewModel
:如何ViewModel
从我的控件中获取此属性的值?
例如,在ViewModel
I assign itfalse
中,在控件中我想获取它的值,如果它是 false,它会做一些事情。
我的代码
自定义控件 xaml cs:
public static readonly BindableProperty CustomEmojisProperty =
BindableProperty.Create("CustomEmojis", typeof(bool), typeof(Editor), propertyChanged: OnPropertyChanged);
public bool CustomEmojis
{
get { return (bool)GetValue(CustomEmojisProperty); }
set { SetValue(CustomEmojisProperty, value); }
}
private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var editor = bindable as Editor;
if (((Editor)bindable).CustomEmojis == false)
{
ObservableCollection<Emojis> EmojiList = new ObservableCollection<Emojis>();
editor.collectionView.ItemsSource = EmojiList;
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SlightlySmilingFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithStuckOutTongueAndWinkingEye) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.LoudlyCryingFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.WinkingFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithHeartEyes) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.OkHand) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.ThumbsUp) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.ThumbsDown) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.UpsideDownFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.CryingFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithColdSweat) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FlexedBiceps) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.NeutralFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceScreamingInFear) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithTearsOfJoy) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.BackhandIndexPointingUp) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.GrinningFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.MoneyMouthFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.OpenHands) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.RaisedFist) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.RaisedHand) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.RelievedFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.PensiveFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithOpenMouth) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithOpenMouthAndSmilingEyes) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithOpenMouthAndClosedEyes) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithOpenMouthAndColdSweat) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.VulcanSaluteLightSkinTone) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithSunglasses) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithStuckOutTongue) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithStuckOutTongueAndClosedEyes) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.RaisingHands) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.UnamusedFace) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.IndexPointingUp) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.VictoryHand) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.WavingHand) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SignOfTheHornsMediumLightSkinTone) });
}
}
主页.xaml:
<fav1:Control CustomEmojis="{Binding CustomEmojis}"/>
主页.cs:
public MainPage()
{
InitializeComponent();
Xamarin.Forms.Application.Current.On<Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
BindingContext = new EmojiViewModel();
}
ViewModel.xaml.cs:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
ObservableCollection<Emojis> emojilist;
public ObservableCollection<Emojis> EmojiList
{
get => emojilist; set
{
emojilist = value;
OnPropertyChanged();
}
}
bool customemojis;
public bool CustomEmojis {
get =>customemojis;
set {
customemojis = value;
OnPropertyChanged();
}
}
public ViewModel() {
CustomEmojis = true;
if (CustomEmojis == true)
{
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.Niger) });
EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.Kiss) });
}
}
我不确定这是否真的可以做到,如果没有,有没有办法调用在 ViewModel 中的控件上创建的方法?