我有一个特殊的问题,我必须覆盖 Window 类中的特定方法。
我正在使用 MVVM ,我的ViewModel继承到具有 INotifyPropertyChanged
的BaseVm我需要的是这个
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);
}
我的当前代码是
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);
}
}
我的问题是,我的课程未继承 Window 。 OnSourceInitialized(EventArgs e)正在提供错误>
如何在同一代码中继承 Window 类。
答案 0 :(得分:2)
您的视图模型可能有一个SourceInitialized
方法,您附加到Window的SourceInitialized
事件(而不是覆盖OnSourceInitialized
方法):
public class HomeWindowVm : BaseVm
{
public void SourceInitialized(object sender, EventArgs e)
{
var window = (Window)sender;
var windowSource = PresentationSource.FromVisual(window) as HwndSource;
...
}
}
创建视图模型实例时,您可以像这样附加处理程序:
public MainWindow()
{
InitializeComponent();
var vm = new HomeWindowVm();
SourceInitialized += vm.SourceInitialized;
DataContext = vm;
}
答案 1 :(得分:0)
您无法在ViewModel中执行此操作,因为您可以看到OnSourceInitialized
是一个Window函数,因此只能在派生的Window类中重写。从Window派生VM没有意义,因为VM旨在向View提供DataContext(在您的情况下为窗口)。即使您从Window派生VM,也不是要覆盖此功能的Window。
所以在这种情况下,您可以将此代码保存在您的Windows代码中,我不认为这是MVVM的声音。
或者您可以定义附加行为并将其放在Window类上并检查行为本身的所有内容。