从c#android应用程序加载网页
嗨,我正在尝试使用xamarin和c制作一个android应用程序,我已经尝试了很多不同的东西,我只是不知道如何打开网页,我到目前为止已经有了这个从c#android应用程序加载网页,c#,android-activity,xamarin.android,xamarin,C#,Android Activity,Xamarin.android,Xamarin,嗨,我正在尝试使用xamarin和c制作一个android应用程序,我已经尝试了很多不同的东西,我只是不知道如何打开网页,我到目前为止已经有了这个 using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using Android.Webkit; using Android.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Android.Net;
namespace llll
{
[Activity (Label = "llll", MainLauncher = true)]
public class MainActivity : Activity
{
string target = "http://www.wealdstonefc.co.uk/";
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
LinearLayout layout = new LinearLayout (this);
layout.Orientation = Orientation.Vertical;
TextView label = new TextView (this);
label.Text = "Wealdstone";
Button button = new Button (this);
button.Text = "League Table";
button.Click += (object sender, EventArgs e) => {
StartActivity (new Intent (Intent.ActionView, Uri.parse("http://www.wealdstonefc.co.uk/")));
};
}
}
}
非常感谢您的帮助
我认为整行StartActivity(新的Intent(Intent.ActionView,Uri.parse()http://www.wealdstonefc.co.uk/")));代码>是错误的,因为我不断出错
- C:\Users\Moonie\Documents\Projects\llll\llll\MainActivity.cs(51,51): Error CS0104: 'Uri' is an ambiguous reference between 'System.Uri' and 'Android.Net.Uri' (CS0104) (llll)
- C:\Users\Moonie\Documents\Projects\llll\llll\MainActivity.cs(55,55): Error CS0117: 'System.Uri' does not contain a definition for 'parse' (CS0117) (llll)
第二个错误:“System.Uri”不包含“parse”的定义。
意味着编译器无法确定要将哪个包用于Uri.parse
@Admdew是正确的,您需要选择一个,但称之为明确:
StartActivity (new Intent (Intent.ActionView, Android.Net.Uri.parse("http://www.wealdstonefc.co.uk/")));
StartActivity(新的意图)(Intent.ActionView,Android.Net.Uri.parse(“http://www.wealdstonefc.co.uk/")));
或者使用文件顶部的usingandroid.Net.Uri
指令
免责声明:我对Xamarin了解不多 实际上,您需要使用Android.Net.Uri.Parse
方法来获取Android.Net.Uri
对象,这是Android.Content.Intent
类在其构造函数中所期望的
如果不确定使用哪个名称空间,可以检查VisualStudio为要调用的方法提供的IntelliSense详细信息。按Ctrl+Shift+Space(光标在调用中),然后用箭头键向下移动到特定重载以查看参数信息,其中包括预期类型
在您的情况下,这将是重载#8中的6:Intent.Intent(string action,Android.Net.Uri)
在处理编译错误时,修复“不明确引用”问题应该始终是您的首要任务。在第一次尝试清除歧义时使用System.Uri
类,您会注意到它甚至没有(public).Parse
方法
通常,将Android Java代码移植到Xamarin C#代码需要重命名方法和属性(例如.parse
->.parse
)。从一开始就搜索C#示例可能是个好主意
值得注意的是,Xamarin提供的公共文档中有许多常见情况的示例:
嗯,这些错误都是非常具有描述性的parse
在System.Uri
中不是有效的方法(记住C#是区分大小写的,所以-parse
),并且您没有显式调用System.Uri
或Android.Net.Uri
。挑一个!