【问题标题】:How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener如何通过 onclick 侦听器从 Xamarin Android Activity 打开 Xamarin Forms 页面
【发布时间】:2019-06-29 11:52:02
【问题描述】:

我正在向我的应用程序添加一个辅助功能,当用户按住主页按钮时,一个 android 活动将打开,用户可以单击该卡片,然后打开相关的 xamarin 表单页面,例如当数学点击卡片 MathPage xamarin 表单页面打开这里是我的 android 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_tools_home_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:id="@+id/whole"
        tools:context="${relativePackage}.${activityClass}"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp">
        <LinearLayout
            android:layout_width="wrap_content"
            android:id="@+id/scroll"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:id="@+id/one"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardBackgroundColor="@color/cardsbg"
                app:cardElevation="3dp"
                android:clickable="false"
                tools:layout_conversion_absoluteX="16dp"
                tools:layout_conversion_absoluteY="8dp"
                tools:layout_conversion_absoluteWidth="379dp"
                tools:layout_conversion_absoluteHeight="540dp">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <android.support.v7.widget.CardView
                        android:id="@+id/webcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="8dp"
                        android:foreground="?android:attr/selectableItemBackground"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="web">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/webicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/web" />
                            <TextView
                                android:id="@+id/web"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/webicon"
                                android:text="Internet"
                                android:paddingLeft="8dp"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/mailcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/webcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="email">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/emailicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/email" />
                            <TextView
                                android:id="@+id/email"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:paddingLeft="8dp"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/emailicon"
                                android:text="Email"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/mathcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/mailcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="math">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/musicicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/math" />
                            <TextView
                                android:id="@+id/music"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/musicicon"
                                android:text="Math"
                                android:paddingLeft="8dp"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/scicard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/mathcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="science">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/sci" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Science"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/englishcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/scicard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="english">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/handwrittin" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Handwriting"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/flashcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/englishcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="flashcard">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/flashcards" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Flashcards"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <RelativeLayout
                        android:id="@+id/bttom"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/flashcard"
                        android:paddingTop="8dp"
                        android:layout_marginBottom="8dp">
                        <Button
                            android:id="@+id/button_2"
                            android:onClick="cam"
                            android:foreground="?android:attr/selectableItemBackground"
                            android:layout_alignParentLeft="true"
                            android:layout_marginLeft="50dp"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:background="@drawable/settings" />
                        <Button
                            android:id="@+id/button_1"
                            android:onClick="settings"
                            android:foreground="?android:attr/selectableItemBackground"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="50dp"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:background="@drawable/settings" />
                    </RelativeLayout>
                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
        <Button
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:id="@+id/button_4"
            android:onClick="now"
            android:layout_marginTop="-42dp"
            android:layout_below="@+id/scroll"
            android:foreground="?android:attr/selectableItemBackground"
            android:clipToPadding="false"
            android:layout_centerHorizontal="true"
            android:background="@drawable/books" />
    </RelativeLayout>
</RelativeLayout>

这是我的代码:

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

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;

namespace AppName.Droid
{
[Activity(LaunchMode = LaunchMode.SingleInstance, Theme = "@style/Theme.Transparent")]
    [IntentFilter(new[] { Intent.ActionAssist }, Categories = new[] { Intent.CategoryDefault })]    
    public class ToolBelt : FormsApplicationActivity
    {

        Button mathbutton;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.ToolBelt);

        }


        //Code That Opens The Math  Part!

        [Java.Interop.Export("math")] // The value found in android:onClick attribute.
        public void btnOneClick4(View v) // Does not need to match value in above attribute.
        {

            //code to open the MathPage Forms Page Goes here
        }

        //Code That Opens The Internet  App!

        [Java.Interop.Export("web")] // The value found in android:onClick attribute.
        public void btnOneClick8(View v) // Does not need to match value in above attribute.
        {
            var uri = Android.Net.Uri.Parse("http://www.google.com");
            var intent = new Intent(Intent.ActionView, uri);
            StartActivity(intent);
        }



        //Code That Opens The Gmail App!

        [Java.Interop.Export("email")] // The value found in android:onClick attribute.
        public void btnOneClick3(View v) // Does not need to match value in above attribute.
        {
            var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.gm");
            StartActivity(intent);
        }
    }
}

【问题讨论】:

  • 您需要启动 MainLauncher Activity(初始化 Xamarin.Forms 的那个)并带有一些 Intent 附加功能,然后将这些附加功能传递到表单的应用程序中,并根据该信息打开适当的基于表单的页面.
  • @SushiHangover 感谢您的回复!你能给我一个链接或一些示例代码吗?

标签: c# xamarin xamarin.android xamarin.forms


【解决方案1】:

这是一个简单的例子……

基于非表单的页面选择Activity

用户点击一个按钮,我们创建一个Intent 来打开基于主表单的Activity 并添加一个字符串Extra 以及该用户选择的“页面”。

[Activity(Label = "PageSelectionActivity", Theme = "@style/MyTheme",  Name = "com.sushhangover.openpage.PageSelectionActivity")]
public class PageSelectionActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.PageSelection);
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Click += delegate 
        {
            var intent = new Intent(ApplicationContext, typeof(MainActivity));
            intent.PutExtra("page", "StackOverflowPage");
            StartActivity(intent);          
        };
    }
}

修改您的MainActivity 以获取Extra(如果有)并将其传递给App .actor:

[Activity(Label = "OpenPage.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        var page = Intent.GetStringExtra("page");
        LoadApplication(new App(page));
    }
}

修改 App 默认 .actor 以接受参数并对其进行操作:

public class App : Application
{
    public App(string pageName = "SomeDefaultMainApplicationEntryPage")
    {
        switch (pageName)
        {
            case "StackOverflowPage":
                MainPage = new NavigationPage(new StackOverflowPage);
                break;
            default:
                MainPage = new NavigationPage(new SomeDefaultMainApplicationEntryPage);
                break;
        }
    }
}

测试

您可以使用adb打开页面选择Activity来启动它:

adb shell am start -n com.sushhangover.openpage/.PageSelectionActivity

注意:您需要处理Xamarin.Form 页面生命周期,因为它可能会变得混乱,这取决于您的ToolBelt Activity 是否在用户已经打开您的应用程序的情况下打开并且它正在进行一些活动,您通过重新启动应用程序来破坏他们的工作....

【讨论】:

  • 非常感谢您提供的示例,我尝试了您的示例,当我单击数学按钮时首先遇到了一些问题,它会打开应用程序的表单部分,但不会打开主页数学页面继承了基于您示例的当前代码的 gitlist:gist.github.com/TeamBrainStorm/aaedf7a44440895960d23052d6de1b32 你能看看,让我知道我做错了什么吗?在此先感谢:)
  • @Phoneswapshop 它看起来 对,在LoadApplication(new App(page)); 行上放置断点页面变量的值是多少?是“数学”吗?还是空?
  • 在 LoadApplication(new App(page)) 上放置断点后;我发现页面变量为空我该如何解决这个问题?请让我知道并感谢您迄今为止的所有帮助! :)
  • @Phoneswapshop 这意味着找不到具有“page”键的额外内容,在intent.PutExtra("page", "Math");行和switch (pageName)行上放置断点并跟踪结果以确保页面正在正确传递...
  • 我按照你说的做了,pageName 值为 null 以及如何解决这个问题的任何提示?请告诉我:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2018-04-12
  • 2019-08-13
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多