0
我已經將縮放比例設置爲AspectFit,以便我的圖像在UIImageView中顯示。 但是我的圖像出現在iphone屏幕的中心。將UIImageView放在UIViewContentMode.ScaleAspectFit的屏幕上方
我希望這將顯示在我的屏幕頂部,保持其縱橫比。這可以在C#中完成。這是我目前如何設置我的形象。
public override void Draw(CGRect rect)
{
myImageView = new UIImageView()
{
Image = UIImage.FromBundle("filename"),
ContentMode = UIViewContentMode.ScaleAspectFit,
Frame = new CGRect(0, 0, rect.Width, rect.Height)
};
}
任何代碼示例或建議都會很有幫助。 謝謝。
我CustomControl:
[DesignTimeVisible(true), System.ComponentModel.Category("CustomControl")]
public partial class CustomControl : UIView
{
[Export("ImageFileName"), Browsable(true)]
public string ImageFileName{get;set;}
public static KipperView Create()
{
var arr = NSBundle.MainBundle.LoadNib("CustomControl", null, null);
var view = Runtime.GetNSObject<KipperView>(arr.ValueAt(0));
return view;
}
public CustomControl() : base()
{}
public CustomControl(IntPtr p) : base(p)
{}
public override void Draw(CGRect rect)
{
myImageView = new UIImageView()
{
Image = UIImage.FromBundle(ImageFileName),
ContentMode = UIViewContentMode.ScaleAspectFit,
Frame = new CGRect(0, 0, rect.Width, rect.Height)
};
this.Add(myImageView);
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
var x = (float)touch.GetPreciseLocation(myImageView).X;
var y = (float)touch.GetPreciseLocation(myImageView).Y;
}
}
而且我CustomRendered:
public class CustomRendered : ViewRenderer<MyFormsControl, CustomControl>
{
protected override void OnElementChanged(ElementChangedEventArgs<MyFormsControl> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var control = new CustomControl();
SetNativeControl(control);
}
if (this.Element == null) return;
Control.ImageFileName = "logo.png";
Control.ContentMode = UIViewContentMode.ScaleAspectFit;
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
switch (e.PropertyName)
{
case "ImageFileName":
Control.ImageFileName = "xamarin.png";
this.SetNeedsDisplay();
break;
default:
return;
}
}
}
我設置的形式控制爲:
public class MyFormControl : View
{
public static readonly BindableProperty ImageNameProperty = BindableProperty.Create(nameof(ImageName), typeof(string), typeof(CustomKipperControl), null, propertyChanged: OnItemsSourcePropertyChanged);
private static void OnItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue) { }
public string ImageName
{
get
{
return (string)GetValue(ImageNameProperty);
}
set
{
SetValue(ImageNameProperty, value);
}
}
protected override void OnPropertyChanged(string propertyName)
{
base.OnPropertyChanged(propertyName);
switch (propertyName)
{
case "ImageFileName":
break;
}
}
}
你爲什麼初始化方法ImageView的畫?是否ImageView的有父視圖? –
@ColeXia那是因爲,我有我的Customcontrol中的這個ImageView:UIView,我將在xamarin窗體中使用它。我將從窗體動態地改變我的圖像。我接受任何其他建議。 – Praddy
你的意思是customRenderer?請提供更多代碼CustomControl –