【问题标题】:How to rotate Grid depending on a property easiest way如何根据属性旋转网格最简单的方法
【发布时间】:2021-12-12 23:01:01
【问题描述】:

您好,我想知道旋转网格的简单方法是什么。 我有 4 页:

 private static Figure[] array;
 public App ()
    {
        Initialize(array); // Fills array with figures with ImageSources
        InitializeComponent ();
        MainPage = new Page(array,Color.Red);
    }


class Figure
{
private ImageSource Source {get; set;}
public Figure(ImageSource source)
   {
       Source = source;
   }
}

class Page
{
private Color Color;
private Grid Grid;
public Page (Figure[] Figures, Color color)
   {
       Color = color;
       // Now this is where I need help...
   }
}

我希望 Grid 始终具有相同的大小并始终填充相同的数组,但取决于颜色,方向应该改变。事实上,整个网格应该根据颜色旋转 90 度。这些网格应该具有绑定到图形的 Imagesource 的 ImageButtons(带有转换器)。我考虑过在 Xaml 中创建 4Grids 并手动实现所有内容,并为每个页面提供自定义网格。我想出的另一个选择是仅创建一个网格并使用网格的旋转方法(但使用此选项我必须旋转网格的每个子项,否则图片将随网格旋转......我认为这两种解决方案都很不方便我想知道我还有什么其他选择。也许有人可以帮助我吗?非常感谢...

【问题讨论】:

  • "但是使用此选项我必须将网格的每个子元素都向后旋转,否则图片会随着网格旋转。" 有多少行和列?如果“行数 == 列数”,那么我会以不同的方式处理它。假设网格是 2x2。不要旋转网格,而是编写绑定表达式,根据颜色将图像放置在正确的单元格中。我会为这 4 个案例使用枚举。例如。 enum Grid4 {One, Two, Three, Four}Grid4 which4 = Grid4.OneSource={Binding Image11}public Imagesource Image11 => which4 == Grid4.One ? Figures[0] : ...;
  • @ToolmakerSteve 嗨,不是图像必须切换方向。 ImageButton 必须改变方向(它包含图像)。您的解决方案在这种情况下也有效吗?如果是这样,您能否对实现更具体一些,因为我从未实现自己的绑定……也许是一个链接……
  • 首先,让我澄清一下我的建议:你不能完全避免切换方向吗?不要旋转网格。不要旋转任何东西。只需更改每个单元格中显示的内容,使其在用户看来就像旋转了一样。谷歌xamarin forms data binding - 第一个链接是Xamarin.Forms Data Binding。该 google 的其他链接也可能有所帮助。
  • 如果在 X 行 Y 列的网格绑定应该是 ImageButton buttonOne 但 buttonOne 源应该绑定到图形 [theImagebuttonOneNeeds],我可以实现这一点。该页面仅获取数字。我必须自己写绑定吗? @ToolmakerSteve
  • 我添加了一个答案来展示如何按照我的建议进行操作。

标签: c# .net xamarin xamarin.forms grid


【解决方案1】:

根据设置更改的 ImageSource 示例。

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestBugs.MainPage">
    <StackLayout>
        <Label Text="Test"/>
        <Grid ColumnDefinitions="50,50" RowDefinitions="50,50">
            <Image Grid.Row="0" Grid.Column="0" Source="{Binding Source1A}" BackgroundColor="Red"/>
            <Image Grid.Row="0" Grid.Column="1" Source="{Binding Source1B}" BackgroundColor="Green"/>
            <Image Grid.Row="1" Grid.Column="0" Source="{Binding Source2A}" BackgroundColor="Blue"/>
            <Image Grid.Row="1" Grid.Column="1" Source="{Binding Source2B}" BackgroundColor="Yellow"/>
        </Grid>
    </StackLayout>
</ContentPage>

C#:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestBugs
{
    public partial class MainPage : ContentPage
    {
        // REPLACE "TestBugs" with your project's assembly name.
        public const string AssemblyName = "TestBugs";


        public enum Orientation
        {
            One, Two, Three, Four
        }

        const int NOrientations = 4;


        public MainPage()
        {
            // Assuming stored locally in files or resources.
            // If need server queries, recommend not doing this in constructor.
            LoadOurImages();

            InitializeComponent();
            // In this simple example, the binding sources are in the page itself.
            BindingContext = this;
        }


        protected override void OnAppearing()
        {
            base.OnAppearing();

            BackgroundTestLoop();
        }

        static Random Rand = new Random();

        private void BackgroundTestLoop()
        {
            Task.Run(async () =>
            {
                const int NTimes = 20;
                for (int i = 0; i < NTimes; i++)
                {
                    await Task.Delay(3000);

                    Orientation nextOrient = (Orientation)Rand.Next(NOrientations);
                    // Only affect UI from main thread.
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Orient = nextOrient;
                    });
                }
            });
        }

        public Orientation Orient {
            get => _orient;
            set
            {
                _orient = value;
                // When Orient changes, that affects the values of these properties.
                // OnPropertyChanged is from super-class BindableObject.
                OnPropertyChanged(nameof(Source1A));
                OnPropertyChanged(nameof(Source1B));
                OnPropertyChanged(nameof(Source2A));
                OnPropertyChanged(nameof(Source2B));
            }
        }
        private Orientation _orient = Orientation.One;

        // Public getters. These change when Orient changes.
        public ImageSource Source1A => Sources[Indexes1A[(int)Orient]];
        public ImageSource Source1B => Sources[Indexes1B[(int)Orient]];
        public ImageSource Source2A => Sources[Indexes2A[(int)Orient]];
        public ImageSource Source2B => Sources[Indexes2B[(int)Orient]];


        List<string> ResourcePaths = new List<string> {
            "apple.png", "banana.png", "car.png", "dog.png"};

        List<ImageSource> Sources = new List<ImageSource>();

        // Change these as needed.
        List<int> Indexes1A = new List<int> { 0, 1, 2, 3 };
        List<int> Indexes1B = new List<int> { 1, 2, 3, 0 };
        List<int> Indexes2A = new List<int> { 2, 3, 0, 1 };
        List<int> Indexes2B = new List<int> { 3, 0, 1, 2 };



        private void LoadOurImages()
        {
            foreach (var path in ResourcePaths)
                Sources.Add(CreateOurSource(path));
        }

        private ImageSource CreateOurSource(string resourcePath)
        {
            // For embedded resources stored in project folder "Media".
            var resourceID = $"{AssemblyName}.Media.{resourcePath}";
            // Our media is in the cross-platform assembly. Find that from this page.
            Assembly assembly = this.GetType().GetTypeInfo().Assembly;
            ImageSource source = ImageSource.FromResource(resourceID, assembly);
            return source;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2012-04-20
    • 2013-04-22
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多