使用xamarin形式pcl&棱镜图书馆
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)
{
}
}
主要的Page.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");
}
}}
答案 0 :(得分:2)
如果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>
请注意,不要求在Prism.Forms中使用XAML文件(虽然我认为这是推荐的),所以您也可以省略InitializeComponent
电话。
收到更多信息后进行修改
在MainPage.xaml
中,您在ScrollView
中放置了StackLayout
,其中StackLayout
的{{1}}设置为VerticalOptions
。由于End
是唯一的孩子,StackLayout
无法知道应该有多大。移除外部ScrollView
,很可能会出现StackLayout
上的内容。
如果您只是想尝试一种简单的方法来排除Prism是个问题,那么您也可以用简单的MainPage
元素替换XAML。
基于您的XAML的另一个评论:尽量避免嵌套不必要的Label
,因为它会导致性能下降。