5

我想在构建页面时知道设备(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 正在做他的工作,但我没有成功检索设备的方向......

4

2 回答 2

2

我解决了一个类似的问题,并在可能对您有帮助的好帖子上找到它(请参阅下面的超链接)。

在快捷方式中:找出方向

Page.Width < Page.Height 

并在创建页面时在 ContentPage (或其他)的构造函数中使用此信息

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

于 2015-02-12T07:15:10.540 回答
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");
        }
    }
}
于 2014-11-01T09:45:27.117 回答