Monotouch Frame dimensions can't be set direct

2019-05-31 11:08发布

Editing UI Frame properties directly don't seem to work.

i.e.

scrollView.ContentSize.Width = 100;

doesn't work but

RectangleF scrollFrame = ScrollView.Frame;
scrollFrame.Width = width * pageCount;
ScrollView.ContentSize = scrollFrame.Size;

does! Why is this? Isn't Monotouch supposed to protect against arcane programming styles?

1条回答
霸刀☆藐视天下
2楼-- · 2019-05-31 11:36

This is basic C# behavior.

The ContentSize property is a SizeF which is a struct (=value type) and not a class (=reference type).

Calling

scrollView.ContentSize.Width = 100;

does not work because you are setting a value on a property of a copied object.

Calling

scrollFrame.Width = width * pageCount;

works because although RectangleF is also a struct, you are setting a value on the actual object.

Likewise,

ScrollView.ContentSize = scrollFrame.Size;

creates a copy, but sets a new object after the '=' and works correctly.

查看更多
登录 后发表回答