簡體   English   中英

如何在WPF中保存圖像源?

[英]How to save image source in 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();

            }

        }

您必須在編寫Settings.settings文件的圖像路徑的任何代碼中調用您的Refreshicon()方法。

編輯:

如果Button_Click_SaveImage位於MainWindow以外的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 ,當您實例MainWindow窗口以進行顯示時:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM