Xamarin页面加载但未显示在屏幕上

时间:2017-12-14 10:09:03

标签: android ios xaml xamarin xamarin.forms

我有一个问题,我的程序卡在等待Pushmodalasync部分。它应该被卡在那里,直到你登录到应用程序,但登录页面永远不会加载,它只是卡在加载屏幕上。

在iOS和Android上存在同样的问题。

加载登录页面

protected async override void OnAppearing()
{
    if (App.Client.Initilized == false)
    {
        await App.Client.Init();
    }

    if (App.Client.LoggedIn == false)
    {
        ShowLoginPage();
        return;
    }

    ShowIssuesFromCurrentLocation();
} 

调用登录页面

private async void ShowLoginPage()
{
    MessagingCenter.Subscribe<LoginPage>(this, "OnLoginPageClosed", (sender) =>
    {
        MessagingCenter.Unsubscribe<LoginPage>(this, "OnLoggedIn");

        if (App.Client.LoggedIn == false)
        {
            Issues.Clear();
        }
        else
        {
            ShowIssuesFromCurrentLocation();
        }
    });

    await this.Navigation.PushModalAsync(new LoginPage());
}

登录页面本身

public partial class LoginPage : ContentPage
{
    public LoginViewModel data;
    public LoginViewModel Data { get { return data; } }
    public LoginPage()
    {
        this.data = App.Client.GetCurrentLoginModel();
        this.BindingContext = this.data;
        this.Title = "Login";
        InitializeComponent();
    }

登录类的xaml部分

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ASFT.IssueManager.LoginPage" Padding="10">
  <ContentPage.Resources>
    <ResourceDictionary>
      <x:String x:Key="Labelfont">Medium</x:String>
      <x:String x:Key="Titlefont">Large</x:String>
    </ResourceDictionary>
  </ContentPage.Resources>
  <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Spacing="10" >
    <StackLayout Orientation="Vertical" VerticalOptions="Start" HorizontalOptions="Center" Spacing="10" WidthRequest="350">
      <Label Text="Login" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Titlefont}"/>
      <BoxView HeightRequest="5" Color="Gray"/>
      <Label Text="Host" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
      <Entry Placeholder="Host/URL" Text="{Binding Host}" />
      <Label Text="UserName" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
      <Entry Placeholder="User name / Account" Text="{Binding Username}" />
      <Label Text="Password" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
      <Entry Placeholder="Password" IsPassword="true" Text="{Binding Password}" />
      <BoxView HeightRequest="5" Color="Gray"/>
      <Button x:Name="btnLogin" Text="Login" HorizontalOptions="FillAndExpand" Clicked="OnButtonLogin" WidthRequest="100"/>
    </StackLayout>
  </StackLayout>
</ContentPage>

InitializeComponent方法

[global :: System.CodeDom.Compiler.GeneratedCodeAttribute(&#34; Xamarin.Forms.Build.Tasks.XamlG&#34;,&#34; 0.0.0.0&#34;)]          private void InitializeComponent(){                     global :: Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this,typeof(LoginPage));                     btnLogin = global :: Xamarin.Forms.NameScopeExtensions.FindByName(this,&#34; btnLogin&#34;);                 }

loginview模型

public LoginViewModel GetCurrentLoginModel()
    {
      // DEBUG
      if (state.Username.Length == 0)
        state.Username = "mudemo";
      if (state.Host.Length == 0)
        state.Host = "";

      // VERY DEBUG. - REMOVE 
      //String password = "4R5zE6mw";
      String password = "mudemo";

      return new LoginViewModel
      {
        // default debug account
        Host = state.Host,
        Username = state.Username,
        Password = password
      };
    }

错误讯息:

我使用xamarinsforms 2.5.0.12,这是最新的稳定版。

  

12-14 12:21:15.185 D / AbsListView(22659):unregisterIRListener()是   12-14 12:21:15.190 D / Mono(22659):DllImport搜索:   &#39; __内部&#39; (&#39;(空)&#39)。 12-14 12:21:15.190 D / Mono(22659):   正在搜索&#39; java_interop_jnienv_call_nonvirtual_float_method_a&#39;。   12-14 12:21:15.190 D / Mono(22659):探索   &#39; java_interop_jnienv_call_nonvirtual_float_method_a&#39 ;. 12-14   12:21:15.190 D / Mono(22659):发现为   &#39; java_interop_jnienv_call_nonvirtual_float_method_a&#39 ;. 12-14   12:21:15.220 D / Mono(22659):DllImport搜索:&#39; __内部&#39;   (&#39;(空)&#39)。 12-14 12:21:15.220 D / Mono(22659):正在寻找   &#39; java_interop_jnienv_call_void_method&#39 ;. 12-14 12:21:15.225 D / Mono
  (22659):探索&#39; java_interop_jnienv_call_void_method&#39;。 12-14   12:21:15.225 D / Mono(22659):发现为   &#39; java_interop_jnienv_call_void_method&#39 ;. 12-14 12:21:15.280   D / AbsListView(22659):unregisterIRListener()被称为12-14   12:21:19.350 D / Mono(22659):卸载图像   System.Diagnostics.Debug.dll [0x8356da00]。 12-14 12:21:19.350 D / Mono
  (22659):图像addref System.Diagnostics.Debug [0x8356ac40] - &gt;   System.Diagnostics.Debug.dll [0x7fbf0d28]:5 12-14 12:21:19.350 D / Mono   (22659):Config尝试解析:   &#39; System.Diagnostics.Debug.dll.config&#39 ;. 12-14 12:21:19.350 D / Mono
  (22659):Config尝试解析:   &#39; /usr/local/etc/mono/assemblies/System.Diagnostics.Debug/System.Diagnostics.Debug.config' ;.   12-14 12:21:19.350 D / Mono(22659):Assembly Ref addref   ASFT.IssueManager [0x7db3d5c0] - &gt; System.Diagnostics.Debug [0x7fc01d28]:   3已解决的挂起断点位于&gt; LoginPage.xaml.cs:32,1&#39;无效   ASFT.IssueManager.LoginPage.OnDisappearing()[0x0000d]。解决   待定断点在&#39; LoginPage.xaml.cs:31,1&#39;无效   ASFT.IssueManager.LoginPage.OnDisappearing()[0x00001]。 12-14   12:21:24.365 D / Mono(22659):图像addref   System.Diagnostics.Tools [0x83567040] - &gt;   System.Diagnostics.Tools.dll [0x835676b0]:2 12-14 12:21:24.365 D / Mono   (22659):准备设置装配&System C.Diagnostics.Tools&#39;   (System.Diagnostics.Tools.dll)12-14 12:21:24.365 D / Mono(22659):   装配System.Diagnostics.Tools [0x83567040]添加到域   RootDomain,ref_count = 1 12-14 12:21:24.365 D / Mono(22659):AOT:   image&#39; System.Diagnostics.Tools.dll.so&#39;找不到:dlopen失败:   图书馆   &#34; /data/app-lib/com.asft.event-68/libaot-System.Diagnostics.Tools.dll.so"   未找到12-14 12:21:24.365 D / Mono(22659):AOT:图像   &#39; /usr/local/lib/mono/aot-cache/arm/System.Diagnostics.Tools.dll.so'   找不到:dlopen失败:库   &#34; /data/app-lib/com.asft.event-68/libaot-System.Diagnostics.Tools.dll.so"   未找到12-14 12:21:24.365 D / Mono(22659):试图尝试   解析:&#39; System.Diagnostics.Tools.dll.config&#39;。 12-14 12:21:24.365   D / Mono(22659):配置尝试解析:   &#39; /usr/local/etc/mono/assemblies/System.Diagnostics.Tools/System.Diagnostics.Tools.config' ;.   12-14 12:21:24.365 D / Mono(22659):Assembly Ref addref   ASFT.IssueManager [0x7db3d5c0] - &gt; System.Diagnostics.Tools [0x83567040]:   2 12-14 12:21:24.365 D / Mono(22659):Assembly Ref addref   System.Diagnostics.Tools [0x83567040] - &gt;系统[0x7ed17940]:12 12-14   12:21:26.585 W / Mono(22659):加载装配的请求   mscorlib v4.0.0.0重新映射到v2.0.5.0 12-14 12:21:26.585 D / Mono
  (22659):卸载映像mscorlib.dll [0x835670b0]。 12-14 12:21:26.585   D / Mono(22659):配置尝试解析:&#39; mscorlib.dll.config&#39;。   12-14 12:21:26.585 D / Mono(22659):配置试图解析:   &#39; /usr/local/etc/mono/assemblies/mscorlib/mscorlib.config' ;. 12-14   12:21:26.585 D / Mono(22659):卸载图像   System.Globalization.dll [0x83579b48]。 12-14 12:21:26.585 D / Mono
  (22659):图像addref System.Globalization [0x8357a8b0] - &gt;   System.Globalization.dll [0x7f17f3d0]:6 12-14 12:21:26.585 D / Mono
  (22659):Config尝试解析:   &#39; System.Globalization.dll.config&#39 ;. 12-14 12:21:26.585 D / Mono
  (22659):Config尝试解析:   &#39; /usr/local/etc/mono/assemblies/System.Globalization/System.Globalization.config' ;.   12-14 12:21:26.585 D / Mono(22659):Assembly Ref addref   Xamarin.Forms.Xaml [0x7dbd5a78] - &gt; System.Globalization [0x7f17ffd8]:4   加载程序集:System.Diagnostics.Tools.dll [外部] 12-14   12:21:26.600 W / Mono(22659):加载装配的请求   mscorlib v4.0.0.0重新映射到v2.0.5.0 12-14 12:21:26.600 D / Mono
  (22659):卸载映像mscorlib.dll [0x835670b0]。 12-14 12:21:26.600   D / Mono(22659):配置尝试解析:&#39; mscorlib.dll.config&#39;。   12-14 12:21:26.600 D / Mono(22659):Config试图解析:   &#39; /usr/local/etc/mono/assemblies/mscorlib/mscorlib.config' ;. 12-14   12:21:26.805 D / Mono(22659):卸载图像   System.Runtime.Extensions.dll [0x8359dec8]。 12-14 12:21:26.805 D / Mono   (22659):图像addref System.Runtime.Extensions [0x83595340] - &gt;   System.Runtime.Extensions.dll [0x7eb780b0]:6 12-14 12:21:26.805 D / Mono   (22659):Config尝试解析:   &#39; System.Runtime.Extensions.dll.config&#39 ;. 12-14 12:21:26.805 D / Mono
  (22659):Config尝试解析:   &#39; /usr/local/etc/mono/assemblies/System.Runtime.Extensions/System.Runtime.Extensions.config' ;.   12-14 12:21:26.805 D / Mono(22659):Assembly Ref addref   Xamarin.Forms.Xaml [0x7dbd5a78] - &gt;   System.Runtime.Extensions [0x7eb77b68]:4 12-14 12:21:31.060   I / Choreographer(22659):跳过946帧!申请可能是   在主线程上做了太多工作。 12-14 12:21:31.075   D / AbsListView(22659):unregisterIRListener()被称为Unhandled   例外:

     

System.ArgumentNullException:值不能为null。参数名称:   源

App start

 public App()
    {
      Client = new AppIssueClient();
      // The root page of your application
      MainPage = GetMainPage();
    }

    public async void Init()
    {
      await Client.Init();
    }

    public static Page GetMainPage()
    {
      var p = new NavigationPage(new IssuesPage(-1));
      App.Navigation = p.Navigation;
      return p;//  new NavigationPage(new HomePage());
    }

2 个答案:

答案 0 :(得分:2)

您的代码示例中发现了多个可能的问题:

  1. LoginPage InitializeComponent();的构造函数中应首先出现。
  2. OnAppearing()中,您必须await ShowLoginPage();,否则代码将继续执行。
  3. 异常指向XAML,尝试启用XAMLC
  4. 尽量消除我逐一发现的问题,希望你能找到解决方案。

答案 1 :(得分:0)

我在加载页面遇到了类似问题,因为我遇到ViewModel问题,所以它在启动页面停止了。你能展示你的LoginModel吗?

尝试代替此

await this.Navigation.PushModalAsync(new LoginPage());

使用:

MainPage = new LoginPage();

- 编辑 在您的ViewModel中:

public class LoginViewModel : INotifyPropertyChanged
{
    String username;
    public String Username{
        get{
            return username;
        }set{
            username = value;
            RaisePropertyChanged("Username");
        }
    }

    String password;
    public String Password{
        get{
            return password;
        }set{
            password = value;
            RaisePropertyChanged("Password");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

然后在您的ContentPage中使用:

public partial class LoginPage : ContentPage
{
  public LoginViewModel data;
  public LoginViewModel Data { get { return data; } }
  public LoginPage()
  {
     //this.data = App.Client.GetCurrentLoginModel();
     this.BindingContext = new LoginViewModel() // or you get from App.cs
     this.Title = "Login";
     InitializeComponent();
 }