【问题标题】:Xamarin page loads but doesn't show up on screenXamarin 页面加载但未显示在屏幕上
【发布时间】:2017-12-14 10:09:03
【问题描述】:

我的程序卡在 await 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>

初始化组件方法

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")] 私人无效初始化组件(){ 全局::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(LoginPage)); btnLogin = global::Xamarin.Forms.NameScopeExtensions.FindByName(this, "btnLogin"); }

登录视图模型

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 搜索: '__Internal' ('(null)')。 12-14 12:21:15.190 D/单声道 (22659): 搜索“java_interop_jnienv_call_nonvirtual_float_method_a”。 12-14 12:21:15.190 D/Mono (22659):探测 'java_interop_jnienv_call_nonvirtual_float_method_a'。 12-14 12:21:15.190 D/Mono (22659):发现为 'java_interop_jnienv_call_nonvirtual_float_method_a'。 12-14 12:21:15.220 D/Mono (22659): DllImport 搜索:'__Internal' ('(空值)')。 12-14 12:21:15.220 D/Mono (22659): 搜索 'java_interop_jnienv_call_void_method'。 12-14 12:21:15.225 日/单
(22659):探测“java_interop_jnienv_call_void_method”。 12-14 12:21:15.225 D/Mono (22659):发现为 'java_interop_jnienv_call_void_method'。 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 日/单
(22659): 图像地址引用 System.Diagnostics.Debug[0x8356ac40] -> System.Diagnostics.Debug.dll[0x7fbf0d28]:5 12-14 12:21:19.350 D/单声道 (22659):配置尝试解析: 'System.Diagnostics.Debug.dll.config'。 12-14 12:21:19.350 日/单
(22659):配置尝试解析: '/usr/local/etc/mono/assemblies/System.Diagnostics.Debug/System.Diagnostics.Debug.config'。 12-14 12:21:19.350 D/Mono (22659): 大会参考地址 ASFT.IssueManager[0x7db3d5c0] -> System.Diagnostics.Debug[0x7fc01d28]: 3 将“LoginPage.xaml.cs:32,1”处的挂起断点解决为无效 ASFT.IssueManager.LoginPage.OnDisappearing () [0x0000d]。解决 'LoginPage.xaml.cs:31,1' 处的挂起断点无效 ASFT.IssueManager.LoginPage.OnDisappearing () [0x00001]。 12-14 12:21:24.365 D/Mono (22659):图像地址 System.Diagnostics.Tools[0x83567040] -> System.Diagnostics.Tools.dll[0x835676b0]:2 12-14 12:21:24.365 D/单声道 (22659):准备设置程序集“System.Diagnostics.Tools” (System.Diagnostics.Tools.dll) 12-14 12:21:24.365 D/Mono (22659): 装配 System.Diagnostics.Tools[0x83567040] 添加到域 根域,ref_count=1 12-14 12:21:24.365 D/Mono (22659): AOT: 找不到图像“System.Diagnostics.Tools.dll.so”:dlopen 失败: 图书馆 “/data/app-lib/com.asft.event-68/libaot-System.Diagnostics.Tools.dll.so” 未找到 12-14 12:21:24.365 D/Mono (22659): AOT: image '/usr/local/lib/mono/aot-cache/arm/System.Diagnostics.Tools.dll.so' 未找到:dlopen 失败:库 “/data/app-lib/com.asft.event-68/libaot-System.Diagnostics.Tools.dll.so” 未找到 12-14 12:21:24.365 D/Mono (22659): 配置试图 解析:'System.Diagnostics.Tools.dll.config'。 12-14 12:21:24.365 D/Mono (22659):尝试解析的配置: '/usr/local/etc/mono/assemblies/System.Diagnostics.Tools/System.Diagnostics.Tools.config'。 12-14 12:21:24.365 D/Mono (22659): 大会参考地址 ASFT.IssueManager[0x7db3d5c0] -> System.Diagnostics.Tools[0x83567040]: 2 12-14 12:21:24.365 D/Mono (22659):大会参考地址 System.Diagnostics.Tools[0x83567040] -> System[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):尝试解析的配置:“mscorlib.dll.config”。 12-14 12:21:26.585 D/Mono (22659):配置尝试解析: '/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 日/单
(22659): 图像地址引用 System.Globalization[0x8357a8b0] -> System.Globalization.dll[0x7f17f3d0]: 6 12-14 12:21:26.585 D/Mono
(22659):配置尝试解析: 'System.Globalization.dll.config'。 12-14 12:21:26.585 日/单
(22659):配置尝试解析: '/usr/local/etc/mono/assemblies/System.Globalization/System.Globalization.config'。 12-14 12:21:26.585 D/Mono (22659): 大会参考地址 Xamarin.Forms.Xaml [0x7dbd5a78]-> 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):尝试解析的配置:“mscorlib.dll.config”。 12-14 12:21:26.600 D/Mono (22659):配置尝试解析: '/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/单声道 (22659): 图片地址 System.Runtime.Extensions[0x83595340] -> System.Runtime.Extensions.dll[0x7eb780b0]:6 12-14 12:21:26.805 D/单声道 (22659):配置尝试解析: 'System.Runtime.Extensions.dll.config'。 12-14 12:21:26.805 日/单
(22659):配置尝试解析: '/usr/local/etc/mono/assemblies/System.Runtime.Extensions/System.Runtime.Extensions.config'。 12-14 12:21:26.805 D/Mono (22659): 大会参考地址 Xamarin.Forms.Xaml[0x7dbd5a78] -> 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:值不能为空。参数名称: 来源

应用启动

 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());
    }

【问题讨论】:

  • 你需要在 OnAppearing() 中等待 ShowLoginPage()。
  • 嗯,所做的只是让它在返回后崩溃。活动被销毁时由于生命周期回调导致的空引用错误,可以在那里编辑崩溃
  • 你能展示你的 LoginModel 吗?
  • 我添加了 LoginModel,但我不确定它是否相关。 :)
  • 在您的 xaml 中您绑定用户名和密码,但在您的 ViewModel 中您没有此字段。并且 ViewModel 必须实现 INotifyPropertyChanged

标签: android ios xaml xamarin xamarin.forms


【解决方案1】:

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

  1. LoginPage的构造函数中InitializeComponent();应该排在第一位。
  2. OnAppearing()你必须await ShowLoginPage();,否则代码会继续执行。
  3. 异常指向 XAML,尝试启用XAMLC

尝试将我发现的问题一一排除,希望您能找到解决方案。

【讨论】:

  • 啊,该死的。都试过了。变化不大,很遗憾。 Initalizecomopnent first 只是让它更早地崩溃。不确定 XAMLC 是否做了任何事情,但我在登录页面的类级别启用了它。我只是不明白为什么它不会在继续之前等待我的登录页面。
  • 在 LoginPage 上注释掉大部分代码,并一一取消注释,看看问题出在哪里。崩溃是否发生在 InitializeComponent 上?如果是这样,问题显然出在您的 XAML 中。
  • 崩溃实际上发生在 initalizecomponent 之后。在 initalizecomponent 之后它返回到首先等待 this.Navigation.PushModalAsync(new LoginPage());然后等待 ShowLoginPage();然后崩溃下一行。
  • 什么设置为App的MainPage?等待 ShowLoginPage();显示模式页面还是直接崩溃?
  • 它只是崩溃了。我编辑了应用程序的主页开始,但主页是一团糟,我希望它不相关。它会加载另一个内容页,在该内容页之前是 onappearing 所在的位置。
【解决方案2】:

我在加载页面时遇到了类似的问题,它在启动页面中停止,因为我的 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();
 }

【讨论】:

  • 我知道,但我没有 50 声望,我无法在主帖中发表评论
  • 干得好!但是,它无法单击登录按钮并输入您的用户名/密码,而是继续进行。 ://
  • 但是你可以在你的ViewModel绑定按钮上点击。但它现在是否正常工作并且您会看到您的登录页面?
  • 不,仍然没有。根本看不到登录页面。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2015-06-18
  • 2021-03-20
  • 1970-01-01
相关资源
最近更新 更多