【问题标题】:MonoTouch: very simple webView codeMonoTouch:非常简单的 webView 代码
【发布时间】:2013-12-29 04:05:43
【问题描述】:

我正在使用 MonoTouch 上的一些 C# 后台任务创建一些 Web/本机混合 iOS 应用程序。

首先,我尝试创建非常简单的 webView 示例,参考

(上面的示例项目代码有效,但我只需要一个没有 NavigatorView 的 webView) 和

到目前为止我的代码是:

AppDelegate.cs

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iostest
{ 
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    { 
        UIWindow window;  
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            // create a new window instance based on the screen size
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.RootViewController = new WebViewController(); 
            // make the window visible
            window.MakeKeyAndVisible ();

            return true;
        }
    }
}

WebViewController.cs

using System;
using System.IO;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iostest
{
    public class WebViewController : UIViewController {

        UIWebView webView;
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            Console.WriteLine ("WebView Launched"); //this works

            webView = new UIWebView(View.Bounds);
            webView.ScalesPageToFit = false;
            webView.LoadRequest (new NSUrlRequest
                (new NSUrl (Path.Combine(NSBundle.MainBundle.BundlePath,
                               "www/app.html"), false)));

            this.View.AddSubview(webView);
        }
    }
}

此代码运行没有错误,但结果只有一个空白页,没有显示我的 HTML 内容“www/app.html”(或“http://google.com”等等)。

我看不出我错过了什么逻辑。任何想法?谢谢。

【问题讨论】:

    标签: ios webview uiwebview xamarin.ios


    【解决方案1】:

    好的,事情一直很模糊,但我自己解决了。

    问题是本地资源文件(“www/app.html”)应在 IDE 的 BuildAction 属性中标记为“内容”。

    我记得几年前我被这个困住了,现在仍然看到很多人被困住。

    缺乏文档会导致浪费时间。请记录 Xamarin。

    当前代码供以后参考:

    AppDelegate.cs

    using System;
    //using System.Collections.Generic;
    //using System.Linq;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    
    namespace iOStest2
    { 
        [Register ("AppDelegate")]
        public partial class AppDelegate : UIApplicationDelegate
        {
            UIWindow window;     
            public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            {
                window = new UIWindow (UIScreen.MainScreen.Bounds);      
                window.RootViewController = new WebViewController ();
                window.MakeKeyAndVisible ();
    
                return true;
            }
        }
    }
    

    WebViewController.cs

    using System;
    using System.IO;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    
    namespace iOStest2
    {
        public class WebViewController : UIViewController
        {
            UIWebView webView;
    
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                Console.WriteLine ("WebView Launched");
                View.BackgroundColor = UIColor.Gray;
                //string url = "http://google.com";
                string url= Path.Combine(NSBundle.MainBundle.BundlePath,
                                     "Content/app.html");
                webView = new UIWebView(View.Bounds);
                webView.LoadRequest(new NSUrlRequest(new NSUrl(url,false))); 
                webView.ScalesPageToFit = false;
    
                View.AddSubview(webView);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多