Flow
OnSizeAllocated
execute a command.
- On command, change
MangoBox.RatioX
value.
- When
MangoBox
Changed, MangoBox
raises RaisePropertyChanged
.
- I expect
BaseLayerView.OnMangoBoxChanged
will be executed.
However, step 4 only executed once when initialize and bind BaseLayerViewModel
.
I checked changing MangoBox
property does execute RaisePropertyChanged
, but BaseLayerView.OnMangoBoxChanged
does not executed.
View
public abstract class BaseLayerView<T> : MvxContentView
{
public static readonly BindableProperty MangoBoxProperty =
BindableProperty.Create(
propertyName: nameof(MangoBox),
returnType: typeof(MangoBox),
declaringType: typeof(BaseLayerView<T>),
defaultValue: null,
propertyChanged: OnMangoBoxChanged);
public MangoBox MangoBox
{
get => (MangoBox)GetValue(MangoBoxProperty);
set => SetValue(MangoBoxProperty, value);
}
static void OnMangoBoxChanged(BindableObject bindable, object oldValue, object newValue)
{
if (newValue is null) { return; }
var baseLayerView = (BaseLayerView<T>)bindable;
// code i want to execute when `MangoBox` changed.
}
}
I bind BaseLayerView
and BaseLayerViewModel
by DataTemplate
.
<DataTemplate x:Key="RectLayerView">
<forms:RectLayerView forms:MangoBox="{Binding MangoBox}" />
ViewModel
Here is binded BaseLayerViewModel
public abstract class BaseLayerViewModel<LayerModel> : MvxViewModel
{
protected T _layerModel;
public MangoBox MangoBox
{
get => _layerModel?.MangoBox;
}
public T LayerModel
{
get => _layerModel;
set => SetProperty(ref _layerModel, value, () =>
{
_layerModel.MangoBox.PropertyChanged += (sender, e) =>
{
RaisePropertyChanged(nameof(MangoBox));
};
RaisePropertyChanged(nameof(MangoBox));
});
}
}
Model
LayerModel
is used when BaeLayerViewModel
created.
public class LayerModel : MvxNotifyPropertyChanged
{
private MangoBox _mangoBox;
public MangoBox MangoBox
{
get => _mangoBox;
set => SetProperty(ref _mangoBox, value, () =>
{
BindBoxEventHandler();
});
}
public LayerModel(MangoBox box)
{
_mangoBox = box;
BindBoxEventHandler();
}
private void BindBoxEventHandler()
{
_mangoBox.XChanged += OnBoxPropertyChanged;
}
private void OnBoxPropertyChanged(object sender, MangoUnitChangedArgs e)
{
RaisePropertyChanged(nameof(MangoBox));
}
}
MangoBox
public class MangoBox : MvxNotifyPropertyChanged
{
private readonly MangoPoint _point;
private readonly MangoSize _size;
public float RatioX
{
get => _point.RatioX;
set => _point.RatioX = value;
}
public MangoBox(Standard2D standard, float x, float y, float width, float height)
{
_point = new MangoPoint(standard, x, y);
_point.XChanged += OnXChanged;
}
public event EventHandler<MangoUnitChangedArgs> XChanged;
private void OnXChanged(object sender, MangoUnitChangedArgs e)
{
XChanged?.Invoke(this, e);
RaisePropertyChanged(nameof(RatioX));
RaisePropertyChanged(nameof(ValueX));
}
}