简体   繁体   中英

A more elegant way to change a single element in a struct in C#

Is there a more elegant way to change a single element in a View.Frame struct in Xamarin/C# without using one of the following patterns?

var frame = View.Frame;
frame.Y = 123;
View.Frame = frame;

or

View.Frame = new RectangleF(View.Frame.X, 123, View.Frame.Width, View.Frame.Height);

Simply changing the element like this: View.Frame.Y = 123; doesn't have any effect. Only once the entire Frame struct is reassigned does it take effect.

I took a few minutes and tried to come up with something, but because the Frame is a struct it doesn't look there is a more elegant way:

MonoTouch: How to change control's location at runtime?

One thing you could possibly do is create a static utility class like this that you could reuse:

public static class FrameUtil{
     public RectangleF UpdateX(RectangleF frame, float newX){
          var tempFrame = frame;
          tempFrame.x = newX;
          return tempFrame;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM