【问题标题】:Convert string to activity name C#将字符串转换为活动名称 C#
【发布时间】:2015-07-24 21:53:33
【问题描述】:

我在 Xamarin Android 中使用 ListView 来显示餐厅列表,当单击一个时,我想导航到一个新活动。每个活动都使用以下约定命名:[restaurantName]Activity。 我正在尝试使用意图:

    protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        string t = items[position] + "Activity";
        var intentPlace = new Intent(this, typeof(???));
        StartActivity(intentPlace);
    }

String t 给出了正确的格式,但是放在 typeof() 中的类型是错误的。但是,我不知道用什么来代替“???”,因为我无法使用 setTitle 来创建活动名称。 有人能指出我正确的方向吗?

【问题讨论】:

    标签: c# android xamarin xamarin.android


    【解决方案1】:

    您需要为您的活动附加完整路径。

    var actType = Type.GetType("part of full path" + items[position] + "Activity")
    

    我还没有测试过,但它应该可以工作:)

    protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        var actType = Type.GetType("part of full path" + items[position] + "Activity")
        var intentPlace = new Intent(this, actType);
        StartActivity(intentPlace);
    }
    

    也有用if the assembly with activity has been loaded in the current domain

    【讨论】:

    • 我刚试了一下,虽然它是在我点击项目时编译的,但我得到了错误:'[MonoDroid] System.ArgumentException: type [MonoDroid] Parameter name: Type is not derived from a Java 类型。这是只有 Java 才有的功能吗?
    • 好的,通过现有活动的反射获取类型的完整路径,然后将其放入“完整路径的一部分”?
    • 它成功了,我只需要更改路径名(我在那里有一个额外的“活动”)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-11-09
    • 2016-02-06
    • 2015-04-02
    • 2015-07-26
    • 2012-02-22
    • 2013-08-07
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多