如何通过单击xamarin表单中的按钮打开网页

时间:2015-12-10 12:05:50

标签: c# xamarin.forms

我有一个名为HomePage的xamarin形式的文件,它从用户那里获取eURL内容。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace zaposta_angelar
{
    public class HomePage : ContentPage
    {
        Label lTitle;
        Entry eURL;
        Button bButton;

        public HomePage()
        {
            lTitle = new Label
            {
                Text = "WELCOME",
                HorizontalOptions = LayoutOptions.Center,
                FontAttributes = Font.SystemFontOfSize(25, FontAttributes.Bold).FontAttributes,
                 FontSize = Font.SystemFontOfSize(25, FontAttributes.Bold).FontSize,
                //WidthRequest = 200,
                HeightRequest = 100
            };

            eURL = new Entry { Placeholder = "Enter Site URL", };

            bButton = new Button { Text = "ENTER", };
            bButton.Clicked += bButton_Clicked;

            this.Content = new StackLayout
            {
                Children = {
                    lTitle, eURL, bButton
                }
            };
        }

        void bButton_Clicked(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(eURL.Text)) { DisplayAlert("URL", "Your URL is Empty", "OK"); }
            else
            {
                SitePage.LoadRequest(new SitePage(eURL)));
            }
        }
    }
}

并点击按钮bButton打开eURL并将网站加载到另一个名为SitePage的页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace zaposta_angelar
{
    public class SitePage : NavigationPage
    {
        private Entry eURL;

        public SitePage()
        {

        }

        public SitePage(Entry eURL)
        {
            // TODO: Complete member initialization
            this.eURL = eURL;
        }

        internal static void LoadRequest(SitePage sitePage)
        {
            throw new NotImplementedException();
        }
    }
}

代码无法正常工作

1 个答案:

答案 0 :(得分:5)

嗯,我不确定您打算走哪条路,但您可以通过

打开网页
  1. 内部使用WebView

  2. 使用Device.OpenUri