如何在网格中更改(交换)帧内容?

时间:2018-01-18 09:47:01

标签: c# uwp

我正在通过拖放来制作帧更改例程。

通过拖放,识别两个帧都没问题。

然而,我无法交换这两个帧。

并且,我必须按原样交换两个帧。 (没有重新初始化或重新分配等)

我的英语很差,但是,看到源代码,你们可以理解我的问题:-)(这段代码会抛出错误!)

namespace FrameDragTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.ElementFrame0.Tag = "0";
            this.ElementFrame0.Background = new SolidColorBrush(Colors.Pink);
            this.ElementFrame1.Tag = "1";
            this.ElementFrame2.Tag = "2";
            this.ElementFrame2.Background = new SolidColorBrush(Colors.Pink);
            this.ElementFrame3.Tag = "3";
            this.ElementFrame4.Tag = "4";
            this.ElementFrame4.Background = new SolidColorBrush(Colors.Pink);
            this.ElementFrame5.Tag = "5";
            this.ElementFrame6.Tag = "6";
            this.ElementFrame6.Background = new SolidColorBrush(Colors.Pink);
            this.ElementFrame7.Tag = "7";

            this.ElementFrame0.Navigate(typeof(ElementPage));
            this.ElementFrame1.Navigate(typeof(ElementPage));
            this.ElementFrame2.Navigate(typeof(ElementPage));
            this.ElementFrame3.Navigate(typeof(ElementPage));
            this.ElementFrame4.Navigate(typeof(ElementPage));
            this.ElementFrame5.Navigate(typeof(ElementPage));
            this.ElementFrame6.Navigate(typeof(ElementPage));
            this.ElementFrame7.Navigate(typeof(ElementPage));

            this.ElementFrame0.DragEnter += OnDragEnter;
            this.ElementFrame1.DragEnter += OnDragEnter;
            this.ElementFrame2.DragEnter += OnDragEnter;
            this.ElementFrame3.DragEnter += OnDragEnter;
            this.ElementFrame4.DragEnter += OnDragEnter;
            this.ElementFrame5.DragEnter += OnDragEnter;
            this.ElementFrame6.DragEnter += OnDragEnter;
            this.ElementFrame7.DragEnter += OnDragEnter;

            this.ElementFrame0.Drop += OnDragDrop;
            this.ElementFrame1.Drop += OnDragDrop;
            this.ElementFrame2.Drop += OnDragDrop;
            this.ElementFrame3.Drop += OnDragDrop;
            this.ElementFrame4.Drop += OnDragDrop;
            this.ElementFrame5.Drop += OnDragDrop;
            this.ElementFrame6.Drop += OnDragDrop;
            this.ElementFrame7.Drop += OnDragDrop;

            this.ElementFrame0.DropCompleted += OnDropCompleted;
            this.ElementFrame1.DropCompleted += OnDropCompleted;
            this.ElementFrame2.DropCompleted += OnDropCompleted;
            this.ElementFrame3.DropCompleted += OnDropCompleted;
            this.ElementFrame4.DropCompleted += OnDropCompleted;
            this.ElementFrame5.DropCompleted += OnDropCompleted;
            this.ElementFrame6.DropCompleted += OnDropCompleted;
            this.ElementFrame7.DropCompleted += OnDropCompleted;
        }

        private void OnDragEnter(object sender, DragEventArgs e)
        {

            int x;
            x = 1;

            e.AcceptedOperation = DataPackageOperation.Move;
        }

        Frame m_pSrcFrame = null;
        Frame m_pDestFrame = null;

        async void OnDragDrop(object Sender, DragEventArgs e)
        {
            m_pDestFrame = (Frame)Sender;

        }

        async void OnDropCompleted(object Sender, DropCompletedEventArgs e)
        {
            m_pSrcFrame = (Frame)Sender;

            SwapFrame(m_pSrcFrame, m_pDestFrame);
        }

        void SwapFrame(Frame IN_pSrcFrame, Frame IN_pDestFrame)
        {    
            ElementFrame1.Content = IN_pDestFrame;
            ElementFrame2.Content = IN_pSrcFrame;
        }
    }
}
  

<GridView Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame0" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame1" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame2" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame3" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame4" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame5" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Frame x:Name="ElementFrame6" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible" CanDrag="True">
            <Frame x:Name="ElementFrame7" Height="313" Width="325" CanDrag="True" AllowDrop="True" />
        </ScrollViewer>
    </Grid>

</GridView> </Page>

1 个答案:

答案 0 :(得分:1)

似乎SwapFrame方法没有按照你真正想要的方式做,因为我知道你只想交换它们的内容:

void SwapFrame(Frame IN_pSrcFrame, Frame IN_pDestFrame)
{    
    var firstFrameContent = IN_pSrcFrame.Content;
    var secondFrameContent = IN_pDestFrame.Content;

    //detach contents
    IN_pSrcFrame.Content = null; 
    IN_pDestFrame.Content = null;

    IN_pSrcFrame.Content = secondFrameContent;
    IN_pDestFrame.Content = firstFrameContent;
}

您可以看到我们必须首先分离内容,因为当您尝试分配新内容时将内容分配给另一个框架时,会导致问题。

对于好奇的 - 我们实际上只能将IN_pDestFrame的内容与null分开,因为替换了第一个Frame&#39 ; s Content,也会&#34;分离&#34;它在过程中的原始内容,但这个&#34;对称&#34;解决方案看起来更好。