如何在WPF中保存图像源?

时间:2013-09-06 17:44:20

标签: c# .net wpf

我创建了WPF应用程序,当用户选择图像并保存时,它将应用于所有按钮,导航栏项等。我编写了代码以保存Settings.settings文件中的图像路径。当我选择图像时,它将其保存到数据库但不适用于navigation bar items or buttons source of image,直到我重新启动我的应用程序。 我的代码是:

    public partial class MainWindow : DXWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Refreshicon();
        }

        public void Refreshicon()
        {
            BitmapImage bi = new BitmapImage(new Uri(ApplicationSettings.Default.ImageName));  //Image From Settings File!
            MessageBox.Show("Image Path" + bi.ToString());
            navBarGroup1.ImageSource = bi;
            navBarGroup2.ImageSource = bi;
            navBarItem1.ImageSource = bi;
            navBarItem2.ImageSource = bi;
        }

如何在不重新启动应用程序的情况下将用户定义的图像路径应用于navigation bar items or buttons

修改 //以下代码用于保存图像并调用Refreshicon()函数

 private void Button_Click_SaveImage(object sender, RoutedEventArgs e)
        {
        string imagepath = ApplicationSettings.Default.ImageName;
        ApplicationSettings.Default.SetImage(imageEdit1.ImagePath);
        MainWindow a = null;
        if (a == null)
            {
            a=new MainWindow();
            a.Refreshicon();

            }

        }

1 个答案:

答案 0 :(得分:2)

您必须在编写Refreshicon()文件的图像路径的任何代码中调用Settings.settings方法。

编辑:

如果Button_Click_SaveImage位于MainWindow以外的窗口中,则需要将原始MainWindow实例的引用添加到子窗口类并调用其Refreshicon() } 方法。像这样:

在子窗口类中,我们称之为DialogWindow:

public class DialogWindow : Window {
    // All your code here . . .

    public MainWindow Parent { get; set; }

    private void Button_Click_SaveImage( object sender, RoutedEventArgs e ) {
        // Your code up to the MainWindow a line goes here
        Parent.Refreshicon();
    }
}

然后,在MainWindow中,当您实例化子窗口以进行显示时:

DialogWindow childWindow = new DialogWindow();
childWindow.Parent = this;