在跨平台共享代码Xamarin登录屏幕后打开平台特定屏幕?

时间:2017-08-08 10:18:32

标签: c# xamarin cross-platform

我想从登录屏幕为Android设备和iOS设备打开Android活动。我写的登录屏幕的代码是在共享的可移植代码区域。我使用了界面和#if ANDROID #endif,但它也无法正常工作。 Android屏幕仅包含textview,iOS屏幕将包含Image。这是我在便携式文件夹中的登录页面

namespace DemoApp
{
    public class HomePage : ContentPage
    {
        public HomePage()
        {
            var title = new Label
            {
                Text = "Welcome to CloudCakes",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };


            var email = new Entry
            {
                Placeholder = "E-Mail",
            };

            var password = new Entry
            {
                Placeholder = "Password",
                IsPassword = true
            };

            var login = new Button
            {
                Text = "Login"
            };

            // With the `PushModalAsync` method we navigate the user
            // the the orders page and do not give them an option to
            // navigate back to the Homepage by clicking the back button
            login.Clicked += (sender, e) =>
         {
             App.UserPreferences.Open();
             // await Navigation.PushModalAsync(new MainPage());
         };

            Content = new StackLayout
            {
                Padding = 30,
                Spacing = 10,
                Children = { title, email, password, login}
            };
        }
    }
}

而且,这是Droid文件夹中的页面,我想点击登录按钮导航 name space { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Page1 : IUserPreferences { public Page1() { InitializeComponent(); } public void Open() { var title = new Label { Text = "Welcome to Xamarin Forms IOS Screen!!", FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)), HorizontalOptions = LayoutOptions.CenterAndExpand, }; Content = new StackLayout { Padding = 30, Spacing = 10, Children = { title } }; } } }

Open是一种存在于便携式文件夹中的接口方法。

1 个答案:

答案 0 :(得分:0)

我假设你正在使用Xamarin.Forms,所以你需要做的事实上很容易实现。

不是为每个平台设置单独的页面,而是使用以下代码创建一个新的XAML页面:

<强> 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="YourApp.Views.MainPage">
  <ContentView>
    <OnPlatform x:TypeArguments="View">
      <OnPlatform.iOS>
        <Image Source="something.png" />
      </OnPlatform.iOS>
      <OnPlatform.Android>
        <Label Text="Android" />
      </OnPlatform.Android>
    </OnPlatform>
  </ContentView>
</ContentPage>

通过这种方式,您可以控制每个平台的页面内容。

要从LoginPage导航到MainPage,您需要在后面的代码(MainPage.xaml.cs)中实现Clicked事件(或者最好使用Commands)并在那里进行导航:

private void SomeButton_Clicked(object sender, System.EventArgs e)
{
    await this.Navigation.PushAsync(new MainPage());
}

从长远来看,你应该考虑使用ViewModel和命令在代码之外做所有这些。

编辑:由于第一个解决方案对您不起作用,所以该怎么做:

1)核心项目中的子类ContentPage。

public class MainPage : ContentPage { }

2)分别为每个平台实现一个PageRenderer。如上所述,您将能够为页面使用本机布局定义。

Xamarin博客上有一个关于如何实现这一目标的great article。您还可以找到source code on GitHub