C# 调用UIViewController类的非静态函数
我正在尝试用c构建一个移动跨平台的ios应用程序,但我在这一点上陷入了困境,因为我无法调用UIViewController的非静态函数。 以下是我的主要观点:C# 调用UIViewController类的非静态函数,c#,ios,xcode,uiviewcontroller,C#,Ios,Xcode,Uiviewcontroller,我正在尝试用c构建一个移动跨平台的ios应用程序,但我在这一点上陷入了困境,因为我无法调用UIViewController的非静态函数。 以下是我的主要观点: ... namespace BSoft.iOS { public class Application { static void Main(string[] args) { UIApplication.Main(args, null, "AppDelegate"); } public s
...
namespace BSoft.iOS
{
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public static void OpenWebPage(string link, UIViewController controller)
{
UIStoryboard board = UIStoryboard.FromName("Main", null);
UIViewController ctrl = (UIViewController)board.InstantiateViewController("WebViewController");
/// here I should call SetLink() from WebViewController class
ctrl.ModalTransitionStyle = UIModalTransitionStyle.PartialCurl;
controller.PresentViewController(ctrl, true, null);
}
}
}
下面是WebViewController类:
...
namespace BSoft.iOS
{
public partial class WebViewController : UIViewController
{
WKWebView WebViewer;
public WebViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
WebViewer = new WKWebView(View.Frame, new WebKit.WKWebViewConfiguration());
WebViewer.SizeToFit();
WebViewer.LoadRequest(Foundation.NSUrlRequest.FromUrl(Foundation.NSUrl.FromString("https://www.360bsoft.com/register")));
WebViewerContainer.AddSubview(WebViewer);
}
public void SetLink(string link)
{
if(WebViewer!=null)
WebViewer.LoadRequest(Foundation.NSUrlRequest.FromUrl(Foundation.NSUrl.FromString(link)));
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
}
}
在我的故事板中,我将一个自定义类WebViewController和一个标识WebViewController归于我的webview控制器,问题是当我更改视图时,我无法从我的主应用程序调用SetLink
有什么想法吗?在main.cs中,尝试将ctrl键转换为WebViewController而不是UIViewController:
WebViewController ctrl = (WebViewController)board.InstantiateViewController("WebViewController");
很高兴它起作用了。这不起作用的原因是在UIViewController类上没有定义“SetLink”,而UIViewController类正是您之前强制转换“ctrl”的类型。旁注:在这种情况下,实际上可以动态调用它,但这对于您的情况来说是不必要的复杂化