1

我正在开发一个通用应用程序,在该应用程序中我试图从选择中生成位图,以便我可以显示所选对象从 SettingsFlyout 内部移动到屏幕上的新位置的精美动画,同时 SettingsFlyout 与列表视图被转换为“详细信息”。

我在其他地方看到过这段代码,但它似乎不起作用。我得到一个 System.ArgumentException,后跟“值不在预期范围内”。在 RenderAsync 调用中。知道为什么会这样吗?单步执行代码显示 ListViewItem 已正确找到,但渲染它不起作用。我看到另一个提到 ItemContainerGenerator 但它产生了 NullReferenceException。

private async void AnimateSelection(object sender, SelectionChangedEventArgs e)
{   
    var container =
        (sender as ListView).ContainerFromItem((sender as ListView).SelectedItem);

    var bitmap = new RenderTargetBitmap();
    await bitmap.RenderAsync(container as FrameworkElement);

    // go on to animate this by copying it to another grid
}

以前有人试过这个吗?我想要做的是简单地复制列表视图的选定项并在另一个网格上使用它。让我知道是否需要使用更多信息编辑问题。

编辑:根据另一个开发人员的建议,我尝试从同一页面渲染 pageRoot 堆栈面板和另一个 TextBox,但也失败了。我认为问题在于渲染本身 - 我可以用其他代码替换此代码吗?

var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(container as FrameworkElement);

编辑:开发人员建议调度程序可能是一个问题,结果调度程序在通用应用程序中的处理方式不同,所以我尝试通过它来使用它 - 没有改变

var container =
            (sender as ListView).ContainerFromItem((sender as ListView).SelectedItem);

        await this.dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            var bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(container as FrameworkElement);

            var vm = this.DataContext as PersonViewModel;
            vm.TransitionImageSource = bitmap;
        });
4

2 回答 2

0

我通过将 SettingsFlyout 更改为 UserControl 并伪造 SettingsFlyout 的效果来解决此问题,这会破坏过程中的标准行为。看起来这不是真正的答案,但 SettingsFlyout 肯定存在问题,需要彻底调查

谢谢马特

于 2014-12-17T13:49:30.013 回答
0

如果没有对您的代码进行复制,我无法说出问题所在,但您的方法根本没有任何问题。

以下作品:

  • 创建一个新的空白通用应用程序。
  • 在两个目标之间共享 MainPage
  • 添加此 XAML:

    <StackPanel>
        <TextBlock>Some text</TextBlock>
        <Button Click="ChangeColor_OnClick">change color</Button>
        <ListBox x:Name="ListBox">
            <ListBoxItem>item 1</ListBoxItem>
            <ListBoxItem>item 2</ListBoxItem>
            <ListBoxItem>item 3</ListBoxItem>
        </ListBox>
    
        <Button Click="TakeScreenshot_OnClick">take screenshot</Button>
        <Button Click="TakeScreenshotOfSelected_OnClick">take screenshot of just selected item</Button>
        <Image x:Name="DisplayedScreenshot" Height="200" />
    </StackPanel>
    
  • 在后面添加这段代码:

    private void ChangeColor_OnClick(object sender, RoutedEventArgs e)
    {
        var rand = new Random();
    
        this.Background = new SolidColorBrush(new Color { A = 255, R = (byte)rand.Next(1, 255), G = (byte)rand.Next(1, 255), B = (byte)rand.Next(1, 255) });
    }
    
    private async void TakeScreenshot_OnClick(object sender, RoutedEventArgs e)
    {
        RenderTargetBitmap rtb = new RenderTargetBitmap();
    
        await rtb.RenderAsync(this as FrameworkElement);
    
        this.DisplayedScreenshot.Source = rtb;
    }
    
    private async void TakeScreenshotOfSelected_OnClick(object sender, RoutedEventArgs e)
    {
        RenderTargetBitmap rtb = new RenderTargetBitmap();
    
        await rtb.RenderAsync(this.ListBox.SelectedItem as UIElement);
    
        this.DisplayedScreenshot.Source = rtb;
    }
    
于 2014-12-17T12:34:14.813 回答