tags:

views:

34

answers:

3

Hello,

i want to create a Window in an ControlLibrary called MyWindow so that i can define a Standard Look for all my new Applications (for example i want to put an Icon in the MyWindow) and some basic logic so i can inherit from that window in other Wpf-Applications.

But if i try to inherit MyWindow in another application i get an error ...

Can somebody give me a hint how i can solve this problem?

A: 

If you want to inherit from a Class(MyWindow in your case) you need to declare it in pure code, no xaml is allowed as xaml can not be inherited.

You can make the root tag of your xaml to a class defined by you also. Here you have to derive your base class from Window and then you can derive your windows classes from your base class. sorry I don't know vb.net, so this code is in C#

public class MyWindow : Window
{
    .....//your basic logic here
}

you can derive your page classes from a class like this:

public partial class MyDeriveClass : MyWindow
{
    .............
}

and In your xaml write

<y:MyWindow y="add your Namesapace here" and add other attribute of Page also like default namespace and xmlns:x>
    ...............
</y:MyWindow>

Hope this helps!!

viky
i have tried this but it doesn't work. below i post an example of what i have done ...
Nico
A: 

i have tow projects: 1. my ControlLib where i define a Base Window 2. A Project where i want to use my Base as the main Window

My Base Window looks like the following:

<Window x:Class="BaseWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="GeoAS.ico"
Title="BaseWindow" Height="300" Width="300"
Background="black">
  <Grid>

  </Grid>
</Window>

This in the Namespace MyControllLib. I've added this Lib as a reference to my Project where i want to use this Window as my Main Window.

I expected to put somthing like this is my Codebehind from the new Window:

Class Window1
Inherits MyControlLib.BaseWindow

End Class

But the compiler put out an error (i have to translate, because the error is in german): The Baseclass of the Window1-Class has not to be different from the myControlLib.BaseWindow-Baseclass.

I have no idea whats to do

Nico
A: 

Your BaseWindow class should be in pure code. Like this:

public class BaseWindow : Window
{
    public BaseWindow()
    {
        // Code modified here
        Uri iconUri = new Uri("pack://application:,,,/MyControlLib;component/my.ico", UriKind.RelativeOrAbsolute);
        Icon = BitmapFrame.Create(iconUri);
        Title = "BaseWindow";
        Height = 300;
        Width = 300;
        Background = Brushes.Black;
    }
}

and your derive class can be either in pure code or in xaml and code both. In first case you need to do just this:

public partial class Window1 : BaseWindow
{
    public Window1()
    {
        InitializeComponent();
    }
}

but in second case you need to drive your xaml also, like this:

<local:BaseWindow x:Class="ErrorTemplatePOC.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControlLib;assembly=MyControlLib">
</local:BaseWindow>
viky
cool, the inherity works now, but i get an error when i include the Part where i set my Icon ... he told me that i cant instancieate my BaseWindow .... I tried the way with "pack://...." and tried Icon = New BitmapImage(New Uri("my.ico", UriKind.RelativeOrAbsolute)). everytime a error. Where i have to put my .ico that the way you postet works?
Nico
I have updated my code in my BaseWindow class here for your requirement. you can add your image in to your MyControlLib project and use this pattern to set the Icon
viky
Great ... works! Many thanks!
Nico