gpt4 book ai didi

c# - 我应该在设置之前检查 VisualElement.IsVisible 吗?

转载 作者:行者123 更新时间:2023-12-04 09:32:07 24 4
gpt4 key购买 nike

Xamarin Forms 中的可视元素通过设置它们的 IsVisible 属性变得可见/不可见。更改视觉元素上的某些属性或数据可能会导致界面重绘,这可能会很昂贵。
我正在尝试提高我的应用程序的性能,因此我引入了缓存并尝试进行一些布局简化,但我必须使许多视觉元素可见/不可见,并且想知道优化它的最佳方法是什么。
哪个操作更有效?

var myButton.IsVisible = true;
或者:
if(!myButton.IsVisible) myButton.IsVisible = true;
如果 Xamarin 已检查状态并决定是否重绘,则第二个选项是多余的,因此效率低下。如果每次设置属性时都发生重绘,那么效率会更高。我找不到任何文件来告诉我是哪种情况。

最佳答案

DependencyProperty 的任何良好实现(或 INotifyPropertyChanged )成员应该忽略连续的 set如果那 value是一样的。出于这个原因,让 IsVisible弄清楚该怎么做,而不是将责任放在来电者身上。

If Xamarin already checks the state and decides whether to redraw then the second option is redundant and therefore inefficien


这是正确的。 Xamarin 与其他基于 XAML 的实现一样,具有视觉效率(与 WinForms 不同),因此设置视觉属性不太可能立即导致屏幕刷新。此外,整个应用程序窗口都在单个 blit 中呈现以避免闪烁。
在 Xamarin 中,设置 button.IsVisible例如使它下降到 BindableObject.SetValueActual ( Button 在其继承图中具有 BindableObject),其中检查相同性。仅当值不同或 SetValueFlags.RaiseOnEqual 时才应用新值设置。 OnPropertyChangedPropertyChanged在不同的值上调用或者如果 RaiseOnEqual设置。
Xamarin 源代码 来自 GitHub :(我的评论)
void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, bool silent = false)
{
object original = context.Value;
bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;

bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
if (!silent && (!same || raiseOnEqual))
{
property.PropertyChanging?.Invoke(this, original, value);

OnPropertyChanging(property.PropertyName);
}

if (!same || raiseOnEqual)
{
context.Value = value; // <---------- assignment
}
.
.
.
if (!silent && (!same || raiseOnEqual)) // <------- notifications guard
{
if (binding != null && !currentlyApplying)
{
_applying = true;
binding.Apply(true);
_applying = false;
}

OnPropertyChanged(property.PropertyName);

property.PropertyChanged?.Invoke(this, original, value);

关于c# - 我应该在设置之前检查 VisualElement.IsVisible 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62790904/

24 4 0