简体   繁体   English

窗口切换之间刷新图片

[英]c# - wpf - refresh picture between window switching

I have simple wpf app which consist of 2 windows: MainMenu and PictureWindow. 我有一个简单的wpf应用程序,其中包含2个窗口:MainMenu和PictureWindow。 In MainMenu I have one button with click event which opens Picture Window: 在MainMenu中,我有一个带有单击事件的按钮,可打开图片窗口:

private void btnOpenPicWindow_Click(object sender, RoutedEventArgs e)
{
    var picWindow = new PictureWindow();
    Application.Current.MainWindow = picWindow;
    Close();
    picWindow.Show();
}

In PictureWindow I have WindowsFormsHost with PictureBox . 在PictureWindow中,我具有带有PictureBox WindowsFormsHost In PictureWindow I receive image which I send from another app and I show it on PictureBox . 在PictureWindow中,我收到从另一个应用程序发送的图像,并将其显示在PictureBox PictureWindow also has a button with click event which goes back to MainMenu like this: PictureWindow还有一个带有单击事件的按钮,它可以像下面这样返回到MainMenu:

private void btnBack_Click(object sender, RoutedEventArgs e)
{
    var mMenu = new MainWindow();
    System.Windows.Application.Current.MainWindow = mMenu;
    Close();
    mMenu.Show();
}

Everything is ok when I open MainWindow and then PictureWindow. 当我打开MainWindow然后是PictureWindow时,一切正常。 The problem is when I go back from PictureWindow to MainMenu and then once again to PictureWindow and if I send picture to my PictureBox it does not refresh. 问题是当我从PictureWindow返回MainMenu,然后再次回到PictureWindow,并且如果我将图片发送到PictureBox它不会刷新。 I receive the image cause I see it during debbuging but my PictureBox is blank. 我收到图像是因为在调试过程中看到了它,但是我的PictureBox是空白的。

You should try to re-initialize your window with your picture every time you click on the button on your MainWindow. 每次单击MainWindow上的按钮时,都应尝试用图片重新初始化窗口。

You can change LoadAsync for Load if you'd like to load your image with an Asynchronous or Synchronous method. 如果要使用异步或同步方法加载图像,可以更改LoadAsync for Load

WPF PictureWindow: WPF PictureWindow:

<Window x:Class="WpfApplication2.PictureWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PictureWindow" Height="300" Width="300"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Loaded="Window_Loaded">
    <Grid>
        <WindowsFormsHost Height="100" HorizontalAlignment="Left" Margin="12,12,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200">
            <wf:PictureBox x:Name="pbImage" SizeMode="AutoSize"></wf:PictureBox>
        </WindowsFormsHost>
    </Grid>
</Window>

C# PictureWindow: C#PictureWindow:

public partial class PictureWindow : Window
{
    public string imgsrc = string.Empty;
    public PictureWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        pbImage.LoadAsync(imgsrc);
    }
}

C# MainWindow: C#MainWindow:

public partial class MainWindow : Window
{
    PictureWindow window;

    public MainWindow()
    {
        InitializeComponent();
    }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    window = new PictureWindow();
    window.imgsrc = textBox1.Text.Trim(); //Here you update your "Source" for your image.
    window.Show();
}

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

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