Resize events in IMarkupExtension #18940
Unanswered
prabhavmehra
asked this question in
Q&A
Replies: 1 comment
-
This can be solved with:
The following implements a simple resize detector counter so with each page resize we bump up the counter: public class ProxyElement : Element
{
public static readonly BindableProperty PageProperty = BindableProperty.Create(nameof(Page), typeof(ContentPage), typeof(ProxyElement));
public ContentPage? Page
{
get => (ContentPage?)GetValue(PageProperty);
internal set => SetValue(PageProperty, value);
}
public static readonly BindableProperty PageWidthProperty = BindableProperty.Create(nameof(PageWidth), typeof(double), typeof(ProxyElement), default(double));
public double PageWidth
{
get => (double)GetValue(PageWidthProperty);
internal set => SetValue(PageWidthProperty, value);
}
public static readonly BindableProperty PageHeightProperty = BindableProperty.Create(nameof(PageHeight), typeof(double), typeof(ProxyElement), default(double));
public double PageHeight
{
get => (double)GetValue(PageHeightProperty);
internal set => SetValue(PageHeightProperty, value);
}
public ProxyElement()
{
this.SetBinding(PageProperty, static (ContentPage p) => p, BindingMode.OneWay, source: new RelativeBindingSource(mode: RelativeBindingSourceMode.FindAncestor, ancestorType: typeof(ContentPage)));
this.SetBinding(PageWidthProperty, static (ProxyElement p) => p.Page?.Width, BindingMode.OneWay, source: this);
this.SetBinding(PageHeightProperty, static (ProxyElement p) => p.Page?.Height, BindingMode.OneWay, source: this);
}
}
[RequireService([typeof(IReferenceProvider), typeof(IProvideValueTarget)])]
public class ResizeDetectorExtension : Element, IMarkupExtension<BindingBase>
{
public ProxyElement? Proxy { get; internal set; }
int returnValue = 0;
public int ReturnValue
{
get => returnValue;
internal set
{
returnValue = value;
OnPropertyChanged(nameof(ReturnValue));
}
}
public object ProvideValue(IServiceProvider serviceProvider)
=> (this as IMarkupExtension<BindingBase>).ProvideValue(serviceProvider);
BindingBase IMarkupExtension<BindingBase>.ProvideValue(IServiceProvider serviceProvider)
{
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget provideValueTarget)
{
if (provideValueTarget.TargetObject is VisualElement targetElement)
{
this.Proxy = new ProxyElement { Parent = targetElement };
this.Proxy.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(ProxyElement.PageWidth):
case nameof(ProxyElement.PageHeight):
this.ReturnValue++;
break;
}
};
}
}
return BindingBase.Create(static (ResizeDetectorExtension t) => t.ReturnValue, BindingMode.OneWay, source: this);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was wondering if there is a way to get window resize events in a MarkupExtension.
I am trying to build a rsponsive UI and currently I have been using VisualStateManager and some custom implementation of
breakpoints
. I tried implementating the same in a markupextension for better experience.Beta Was this translation helpful? Give feedback.
All reactions