【问题标题】:InitializeComponent(); in Prism Library Does not exist?初始化组件();在 Prism 库中不存在?
【发布时间】:2017-04-27 19:58:58
【问题描述】:

使用 xamarin 表单 pcl 和 Prism 库

 public class App : PrismApplication
   {  
    public App(IPlatformInitializer initializer = null) : base(initializer) 
    { }
    protected override void OnInitialized()
    {

        //InitializeComponent();
        NavigationService.NavigateAsync("MainPage");
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MainPage>("MainPage");
        Container.RegisterTypeForNavigation<LoginPage>("LoginPage");
        Container.RegisterTypeForNavigation<ServicePage>("ServicePage");
    }

}

在 MainActivity.cs 中

  public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        //TabLayoutResource = Resource.Layout.Tabbar;
        //ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App(new AndroidInitializer()));
    }
    public class AndroidInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IUnityContainer container)
        {
        }


    }

主页.cs

 namespace PRProject.Views
    {
        public partial class MainPage : ContentPage
   {
    public MainPage()
    {
        InitializeComponent();
        BackgroundImage = "Background.png";
    }
}

和 MainPage.Xaml

   <?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="PRProject.Views.MainPage" BackgroundImage="Background.png"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True">

  <StackLayout VerticalOptions="End" HorizontalOptions="Center" Orientation="Vertical" Margin="20,0,20,10">
    <ScrollView VerticalOptions="FillAndExpand" Orientation="Vertical" HorizontalOptions="Center">
      <StackLayout>


        <StackLayout VerticalOptions="End" HorizontalOptions="Center" Orientation="Horizontal" Margin="0,0,0,20">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>


            <Image Grid.Column="1" Source="signin.png">
              <Image.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding NavigateCommand}" />
              </Image.GestureRecognizers>
            </Image>
          </Grid>

        </StackLayout>
      </StackLayout>
    </ScrollView>
  </StackLayout>
  </ContentPage>

和视图模型

namespace PRProject.ViewModels
    {
     public class MainPageViewModel : BindableBase, INavigationAware
    {
    private INavigationService _navigationService;

    public DelegateCommand NavigateCommand { get; set; }
    public MainPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
        NavigateCommand = new DelegateCommand(Navigate);
    }
    private void Navigate()
    {
        _navigationService.NavigateAsync("LoginPage");
    }
     }}
  • 问题是
    1. 我没有在 OnInitialized() 中调用 InitializeCompenent()
    2. 当它导航到 MainPage 时,它​​显示为空白页
    3. 如果 Main Activity 类继承自 Main Activity 中的另一个额外问题 global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 它返回空异常
    4. 在 Main Activity 内部,Registertype 的参数是 IContainer,但对我来说是 IUnityContainer。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    如果您的 App 类有一个随附的 XAML 文件,则它只会有一个 InitializeComponent 方法。如果不这样做,实现此目的的最简单方法是从“新建文件”对话框将App 类重新创建为“Forms ContentPage XAML”。然后确保 XAML 文件也使用 PrismApplication 类。

    您的 XAML 文件应如下所示:

    <prism:PrismApplication 
        xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
        x:Class="SampleApp">
        <!-- other XAML can go here -->
    </prism:PrismApplication>
    

    请注意,不需要将 XAML 文件与 Prism.Forms 一起使用(尽管我认为这是推荐的),因此您也可以省略 InitializeComponent 调用。

    收到更多信息后编辑

    在您的MainPage.xaml 中,您在StackLayout 中放置了一个ScrollView,其中StackLayoutVerticalOptions 设置为EndStackLayout 无法知道它应该变得多大,因为ScrollView 是它唯一的孩子。删除外部StackLayout,很可能会在MainPage 上显示一些内容。

    如果您只是想尝试一种简单的方法来排除 Prism 的问题,您还可以将 XAML 替换为简单的 Label 元素。

    基于您的 XAML 的另一句话:尽量避免嵌套不必要的 StackLayouts,因为它会导致性能下降。

    【讨论】:

    • 我删除了滚动视图,主页也是空白,但过了一会儿它返回(System.InvalidOperationException:序列不包含元素)异常
    • 尝试在您的 MainPage 中仅使用 Label,以排除任何其他问题。是否调用了 MainPage 构造函数?
    • 是的,MainPage 构造函数已成功调用,没有任何问题或异常,但页面仍为空白。
    • 我也尝试创建 App.Xaml.cs 但我遇到了这个连线错误命名空间“Prism”中不存在类型或命名空间名称“DryIoc”(您是否缺少程序集引用?) .
    • 我创建了一个测试应用程序,添加了您的代码并添加了 2 个 NuGet 包(在 PCL 和平台项目中):Prism.Forms 和 Prism.Unity.Forms。如果我只是这样做,我也会因为ScrollView 而得到一个空白页,就像我在回答中提到的那样(设置背景颜色以便于布局调试)。仅添加 Label 使其显示(如果您设置 Text 属性:-))。我建议你从头开始一个新项目并与之进行比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2013-01-13
    • 2020-10-24
    • 2010-10-03
    • 2023-03-13
    相关资源
    最近更新 更多