Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride a UseControl's library
    primarykey
    data
    text
    <p>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.</p> <p>My test class library looks like:</p> <pre><code>using System.Diagnostics; namespace MyLibrary { public class Foo { virtual public void Bar() { Debug.WriteLine(" Hi from MyLibrary, class Foo, method Bar."); } } } </code></pre> <p>My WPF User Control looks like:</p> <pre><code>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(); } } } </code></pre> <p>I have built a WPF Application called ProgramA, and it looks like:</p> <pre><code>using System; using System.Diagnostics; using System.Windows; namespace ProgramA { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } } </code></pre> <p>When debugging ProgramA, you will see:</p> <pre><code>MyUserControl: Hi from MyLibrary, class Foo, method Bar. </code></pre> <p>in the debug Output window. So far, so good.</p> <p>I have also built ProgramB to try to override the Bar method of MyLibrary. ProgramB looks like:</p> <pre><code>using System.Windows; namespace ProgramB { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } } </code></pre> <p>The XML for both ProgramA and ProgramB contains a reference to MyUserControl:</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;my:UserControl1 Name="userControl11" HorizontalAlignment="Left" VerticalAlignment="Top" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>I have added a Class to ProgramB's project called NewFoo.cs, and it looks like:</p> <pre><code>using System.Diagnostics; using MyLibrary; namespace ProgramB { class NewFoo : MyLibrary.Foo{ override public void Bar() { Debug.WriteLine(" Hi from ProgramB Foo, method Bar."); } } } </code></pre> <p>ProgramB compiles and runs, but the output is:</p> <pre><code>MyUserControl: Hi from library Foo, method Bar. </code></pre> <p>The override did not work.</p> <p>Here is the problem. The Namespace for ProgramB’s Foo method is ProgramB, and hence it does not override the Bar method of MyLibrary.</p> <p>Is there a way ProgramB can override the Bar method used by MyUserControl?</p> <p>Any help or suggestions will be greatly appreciated. Charles</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload