如何创建相互链接的xamarin页面?

时间:2017-12-17 19:29:29

标签: html razor xamarin.android

我试图通过HTML在Xamarin中创建3个页面,它们如何链接在一起? 我有主要活动:

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        WebView webView1 = FindViewById<WebView>(Resource.Id.webView1);
        webView1.Settings.JavaScriptEnabled = true;
        webView1.Settings.CacheMode = CacheModes.NoCache;
        webView1.Settings.SetRenderPriority(WebSettings.RenderPriority.High);

        HIP.Assets.mainPage html = new HIP.Assets.mainPage();
        webView1.LoadDataWithBaseURL("file:///android_asset/", html.GenerateString(), "text/html", "utf-8", null);

        HIP.Assets.page1 p1 = new HIP.Assets.page1();
        webView1.LoadDataWithBaseURL("file:///android_asset/", p1.GenerateString(), "text/html", "utf-8", null);

        HIP.Assets.page2 p2 = new HIP.Assets.page2();
        webView1.LoadDataWithBaseURL("file:///android_asset/", p2.GenerateString(), "text/html", "utf-8", null);


    }
}

链接页面,我在这些页面中使用了razor预处理模板()和标记:

mainPage.cshtm:

<a href="file:///android_asset/page1.cshtml">page 1</a>

和page1.cshtml:

<a href="file:///android_asset/page2.cshtml">page 2</a>

那么,我的计划如何继续?

3 个答案:

答案 0 :(得分:0)

how to create xamarin pages that linked to each other?

You could create a custom Decimal.Parse to filter the WebViewClient, when user click the local html link you could choose use url load this file.

For example :

WebView

Effect.

答案 1 :(得分:0)

实际上,我有3页以上的页面,但是为了对我说3页的问题进行抽样,它超过3页。 所有cshtml页面中的导航栏和文本。 我有一个文本,在某些页面显示它和下一页按钮和上一页按钮我去页面。 它适用于标签。 有关示例请参阅此代码,不要忘记所有页面都有此结构:

<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="file:///android_asset/Scripts/jquery-1.9.1.js"></script>
    <link href="file:///android_asset/Content/bootstrap-rtl.css" rel="stylesheet" />
    <script src="file:///android_asset/Scripts/bootstrap.js"></script>
</head>
<body>
    <div class="container" style="background-color:#e2dddd">
        <div class="row" style="padding:5px">
            <nav class="navbar navbar-inverse">
                <div class="container">
                    <div class="navbar-header">
                        <a href="file:///android_asset/Ethicshtml.cshtml" class="navbar-brand">Home</a>
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavBar">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                    </div>
                    <ul class="collapse nav navbar-nav" id="mainNavBar">
                        <li><a href="file:///android_asset/ProfessionalMain.cshtml">professional</a></li>
                        <li><a href="file:///android_asset/MoralMain.cshtml">moral</a></li>
                        <li><a href="file:///android_asset/SecurityMain.cshtml">security</a></li>
                        <li class="dropdown">
                        </li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right"></ul>
                </div>
            </nav>
        </div>
        <div class="row  text-center" style="padding:0px">
            <div class="col-sm-8" style="background-color:white;height:50px"><a href="file:///android_asset/Moral2.cshtml" class="btn btn-primary btn-lg">next page</a> <a href="file:///android_asset/Moral.cshtml" class="btn btn-primary btn-lg">previous page</a> <a href="file:///android_asset/Moral.cshtml" class="btn btn-primary btn-lg">moral page</a> </div>
            <div dir="rtl" class="col-sm-8" style="background-color:white;height:400px;text-align:justify">
              my text is here
            </div>
        </div>
    </div>
</body>
</html>

根据您的评论,这是我的MainActivity.cs

using Android;
using Android.App;
using Android.OS;
using Android.Webkit;
namespace HIPethics
{
    [Activity(Label = "HIP", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            var webView1 = FindViewById<WebView>(Resource.Id.webView1);
            webView1.Settings.JavaScriptEnabled = true;
            webView1.Settings.CacheMode = CacheModes.NoCache;
            webView1.Settings.SetRenderPriority(WebSettings.RenderPriority.High);


            HIPethics.Assets.Ethicshtml html = new HIPethics.Assets.Ethicshtml();
            webView1.LoadDataWithBaseURL("file:///android_asset/", html.GenerateString(), "text/html", "utf-8", null);

            webView1.SetWebViewClient(new MyWebViewClient());
            webView1.LoadUrl("file:///android_asset/Ethicshtml.cshtml");
        }
    }

    public class MyWebViewClient : Android.Webkit.WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            if (url.Equals("file:///android_asset/MoralMain.cshtml"))
            {
                HIPethics.Assets.MoralMain Moralmain = new HIPethics.Assets.Moralmain();
                view.LoadDataWithBaseURL("file:///android_asset/", Moralmain.GenerateString(), "text/html", "utf-8", null);

            }
            if (url.Equals("file:///android_asset/ProfessionalMain.cshtml"))
            {
                HIPethics.Assets.ProfessionalMain professionalMain = new HIPethics.Assets.ProfessionalMain();
                view.LoadDataWithBaseURL("file:///android_asset/", professionalMain.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/ProfessionalPage1.cshtml"))
            {
                HIPethics.Assets.ProfessionalPage1 ProfessionalPage1 = new HIPethics.Assets.ProfessionalPage1();
                view.LoadDataWithBaseURL("file:///android_asset/", ProfessionalPage1.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/ProfessionalPage2.cshtml"))
            {
                HIPethics.Assets.ProfessionalPage2 ProfessionalPage2 = new HIPethics.Assets.ProfessionalPage2();
                view.LoadDataWithBaseURL("file:///android_asset/", ProfessionalPage2.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/SecurityMain.cshtml"))
            {
                HIPethics.Assets.SecurityMain securityMain = new HIPethics.Assets.SecurityMain();
                view.LoadDataWithBaseURL("file:///android_asset/", securityMain.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/SecurityPage1.cshtml"))
            {
                HIPethics.Assets.SecurityPage1 SecurityPage1 = new HIPethics.Assets.SecurityPage1();
                view.LoadDataWithBaseURL("file:///android_asset/", SecurityPage1.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/SecurityPage2.cshtml"))
            {
                HIPethics.Assets.SecurityPage2 SecurityPage1 = new HIPethics.Assets.SecurityPage2();
                view.LoadDataWithBaseURL("file:///android_asset/", SecurityPage2.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/MoralPage1.cshtml"))
            {
                HIPethics.Assets.MoralPage1 moralPage1 = new HIPethics.Assets.MoralPage1();
                view.LoadDataWithBaseURL("file:///android_asset/", moralPage1.GenerateString(), "text/html", "utf-8", null);
            }
            if (url.Equals("file:///android_asset/MoralPage2.cshtml"))
            {
                HIPethics.Assets.MoralPage2 moralPage2 = new HIPethics.Assets.MoralPage2();
                view.LoadDataWithBaseURL("file:///android_asset/", moralPage2.GenerateString(), "text/html", "utf-8", null);

                return true;
            }
        }
    }
}

如何填写我的代码?

答案 2 :(得分:0)