如何在Xamarin ios中使用WebView在混合应用程序中设置进度栏

如何在Xamarin ios中使用WebView在混合应用程序中设置进度栏,webview,xamarin.ios,hybrid-mobile-app,Webview,Xamarin.ios,Hybrid Mobile App,我正在尝试使用ios版本的混合webview应用程序,以便在我执行一些较长的操作时显示一些进度控制,但不知道如何设置它。在我的Android版本中,我基本上是在Main.axml中创建进度控件,最初隐藏,然后在需要时取消隐藏: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:ori

我正在尝试使用ios版本的混合webview应用程序,以便在我执行一些较长的操作时显示一些进度控制,但不知道如何设置它。在我的Android版本中,我基本上是在Main.axml中创建进度控件,最初隐藏,然后在需要时取消隐藏:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ProgressBar
        android:id="@+id/ProgressSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        style="@android:style/Widget.ProgressBar.Small"
        android:visibility="gone"
        android:layout_marginRight="5dp" />
    <ProgressBar
        android:id="@+id/ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:visibility="gone"
        android:layout_marginRight="5dp" />
    <TextView
        android:id="@+id/ProgressText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView" />
</LinearLayout>
然后使用它:

public override void ProgressOperation(ProgressMode mode, int value, string message)
{
    UIActivityIndicatorView progressSpinner = WebViewController.progressSpinner;
    UIProgressView progressBar = WebViewController.progressBar;
    UILabel progressText = WebViewController.progressText;

    if (progressBar == null)
        return;

    switch (mode)
    {
        case ProgressMode.Start:
            InProgress = true;
            ProgressMax = value;
            if (value <= 0)
                ProgressMax = 1.0f;
            progressBar.Hidden = false;
            progressBar.Progress = 0.0f;
            if (!String.IsNullOrEmpty(message))
            {
                progressText.Text = message;
                progressText.Hidden = false;
                HaveMessage = true;
            }
            break;
        case ProgressMode.Update:
            if (InProgress)
            {
                progressBar.Progress = value / ProgressMax;
                if (HaveMessage)
                {
                    if (!String.IsNullOrEmpty(message))
                        progressText.Text = message;
                }
            }
            break;
        case ProgressMode.Stop:
            progressBar.Hidden = true;
            progressText.Text = String.Empty;
            progressText.Hidden = true;
            InProgress = false;
            HaveMessage = false;
            break;
        case ProgressMode.Hide:
            progressSpinner.StopAnimating();
            progressSpinner.Hidden = true;
            break;
        case ProgressMode.Show:
            progressSpinner.Hidden = false;
            progressSpinner.StartAnimating();
            break;
        case ProgressMode.DelayedShow:
            if (InDelayedProgress)
            {
                progressSpinner.Hidden = false;
                progressSpinner.StartAnimating();
            }
            break;
        default:
            throw new Exception("ProgressOperation: Need mode support for: " + mode.ToString());
    }
}
public override void ProgressOperation(ProgressMode模式、int值、字符串消息)
{
UIActivityIndicatorView progressSpinner=WebViewController.progressSpinner;
UIProgressView progressBar=WebViewController.progressBar;
UILabel progressText=WebViewController.progressText;
if(progressBar==null)
返回;
开关(模式)
{
案例处理模式。开始:
InProgress=true;
ProgressMax=值;

if(value我最后做的是浏览苹果的xcode界面生成器教程()的前两部分.从这一点我可以推断出我需要从一个带有嵌入式活动指示器、进度条、标签和web视图控件的嵌入式垂直堆栈视图开始,适当设置约束,甚至尝试使用Outlet和一些Swift代码

然后,我尝试使用Xamarin interface builder做同样的事情,甚至遵循了他们的一个教程,但我无法理解约束机制来做我在xcode中所做的事情,所以我放弃了,只是复制了xcode脚本源代码。我不得不对其进行一些修改以使其正常工作。我必须重用xcode中的一些相同属性marin版本,删除插座,并使用Xamarin的界面生成器重做插座

我很惊讶没有人能花上几分钟给我一个提示,让我从使用web视图作为主视图改为使用视图->堆栈视图->微调器+条形图+标签+web层次结构。但是我想学习一点xcode的几个小时可能花得很长,也很有趣。xcode界面生成器似乎是唯一可用的工具到目前为止,我使用过的少数几个

public static UIWebView webView;
public static UIActivityIndicatorView progressSpinner;
public static UIProgressView progressBar;
public static UILabel progressText;
// ...

// Get web view from storyboard backing code.
webView = WebView;

// Set up activity indicator.
progressSpinner = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
CGRect bounds = UIScreen.MainScreen.Bounds;
nfloat centerX = bounds.Width / 2;
nfloat centerY = bounds.Height / 2;
progressSpinner.Frame = new CGRect(
    centerX - (progressSpinner.Frame.Width / 2),
    bounds.Top,
    progressSpinner.Frame.Width,
    progressSpinner.Frame.Height);
progressSpinner.AutoresizingMask = UIViewAutoresizing.All;
progressSpinner.Hidden = false;
View.InsertSubviewAbove(progressSpinner, webView);

// Set up progress bar.
progressBar = new UIProgressView(UIProgressViewStyle.Bar);
progressBar.Frame = new CGRect(
    bounds.Left,
    progressSpinner.Frame.Bottom,
    bounds.Width,
    progressSpinner.Frame.Height);
progressBar.AutoresizingMask = UIViewAutoresizing.All;
progressBar.Hidden = false;
View.InsertSubviewAbove(progressBar, webView);

// Set up progress text message.
progressText = new UILabel();
progressText.Frame = new CGRect(
    bounds.Left,
    progressBar.Frame.Bottom,
    bounds.Width,
    progressText.Frame.Height);
progressText.AutoresizingMask = UIViewAutoresizing.All;
progressText.Hidden = false;
View.InsertSubviewAbove(progressText, webView);
public override void ProgressOperation(ProgressMode mode, int value, string message)
{
    UIActivityIndicatorView progressSpinner = WebViewController.progressSpinner;
    UIProgressView progressBar = WebViewController.progressBar;
    UILabel progressText = WebViewController.progressText;

    if (progressBar == null)
        return;

    switch (mode)
    {
        case ProgressMode.Start:
            InProgress = true;
            ProgressMax = value;
            if (value <= 0)
                ProgressMax = 1.0f;
            progressBar.Hidden = false;
            progressBar.Progress = 0.0f;
            if (!String.IsNullOrEmpty(message))
            {
                progressText.Text = message;
                progressText.Hidden = false;
                HaveMessage = true;
            }
            break;
        case ProgressMode.Update:
            if (InProgress)
            {
                progressBar.Progress = value / ProgressMax;
                if (HaveMessage)
                {
                    if (!String.IsNullOrEmpty(message))
                        progressText.Text = message;
                }
            }
            break;
        case ProgressMode.Stop:
            progressBar.Hidden = true;
            progressText.Text = String.Empty;
            progressText.Hidden = true;
            InProgress = false;
            HaveMessage = false;
            break;
        case ProgressMode.Hide:
            progressSpinner.StopAnimating();
            progressSpinner.Hidden = true;
            break;
        case ProgressMode.Show:
            progressSpinner.Hidden = false;
            progressSpinner.StartAnimating();
            break;
        case ProgressMode.DelayedShow:
            if (InDelayedProgress)
            {
                progressSpinner.Hidden = false;
                progressSpinner.StartAnimating();
            }
            break;
        default:
            throw new Exception("ProgressOperation: Need mode support for: " + mode.ToString());
    }
}