简体   繁体   English

如何使用代码UWP弹出图像

[英]How to popup Image using codes UWP

I have an app load pictures from the internet I want to add a popup event to the image so the user can view the image by click on it ! 我有一个应用程序可以从Internet加载图片,我想向图片添加弹出事件,以便用户可以通过单击图片来查看图片! I've try a lot of things to do that all that I can found is to resize the image or just view a content dialog with a text on it ! 我已经尝试了很多事情来做,所有我发现的事情就是调整图像的大小,或者只是查看带有文本的内容对话框! Here's my code : 这是我的代码:

    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);
        }
    }

I want it to look like that I want to keep my old image in the same place and create a new grid that have the same image like this image you can view it here 我希望它看起来像我想要将旧图像保留在同一位置,并创建一个具有与该图像相同图像的新网格,您可以在此处查看

I can't use the xaml file because my image load from the web by code using a dll file ! 我无法使用xaml文件,因为我的图像是通过dll文件通过代码从网上加载的!

The reason why the image doesn't display is because you don't add an Image control into the ViewBox to display the image content, you can try the following code to display an image in the popup. 无法显示图像的原因是,您没有将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);
    }
}

By the way, please notice that the Popup is a control that displays content on top of existing content, within the bounds of the application window, and pay attention to the Remarks part, 顺便说一句,请注意Popup是一个控件,它在应用程序窗口的边界内在现有内容之上显示内容,并注意“ 备注”部分,

Do not use a Popup if a Flyout, MenuFlyout, ToolTip or ContentDialog (MessageDialog for a Windows 8 app) is more appropriate. 如果Flyout,MenuFlyout,ToolTip或ContentDialog(对于Windows 8应用程序为MessageDialog)更合适,请不要使用弹出窗口。

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

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