Xamarin.Forms - 检测方向&调整页面

时间:2014-06-30 11:49:36

标签: mobile xamarin orientation xamarin.forms

我想知道设备(Android,iOS和Windows Phone)在我构建页面时的方向。该页面有一个包含3个列定义的网格,一旦方向更改为横向,就应该有5个列定义。

        Grid grid = new Grid
        {

            HorizontalOptions = LayoutOptions.Fill,
            VerticalOptions = LayoutOptions.Fill,
            RowSpacing = 15,
            ColumnSpacing = 15,
            Padding = new Thickness(15),
            ColumnDefinitions = 
            {
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
            }
        };

        for (int i = 0; i < 12; i++)
        {
            Image img = new Image()
            {
                Source = "ButtonBlue.png"
            };
            //if(DependencyService.Get<IDeviceInfo>().IsPortraitOriented())
            //{ 
                grid.Children.Add(img, i % 3, i / 3);
            //}
            //else
            //{
            //    grid.Children.Add(button, i % 5, i / 5);
            //}
        }

        this.Content = new ScrollView
        {
            Orientation = ScrollOrientation.Vertical,
            Content = grid
        };

所以我在这里添加了12张图片来测试我的代码。该页面在纵向方面看起来很好,如果设备处于横向,则在列之间有很大的空间。

我也在尝试使用依赖注入来检索信息。 DependencyService正在完成他的工作,但我没有成功检索设备的方向......

2 个答案:

答案 0 :(得分:2)

在xamarin.forms中,您可以使用MessageCenter从android部分获取通知。 1.在共享项目中

public partial class MyPage : ContentPage
{   
    public MyPage ()
    {
        InitializeComponent ();

        Stack = new StackLayout
        {
            Orientation = StackOrientation.Vertical,
        };
        Stack.Children.Add (new Button { Text = "one" });
        Stack.Children.Add (new Button { Text = "two" });
        Stack.Children.Add (new Button { Text = "three" });

        Content = Stack;

        MessagingCenter.Subscribe<MyPage> (this, "Vertical", (sender) =>
        {
            this.Stack.Orientation = StackOrientation.Vertical;
            this.ForceLayout();
        });
        MessagingCenter.Subscribe<MyPage> (this, "Horizontal", (sender) =>
        {
            this.Stack.Orientation = StackOrientation.Horizontal;
            this.ForceLayout();
        });

    }
    public StackLayout Stack;
}

2.在Android项目中

[Activity (Label = "XamFormOrientation.Android.Android", MainLauncher = true, ConfigurationChanges = global::Android.Content.PM.ConfigChanges.Orientation | global::Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : AndroidActivity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        Xamarin.Forms.Forms.Init (this, bundle);

        SetPage (App.GetMainPage ());
    }
    public override void OnConfigurationChanged (global::Android.Content.Res.Configuration newConfig)
    {
        base.OnConfigurationChanged (newConfig);

        if (newConfig.Orientation == global::Android.Content.Res.Orientation.Portrait) {
            MessagingCenter.Send<MyPage> (null, "Vertical");
        } else if (newConfig.Orientation == global::Android.Content.Res.Orientation.Landscape) {
            MessagingCenter.Send<MyPage> (null, "Horizontal");
        }
    }
}

答案 1 :(得分:2)

我解决了一个类似的问题并在很棒的帖子上找到它可能对你有用(见下面的超链接)。

在快捷方式中:按

查找方向
Page.Width < Page.Height 

并在创建页面

时在ContentPage(或其他)的构造函数中使用此信息

http://www.sellsbrothers.com/Posts/Details/13740