在VS17中模拟时,无法获得简单的Master / Detail模式Xamarin.Forms应用程序

时间:2017-11-04 20:45:09

标签: c# android visual-studio xaml xamarin.forms

我在为Android和iOS构建我的第一个简单的跨平台应用程序时遇到了一些问题。

所以我创建了一个选择这些设置的Visual Studio 2017项目:

  • 类型:Visual C#中的跨平台应用程序(Xamarin)
  • 模板:空白应用
  • UI技术:Xamarin.Forms
  • 代码共享策略:可移植类库(PCL)

并且遵循了一些不同的教程,但代码和模式看起来都是一样的。但经过很多麻烦后,我坚持这个结果:

当试图模仿“Android_Accelerated_Nougat(Android 7.1 - API 25)”时,应用程序打开,菜单有效,但我无法显示“主页”页面,即使我更改为“ Mainpage = new App1.Page1()“in'App.xaml.cs'

当尝试模仿“KitKat(4.4)XXHDPI手机(Android 4.4 - API 19)”时,它根本无法打开。只是瞬间崩溃。 实际上也尝试了一些,他们也立即崩溃了。

Picture of Solution Explorer

关键代码文件:

MainPage.xaml中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App1.MenuItems;
using App1.Views;
using Xamarin.Forms;

namespace App1
{

public partial class MainPage : MasterDetailPage
{

    public List<MasterPageItem> MenuList { get; set; }

    public MainPage()
    {

        InitializeComponent();

        MenuList = new List<MasterPageItem>();

        var page1 = new MasterPageItem() { Title = "Item 1", Icon = "itemIcon1.png", TargetType = typeof(Page1) };
        var page2 = new MasterPageItem() { Title = "Item 2", Icon = "itemIcon2.png", TargetType = typeof(Page2) };
        var page3 = new MasterPageItem() { Title = "Item 3", Icon = "itemIcon3.png", TargetType = typeof(Page3) };

        // Adding menu items to menuList
        MenuList.Add(page1);
        MenuList.Add(page2);
        MenuList.Add(page3);

        // Setting our list to be ItemSource for ListView in MainPage.xaml
        navigationDrawerList.ItemsSource = MenuList;

        // Initial navigation, this can be used for our home page
        Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Page1)));
    }

    // Event for Menu Item selection, here we are going to handle navigation based
    // on user selection in menu ListView
    private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
    {

        var item = (MasterPageItem)e.SelectedItem;
        Type page = item.TargetType;

        Detail = new NavigationPage((Page)Activator.CreateInstance(page));
        IsPresented = false;
    }
}
}

MainPage.xaml.cs中

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

namespace App1.MenuItems
{

public class MasterPageItem
{

    public string Title { get; set; }
    public string Icon { get; set; }
    public Type TargetType { get; set; }
}
}

MasterPageItem.cs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App1.Views.Page1">
<ContentPage.Content>
    <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
        <Label Text="Welcome to FastDelivery Page 22222" />
    </StackLayout>
</ContentPage.Content>
</ContentPage>

的Page1.xaml

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

using Xamarin.Forms;

namespace App1
{
public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        var navPage = new NavigationPage((Page)Activator.CreateInstance(typeof(Page1)));
        var mdp = new MasterDetailPage()
        {
            Master = new MenuPage(),
            Detail = navPage
        };

        MainPage = mdp;
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}
}

所有Page * .xaml文件都是一样的......

App.xaml.cs(在此文件中尝试了很多不同的选项而没有结果......)

            1>------ Build started: Project: App1, Configuration: Debug Any CPU ------
            1>  App1 -> C:\ShortApps\App1\App1\App1\bin\Debug\App1.dll
            2>------ Build started: Project: App1.Android, Configuration: Debug Any CPU ------
            2>  App1.Android -> C:\ShortApps\App1\App1\App1.Android\bin\Debug\App1.Android.dll
            3>Starting deploy 5" KitKat (4.4) XXHDPI Phone ...
            3>Starting emulator 5" KitKat (4.4) XXHDPI Phone ...
            3>Validating emulator arguments...
            3>Determining if emulator is already running...
            3>Preparing virtual machine...
            3>Launching emulator...
            3>Emulator launched successfully.
            3>Emulator 5" KitKat (4.4) XXHDPI Phone is running.
            3>Waiting for emulator to be ready...
            3>Deploying 5" KitKat (4.4) XXHDPI Phone ...
            3>Build started.
            3>Project "App1.Android.csproj" (Install target(s)):
            3>C:\Program Files\Java\jdk1.8.0_112\\bin\keytool.exe -list -alias androiddebugkey -storepass android -keypass android -keystore "C:\Users\Chronic\AppData\Local\Xamarin\Mono for Android\debug.keystore" 
            3>C:\Program Files\Java\jdk1.8.0_112\\bin\jarsigner.exe -keystore "C:\Users\Chronic\AppData\Local\Xamarin\Mono for Android\debug.keystore" -storepass android -keypass android -digestalg SHA1 -sigalg md5withRSA -signedjar bin\Debug\\com.companyname.App1-Signed-Unaligned.apk C:\ShortApps\App1\App1\App1.Android\obj\Debug\android\bin\com.companyname.App1.apk androiddebugkey 
            3>No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2047-10-28) or after any future revocation date.
            3>C:\Program Files (x86)\Android\android-sdk\build-tools\25.0.3\zipalign.exe 4 "C:\ShortApps\App1\App1\App1.Android\bin\Debug\com.companyname.App1-Signed-Unaligned.apk" "bin\Debug\\com.companyname.App1-Signed.apk" 
            3>Done building project "App1.Android.csproj".
            3>Build succeeded.
            3>Deploy successfully on 5" KitKat (4.4) XXHDPI Phone
            ========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
            ========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========

Git:https://github.com/napzter13/masterdetail

//输出

构建日志:

            Android application is debugging.
            11-04 23:11:00.690 D/dalvikvm( 1254): Late-enabling CheckJNI
            11-04 23:11:00.706 D/dalvikvm( 1254): Trying to load lib /data/app-lib/com.companyname.App1-2/libmonodroid.so 0xa9052830
            11-04 23:11:00.706 D/dalvikvm( 1254): Added shared lib /data/app-lib/com.companyname.App1-2/libmonodroid.so 0xa9052830
            11-04 23:11:00.722 W/monodroid( 1254): Creating public update directory: `/data/data/com.companyname.App1/files/.__override__`
            11-04 23:11:00.722 W/monodroid( 1254): Using override path: /data/data/com.companyname.App1/files/.__override__
            11-04 23:11:00.722 W/monodroid( 1254): Using override path: /storage/emulated/0/Android/data/com.companyname.App1/files/.__override__
            11-04 23:11:00.722 W/monodroid( 1254): Using override path: /storage/emulated/0/../legacy/Android/data/com.companyname.App1/files/.__override__
            11-04 23:11:00.726 W/monodroid( 1254): Trying to load sgen from: /data/data/com.companyname.App1/files/.__override__/libmonosgen-2.0.so
            11-04 23:11:00.726 W/monodroid( 1254): Trying to load sgen from: /storage/emulated/0/Android/data/com.companyname.App1/files/.__override__/libmonosgen-2.0.so
            11-04 23:11:00.730 W/monodroid( 1254): Trying to load sgen from: /storage/emulated/0/../legacy/Android/data/com.companyname.App1/files/.__override__/libmonosgen-2.0.so
            11-04 23:11:00.730 W/monodroid( 1254): Trying to load sgen from: /data/app-lib/com.companyname.App1-2/libmonosgen-2.0.so
            11-04 23:11:00.734 W/monodroid( 1254): Trying to load sgen from: /data/data/com.companyname.App1/files/.__override__/links/libmonosgen-2.0.so
            11-04 23:11:00.734 W/monodroid-debug( 1254): Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:29224,server=y,embedding=1
            11-04 23:11:00.894 W/monodroid-debug( 1254): Accepted stdout connection: 43
            11-04 23:11:01.694 D/Mono    ( 1254): Image addref mscorlib[0xb80f96d0] -> mscorlib.dll[0xb80f2620]: 2
            11-04 23:11:01.694 D/Mono    ( 1254): Prepared to set up assembly 'mscorlib' (mscorlib.dll)
            11-04 23:11:01.698 D/Mono    ( 1254): AOT: image 'mscorlib.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.App1-2/libaot-mscorlib.dll.so" not found
            11-04 23:11:01.698 D/Mono    ( 1254): AOT: image '/usr/local/lib/mono/aot-cache/x86/mscorlib.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.App1-2/libaot-mscorlib.dll.so" not found
            11-04 23:11:01.698 D/Mono    ( 1254): Config attempting to parse: 'mscorlib.dll.config'.
            11-04 23:11:01.698 D/Mono    ( 1254): Config attempting to parse: '/usr/local/etc/mono/assemblies/mscorlib/mscorlib.config'.
            11-04 23:11:01.746 D/Mono    ( 1254): Assembly mscorlib[0xb80f96d0] added to domain RootDomain, ref_count=1
            Could not connect to the debugger.

调试日志:

{{1}}

Screenshot for crash

Android输出日志:https://github.com/napzter13/masterdetail/blob/master/Android_OUTPUT.txt

我希望有人可以帮助我朝着正确的方向前进,谢谢。

2 个答案:

答案 0 :(得分:1)

我不知道您的App.xaml.cs中发生了什么,并且可能存在一些问题。但是这里通常会创建一个MDP。你可以在App.xaml.cs的代码隐藏中执行此操作,它比XAML更容易一些。但是你在XAML中完成了所有其他页面。

#include "thread_pool.hpp"
#include <ctime>
#include <iostream>
#include <vector>


using namespace boost;
using namespace std;


struct T {
  vector<int> nums = {};
};


typedef boost::shared_ptr<T> Tptr;
typedef vector<Tptr> TptrVector;


void create_T(const int i, TptrVector& v) {
  v[i] = Tptr(new T());
  T& t = *v[i].get();
  for (int i = 0; i < 100; i++) {
    t.nums.push_back(i);
  }
}


int main(int argc, char* argv[]) {
  clock_t begin, end;
  double elapsed;

  // define and parse program options

  if (argc != 3) {
    cout << argv[0] << " <num iterations> <num threads>" << endl;
    return 1;
  }
  int iterations = stoi(argv[1]),
      threads    = stoi(argv[2]);

  // create thread pool
  ThreadPool tp(threads);

  // non-threaded
  cout << "non-thread" << endl;
  begin = clock();

  TptrVector v(iterations);
  for (int i = 0; i < iterations; i++) {
    create_T(i, v);
  }

  end = clock();
  elapsed = double(end - begin) / CLOCKS_PER_SEC;
  cout << elapsed << " seconds" << endl;

  // threaded
  cout << "threaded" << endl;
  begin = clock();

  TptrVector v2(iterations);
  for (int i = 0; i < iterations; i++) {
    tp.submit(boost::bind(create_T, i, v2));
  }
  tp.stop();

  end = clock();
  elapsed = double(end - begin) / CLOCKS_PER_SEC;
  cout << elapsed << " seconds" << endl;

  return 0;
}

您可以在每个页面中放置一些示例XAML,以确保它可见。

答案 1 :(得分:0)

好的,我通过以下方式解决了这个问题:

  • Droid项目属性&gt;发布和调试&gt; Android选项&gt;取消选中“使用共享运行时”和“使用快速部署”。
  • Droid项目属性&gt;发布和调试&gt; Android选项&gt;高级&gt;支持的体系结构&gt;检查全部。
  • 用于建筑,使用:构建&gt;部署App1.Android。而不是:Build&gt;部署解决方案。

是的,它无法工作的原因是我编译错误的架构,因此它只能在其中一个仿真器版本上运行。快速部署也搞砸了一些事情,所以请保持关闭。

如果由于某种原因仍然不会出现任何.Apk文件,请右键单击Android项目并单击“存档...”并通过签名制作分布式副本。 (因此取消选中“使用共享运行时”)