I am trying to load an image dynamically into a user control in an MVVM, Prism, Unity type environment. I have not found any examples specific to what I am attempting, and I would be appreciative if someone could look at my code below and set me on the right track:
My xaml, incomplete as it is, looks like this:
<UserControl x:Class="lp.ImageLab.Views.ImageLabDisplayImageView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:my="clr-namespace:lp.ImageLab.Views" mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200"><UserControl.Resources><Image </UserControl.Resources><Grid><Image Width="200"><Image.Source><BitmapImage DecodePixelWidth="200" UriSource="{DynamicResource ResourceKey=ImageUri}" /></Image.Source></Image></Grid></UserControl>
The codebehind for my view looks like this:
namespace lp.ImageLab.Views { /// <summary> /// Interaction logic for ImageLabDisplayImage.xaml /// </summary> public partial class ImageLabDisplayImageView : UserControl, IRegionMemberLifetime { #region Constructor public ImageLabDisplayImageView() { InitializeComponent(); DataContext = new ImageLabDisplayImageViewModel(this); } #endregion #region IRegionMemberLifetime Members public bool KeepAlive { get { return false; } } #endregion } }
The command from my menu control where the user picks the image file to load:
public void OpenImageFile() { try { // Create OpenFileDialog OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = ".jpg"; dlg.Filter = "bitmat images (.jpg)|*.jpg"; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> result = dlg.ShowDialog(); // Get the selected file name and display in a TextBox if (result == true) { string filename = dlg.FileName; MessageBox.Show(filename); // Initialize m_Publish = "ImageLab"; Uri uri = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"); lp.ImageLab.ViewModels.ImageLabDisplayImageViewModel.ImageUri = uri; var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>(); var imageLabDisplayImageView = new Uri("ImageLabDisplayImageView", UriKind.Relative); regionManager.RequestNavigate("WorkspaceRegion", imageLabDisplayImageView, NavigationCompleted); } } catch (Exception) { throw new ApplicationException("Failed loading image"); } }
... And my incomplete view model:
namespace lp.ImageLab.ViewModels { public class ImageLabDisplayImageViewModel : ViewModelBase { #region fields ImageLabDisplayImageView m_view; private static ImageSource _imageLocation; private static Uri _imageUri; #endregion #region Constructor public ImageLabDisplayImageViewModel(ImageLabDisplayImageView view) { m_view = view; } #endregion public static ImageSource ImageLocation { get { return _imageLocation; } set { _imageLocation = value; } } public static Uri ImageUri { get { return _imageUri; } set { _imageUri = value; } } } }
I apologize for uploading so much code, but I think it is necessary to illustrate where I am in this project; which is pretty much L-O-S-T. lol
Experience trumps brilliance.