简体   繁体   中英

loading a web page from a c# android app

Hi i am trying to make an android app using xamarin and c# I have tried so many different things I just cant figure out how to get the webpage to open i have this so far

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/")));
            };
        }
    }
}

any help is greatly appreciated

i think the whole line StartActivity (new Intent (Intent.ActionView, Uri.parse("http://www.wealdstonefc.co.uk/"))); is wrong as i keep getting errors

- 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) 

The second error: 'System.Uri' does not contain a definition for 'parse' means that the complier can't figure out which package you want to use for Uri.parse.

@admdrew is correct that you need to pick one, either but calling it explicity:

StartActivity (new Intent (Intent.ActionView, Android.Net.Uri.parse("http://www.wealdstonefc.co.uk/")));

or using the using Android.Net.Uri directive in the top of your file.

Disclaimer: I don't know much about Xamarin.

Indeed, you need to use the Android.Net.Uri.Parse method to get an Android.Net.Uri object, which is what the Android.Content.Intent class expects in its constructor.

When in doubt which namespace to use, you can inspect the IntelliSense details that Visual Studio supplies for the method you want to call. Press Ctrl + Shift + Space (with the cursor inside the call) and step down with your arrow keys to the specific overload to see the parameter info, which includes the expected types.

In your case, this would be overload #6 of 8: Intent.Intent(string action, Android.Net.Uri uri) .

When going through compilation errors, fixing the "ambiguous reference" issues should always be your first priority. Using the System.Uri class in a first attempt to clear the ambiguity, one would notice that it doesn't even have a (public) .Parse method.

In general, porting Android Java code to Xamarin C# code involves having to rename methods and properties (such as .parse -> .Parse ). It can be a good idea to search for a C# example from the start.

Worth a note is that there are examples for many common situations in the public documentation supplied by Xamarin:

http://docs.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM