覆盖 UseControl 的库
我想开发一个 WPF 用户控件库,它使用带有可以重写的成员函数的类库。我正在使用 C# 4.0 和 VS 2010。
我的测试类库如下所示:
using System.Diagnostics;
namespace MyLibrary {
public class Foo {
virtual public void Bar() {
Debug.WriteLine(" Hi from MyLibrary, class Foo, method Bar.");
}
}
}
我的 WPF 用户控件如下所示:
using System.Windows.Controls;
using System.Diagnostics;
using MyLibrary;
namespace MyUserControl {
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
Debug.WriteLine("MyUserControl: ");
var foo = new Foo();
foo.Bar();
}
}
}
我构建了一个名为 ProgramA 的 WPF 应用程序,它看起来像:
using System;
using System.Diagnostics;
using System.Windows;
namespace ProgramA {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
}
看到:
MyUserControl:
Hi from MyLibrary, class Foo, method Bar.
调试 ProgramA 时,您将在调试输出中 窗户。到目前为止,一切都很好。
我还构建了 ProgramB 来尝试重写 MyLibrary 的 Bar 方法。 ProgramB 看起来像:
using System.Windows;
namespace ProgramB {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
}
ProgramA 和 ProgramB 的 XML 都包含对 MyUserControl 的引用:
<Window x:Class="ProgramB.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:my="clr-namespace:MyUserControl;assembly=MyUserControl">
<Grid>
<my:UserControl1 Name="userControl11"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
</Window>
我已向 ProgramB 的项目添加了一个名为 NewFoo.cs 的类,它看起来像:
using System.Diagnostics;
using MyLibrary;
namespace ProgramB {
class NewFoo : MyLibrary.Foo{
override public void Bar() {
Debug.WriteLine(" Hi from ProgramB Foo, method Bar.");
}
}
}
ProgramB 编译并运行,但输出是:
MyUserControl:
Hi from library Foo, method Bar.
覆盖不起作用。
问题就在这里。 ProgramB 的 Foo 方法的命名空间是 ProgramB,因此它不会覆盖 MyLibrary 的 Bar 方法。
有没有办法 ProgramB 可以覆盖 MyUserControl 使用的 Bar 方法?
任何帮助或建议将不胜感激。 查尔斯
I would like to develope a WPF User Control Library that uses a Class Library with a member function that can be over-ridden . I’m using C# 4.0 and VS 2010.
My test class library looks like:
using System.Diagnostics;
namespace MyLibrary {
public class Foo {
virtual public void Bar() {
Debug.WriteLine(" Hi from MyLibrary, class Foo, method Bar.");
}
}
}
My WPF User Control looks like:
using System.Windows.Controls;
using System.Diagnostics;
using MyLibrary;
namespace MyUserControl {
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
Debug.WriteLine("MyUserControl: ");
var foo = new Foo();
foo.Bar();
}
}
}
I have built a WPF Application called ProgramA, and it looks like:
using System;
using System.Diagnostics;
using System.Windows;
namespace ProgramA {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
}
When debugging ProgramA, you will see:
MyUserControl:
Hi from MyLibrary, class Foo, method Bar.
in the debug Output window. So far, so good.
I have also built ProgramB to try to override the Bar method of MyLibrary.
ProgramB looks like:
using System.Windows;
namespace ProgramB {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
}
The XML for both ProgramA and ProgramB contains a reference to MyUserControl:
<Window x:Class="ProgramB.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:my="clr-namespace:MyUserControl;assembly=MyUserControl">
<Grid>
<my:UserControl1 Name="userControl11"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
</Window>
I have added a Class to ProgramB's project called NewFoo.cs, and it looks like:
using System.Diagnostics;
using MyLibrary;
namespace ProgramB {
class NewFoo : MyLibrary.Foo{
override public void Bar() {
Debug.WriteLine(" Hi from ProgramB Foo, method Bar.");
}
}
}
ProgramB compiles and runs, but the output is:
MyUserControl:
Hi from library Foo, method Bar.
The override did not work.
Here is the problem. The Namespace for ProgramB’s Foo method is ProgramB, and hence it does not override the Bar method of MyLibrary.
Is there a way ProgramB can override the Bar method used by MyUserControl?
Any help or suggestions will be greatly appreciated.
Charles
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
UserControl1
不知道NewFoo
并且只实例化Foo
。它与名称空间无关。只要您提供正确的实例,覆盖就会起作用。
您应该在
UserControl1
中公开一个属性,您可以在其中设置要使用的foo
。Its because
UserControl1
doesnt know aboutNewFoo
and only instantiatesFoo
. It has nothing to do with namespaces.The override will work, as long as you provide the correct instance.
You should expose a property in
UserControl1
where you can set whatfoo
to use.