I am really struggling with this concept of setting an image source for a model property in the ViewModel and then binding to it. I have tried every way been on every forum but i cant get it to work correctly.
The odd thing is if i pause the code at line .65 and expand the Bitmap object and then allow the code to carry on running the bitmap shows in the UI. If i don't pause the code the image never shows this has to be a bug especially when my other bound variables show correctly.
Can somebody use my code to replicate the problem? And then show me what I'm doing wrong? or is it truly a bug with .net Core?
Thanks in advance please put me out of my missery....... I'm a self taught coder with only a little experience in WPF MVVM and binding.
Madaxe
<Window x:Class="UI.Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UI.Project"
xmlns:convertors="clr-namespace:Infrastructure.Project.Convertors;assembly=Infrastructure.Project"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<convertors:StringtoBitmapImageConvertor x:Key="StringtoBitmapImageConvertor" />
</Window.Resources>
<Grid>
<Button
x:Name="Btn_ShowSimpleDialogue"
Content="Button"
Command="{Binding Btn_ShowSimpleDialogue_Click}"
CommandParameter="{Binding ElementName=Btn_ShowSimpleDialogue}"
HorizontalAlignment="Left"
Margin="23,52,0,0"
VerticalAlignment="Top"/>
<Image Source="{Binding BitmapImage}" Width="200" VerticalAlignment="Bottom"/>
</Grid>
</Window>
using System.Windows;
using ViewModels.Project;
namespace UI.Project
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
}
using Infrastructure.Project.WPF;
using Models.Project.Models;
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using UICatalog_Project.UI.Notification;
namespace ViewModels.Project
{
public class MainViewModel :MainModel
{
public RelayCommand Btn_ShowSimpleDialogue_Click { get; private set; }
public MainViewModel()
{
Btn_ShowSimpleDialogue_Click = new RelayCommand(ShowSimpleDialogue, CanShowSimpleDialogue);
BitmapImage = new BitmapImage(new Uri(@"./Assets/NikolaDownload.png", UriKind.Relative));
//If i review the var BitmapImage by pausing the code post its initilization the image shows in the main UI
ImageSource = new Uri(@"../Assets/NikolaDownload.png", UriKind.Relative);
}
void ShowSimpleDialogue(object message)
{
SimpleNotificationViewModel simpleNotificationViewModel = new SimpleNotificationViewModel();
simpleNotificationViewModel.Title = "Error Message";
simpleNotificationViewModel.Description = "Hello im An error";
simpleNotificationViewModel.ErrorImage = new Uri(@"..\Assets\NikolaDownload.png",UriKind.Relative);
Window SimpleDialogue = new SimpleNotification();
SimpleDialogue.DataContext = simpleNotificationViewModel;
SimpleDialogue.Show();
}
bool CanShowSimpleDialogue(object message)
{
return true;
}
}
}
using Models.Project.AbstractModels;
using System;
using System.Windows.Media.Imaging;
namespace Models.Project.Models
{
public class MainModel : BaseModel
{
Uri _ImageSource;
public Uri ImageSource
{
get => _ImageSource;
set { _ImageSource = value; NotifyPropertyChanged(); }
}
BitmapImage _BitmapImage;
public BitmapImage BitmapImage
{
get => _BitmapImage;
set { _BitmapImage = value; NotifyPropertyChanged(); }
}
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Models.Project.AbstractModels
{
public abstract class BaseModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
using System;
using System.Windows.Input;
namespace Infrastructure.Project.WPF
{
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new NullReferenceException("execute");
}
else
{
this._execute = execute;
this._canExecute = canExecute;
}
}
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return this._canExecute == null ? true : this._canExecute(parameter);
}
public void Execute(object parameter)
{
this._execute.Invoke(parameter);
}
}
}