Reputation: 2043
I have a peculiar problem where I have to override a particular method present in the Window class .
I am using MVVM and my ViewModel is inheriting to BaseVm which has INotifyPropertyChanged
What I need is this
public partial class MainWindow : Window
{
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Hook up handler for window messages
var windowSource = PresentationSource.FromVisual(this) as HwndSource;
if (windowSource == null)
return;
windowSource.AddHook(WiDiOnWindowMessage);
try
{
_wiDi = new WiDi();
_wiDi.ValidateWiDi();
//// This is async call so rest of logic handled in OnWiDiWindowMessage
//// Pass the window handle which WiDi extensions library uses to send messages back.
_wiDi.Initialize((uint)windowSource.Handle.ToInt64());
}
catch (WiDiNoWelException)
{
ShowMessageDialog("Warning: The Intel® WiDi auto-connect button is not available.",
"Please use the Intel® WiDi application to connect to your TV.\n" +
"Alternatively you can use an HDMI cable.\n" +
"Issue: The Intel® WiDi Extensions Library (WEL) cannot be found.");
}
catch (WiDiNoAppException)
{
ShowMessageDialog("Warning: Intel® WiDi is not available.",
"Please use an HDMI cable to connect to your TV.\n" +
"Issue: Intel® WiDi cannot be found or this PC may not be Intel® WiDi compatible.");
}
catch (WiDiException exception)
{
ShowMessageDialog("Warning: Intel® WiDi is not available.",
"Please use an HDMI cable to connect to your TV.\n" +
"Issue: " + exception.Message);
}
My Current code is
public class HomeWindowVm : BaseVm
{
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Hook up handler for window messages
var windowSource = PresentationSource.FromVisual(this) as HwndSource;
// var windowsource = PresentationSource.FromVisual(this) as HwndSource;
if (windowSource == null)
return;
windowSource.AddHook(WiDiOnWindowMessage);
try
{
_wiDi = new WiDi();
_wiDi.ValidateWiDi();
// This is async call so rest of logic handled in OnWiDiWindowMessage
// Pass the window handle which WiDi extensions library uses to send messages back.
_wiDi.Initialize((uint)windowSource.Handle.ToInt64());
}
catch (WiDiNoWelException)
{
ShowMessageDialog("Warning: The Intel® WiDi auto-connect button is not available.",
"Please use the Intel® WiDi application to connect to your TV.\n" +
"Alternatively you can use an HDMI cable.\n" +
"Issue: The Intel® WiDi Extensions Library (WEL) cannot be found.");
}
catch (WiDiNoAppException)
{
ShowMessageDialog("Warning: Intel® WiDi is not available.",
"Please use an HDMI cable to connect to your TV.\n" +
"Issue: Intel® WiDi cannot be found or this PC may not be Intel® WiDi compatible.");
}
catch (WiDiException exception)
{
ShowMessageDialog("Warning: Intel® WiDi is not available.",
"Please use an HDMI cable to connect to your TV.\n" +
"Issue: " + exception.Message);
}
}
My problem is that as my Class in Not inheriting fro Window .the OnSourceInitialized(EventArgs e) is giving errors > .
how can I inherit from Window Class in the same code.
Upvotes: 1
Views: 576
Reputation: 128097
Your view model could probably have a SourceInitialized
method that you attach to the Window's SourceInitialized
event (instead of overriding the OnSourceInitialized
method):
public class HomeWindowVm : BaseVm
{
public void SourceInitialized(object sender, EventArgs e)
{
var window = (Window)sender;
var windowSource = PresentationSource.FromVisual(window) as HwndSource;
...
}
}
When you create your view model instance, you would attach the handler like this:
public MainWindow()
{
InitializeComponent();
var vm = new HomeWindowVm();
SourceInitialized += vm.SourceInitialized;
DataContext = vm;
}
Upvotes: 2
Reputation: 18578
You cannot do this in your ViewModel as you can see that OnSourceInitialized
is a Window function and hence can only be overridden in the derived Window class. It does not make sense to derive VM from Window as VM is intended to provide DataContext to the View (window in your case). Even if you derive VM from Window it would not be the Window that you want to override this function.
So in this case you can either keep this code in your Windows code behind which I don't think is a voilation to MVVM here.
OR you can define a attached behavior and put it on your Window class and check everything in the behavior itself.
Upvotes: 0