Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Xamarin.Forms在iOS中创建圆角进度条_C#_Ios_Xamarin_Xamarin.forms_Xamarin.ios - Fatal编程技术网

C# 如何使用Xamarin.Forms在iOS中创建圆角进度条

C# 如何使用Xamarin.Forms在iOS中创建圆角进度条,c#,ios,xamarin,xamarin.forms,xamarin.ios,C#,Ios,Xamarin,Xamarin.forms,Xamarin.ios,我使用来自此的输入,使用可绘制的为Android平台创建了一个带有圆角的自定义进度条。 但是,我无法为iOS创建相同的输出 下面给出了它在Android上的外观。 如何在iOS中也创建相同的效果?有很多方法可以做到这一点,我更喜欢使用UIView的CALayer并添加绘制“进度条”的CAShapeLayer UIView ProgressBar示例: 用法示例: var progressView=newprogressview(new-CGRect(50,50,300,50)); 添加(pr

我使用来自此的输入,使用
可绘制的
Android
平台创建了一个带有圆角的自定义进度条。 但是,我无法为
iOS
创建相同的输出

下面给出了它在Android上的外观。


如何在
iOS
中也创建相同的效果?

有很多方法可以做到这一点,我更喜欢使用
UIView
CALayer
并添加绘制“进度条”的
CAShapeLayer

UIView ProgressBar示例:

用法示例:
var progressView=newprogressview(new-CGRect(50,50,300,50));
添加(progressView);
DispatchQueue.MainQueue.DispatchAsync(异步()=>
{
而(progressView.Complete progressView.Complete+=0.05);
等待任务。延迟(1000);
}
});

谢谢您的详细回答。您还可以帮助我如何使用
CustomRenderer
Xamarin.Forms
实现此方法吗?@LibinTK Xamarin有一个指南,介绍了如何为表单实现自定义视图渲染器:如何在android中显示相同的UI?@Singapore用您目前掌握的代码和您的问题:谢谢你的回答。我用你的答案做了一个进度条,从右边开始<代码>var progressWidth=(rect.Width-(Layer.BorderWidth*2))*(1-完成);var progressRect=new cgrct(rect.X+progressWidth,rect.Y+Layer.BorderWidth,rect.Width-progressWidth,(rect.Height-Layer.BorderWidth*2));progressLayer.Path=UIBezierPath.FromRoundedRect(progressRect,(UIRectCorner.TopRight | UIRectCorner.BottomRight),新的CGSize(30,30)).CGPath但我没有看到标签。
public class ProgressView : UIView
{
    CAShapeLayer progressLayer;
    UILabel label;

    public ProgressView() { Setup(); }
    public ProgressView(Foundation.NSCoder coder) : base(coder) { Setup(); }
    public ProgressView(Foundation.NSObjectFlag t) : base(t) { Setup(); }
    public ProgressView(IntPtr handle) : base(handle) { }
    public ProgressView(CGRect frame) : base(frame) { Setup(); }

    void Setup()
    {
        BackgroundColor = UIColor.Clear;

        Layer.CornerRadius = 25;
        Layer.BorderWidth = 10;
        Layer.BorderColor = UIColor.Blue.CGColor;
        Layer.BackgroundColor = UIColor.Cyan.CGColor;

        progressLayer = new CAShapeLayer()
        {
            FillColor = UIColor.Red.CGColor,
            Frame = Bounds
        };
        Layer.AddSublayer(progressLayer);

        label = new UILabel(Bounds)
        {
            TextAlignment = UITextAlignment.Center,
            TextColor = UIColor.White,
            BackgroundColor = UIColor.Clear,
            Font = UIFont.FromName("ChalkboardSE-Bold", 20)
        };
        InsertSubview(label, 100);
    }

    double complete;
    public double Complete
    {
        get { return complete; }
        set { complete = value; label.Text = $"{value * 100} %"; SetNeedsDisplay(); }
    }

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);
        var progressWidth = (rect.Width - (Layer.BorderWidth * 2)) * complete;
        var progressRect = new CGRect(rect.X + Layer.BorderWidth, rect.Y + Layer.BorderWidth, progressWidth, (rect.Height - Layer.BorderWidth * 2));
        progressLayer.Path = UIBezierPath.FromRoundedRect(progressRect, 25).CGPath;
    }
}
var progressView = new ProgressView(new CGRect(50, 50, 300, 50));
Add(progressView);
DispatchQueue.MainQueue.DispatchAsync(async () =>
{
    while (progressView.Complete <= 1)
    {
        InvokeOnMainThread(() => progressView.Complete += 0.05);
        await Task.Delay(1000);
    }
});