繁体   English   中英

如何使用代码UWP弹出图像

[英]How to popup Image using codes UWP

我有一个应用程序可以从Internet加载图片,我想向图片添加弹出事件,以便用户可以通过单击图片来查看图片! 我已经尝试了很多事情来做,所有我发现的事情就是调整图像的大小,或者只是查看带有文本的内容对话框! 这是我的代码:

    private static void view_tapped(object sender, TappedRoutedEventArgs e)
    {
        Viewbox image = sender as Viewbox;

        Panel parent = image.Parent as Panel;
        if (parent != null)
        {
            image.RenderTransform = new ScaleTransform() { ScaleX = 0.5, ScaleY = 0.5 };
            parent.Children.Remove(image);
            parent.HorizontalAlignment = HorizontalAlignment.Stretch;
            parent.VerticalAlignment = VerticalAlignment.Stretch;
            parent.Children.Add(new Popup() { Child = image, IsOpen = true, Tag = parent });
        }
        else
        {
            Popup popup = image.Parent as Popup;
            popup.Child = null;
            Panel panel = popup.Tag as Panel;
            image.RenderTransform = null;
            panel.Children.Add(image);
        }
    }

我希望它看起来像我想要将旧图像保留在同一位置,并创建一个具有与该图像相同图像的新网格,您可以在此处查看

我无法使用xaml文件,因为我的图像是通过dll文件通过代码从网上加载的!

无法显示图像的原因是,您没有将Image控件添加到ViewBox中以显示图像内容,因此可以尝试以下代码在弹出窗口中显示图像。

private void Viewbox_Tapped(object sender, TappedRoutedEventArgs e)
{
    Viewbox image = sender as Viewbox;
    Image imageControl = new Image() { Width = 500, Height = 500 };
    Uri uri = new Uri("Your image URL");
    BitmapImage source = new BitmapImage(uri);
    imageControl.Source = source;
    image.Child = imageControl;
    Panel parent = image.Parent as Panel;
    if (parent != null)
    {
        image.RenderTransform = new ScaleTransform() { ScaleX = 0.5, ScaleY = 0.5 };
        parent.Children.Remove(image);
        parent.HorizontalAlignment = HorizontalAlignment.Stretch;
        parent.VerticalAlignment = VerticalAlignment.Stretch;
        parent.Children.Add(new Popup() { Child = image, IsOpen = true, Tag=parent});
    }
    else
    {
        Popup popup = image.Parent as Popup;
        popup.Child = null;
        Panel panel = popup.Tag as Panel;
        image.RenderTransform = null;
        panel.Children.Add(image);
    }
}

顺便说一句,请注意Popup是一个控件,它在应用程序窗口的边界内在现有内容之上显示内容,并注意“ 备注”部分,

如果Flyout,MenuFlyout,ToolTip或ContentDialog(对于Windows 8应用程序为MessageDialog)更合适,请不要使用弹出窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM