我在`WPF \ Telerik项目中工作。我遇到了一个非常奇怪的问题,因为功能的相互依赖性,我无法使用解决方法。
我的项目有自动注销功能,为此,我必须使用这段代码,如下所示。
private void InitializeAutoLogoffFeature()
{
HwndSource windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
windowSpecificOSMessageListener.AddHook(new HwndSourceHook(CallBackMethod));
LogOffHelper.LogOffTime = logOffTime;
LogOffHelper.MakeAutoLogOffEvent += new MakeAutoLogOff(AutoLogOffHelper_MakeAutoLogOffEvent);
LogOffHelper.StartAutoLogoffOption();
}
在此HwndSource windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
代码行中,我必须通过窗口(this
)。
因此[Window_Name]
窗口必须使用Window
来实现,因为WindowInteropHelper
构造函数只接受Window
类型。
但是当我在下面恭维时
public partial class MainWindow : Window
{
我收到错误,
Partial declarations of '[WPFApplication].MainWindow' must not specify different base classes
此MainWindow
不是Window
,而是Telerik window
。
XML如下所示。
<telerik:RadWindow x:Class="[WPFApplication].MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Header="POS" Height="820" Width="1280"
WindowStartupLocation="CenterScreen">
<telerik:RadWindow.Resources>
..
这是我的App.xaml
<Application x:Class="[WPFApplication].App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow"
>
<Application.Resources>
</Application.Resources>
我也试过在App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
base.OnStartup(e);
}
}
我该如何克服这个错误?
答案 0 :(得分:2)
如果您的MainWindow
是RadWindow
,则需要
public partial class MainWindow : RadWindow
因为XAML
和.cs
中的窗口类必须相同:
<telerik:RadWindow x:Class="[WPFApplication].MainWindow" ...>
^^^^^^^^^
根据this thread中的帖子,RadWindow
使用Window
作为容器,可以像这样访问
var window = this.ParentOfType<Window>();
因此您可以将RadWindow
用作MainWindow
(KB article)并将标准WPF窗口传递给InteropHelper