Click here to Skip to main content
12,820,156 members (21,368 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
I have decided to start learning c# and wpf today and am sure there will be 100 more posts to follow :)
I plan to follow a few tutorials tonight on here but while I am at work I thought I would have a quick play around.
I have created a page with some buttons, each button is to spawn a new form.
The first button will always work but once i add several more I start getting errors.

Could I have an explanation as to why this is?

 public partial class MainWindow : Window
        public MainWindow()

        private void button1_Click(object sender, RoutedEventArgs e)
            Window win = new bapp();

        private void button2_Click(object sender, RoutedEventArgs e)
            Window win = new Ulinks();

        private void button3_Click(object sender, RoutedEventArgs e)
            Window win = new tasks();

        private void button4_Click(object sender, RoutedEventArgs e)
            Window win = new settings();


I get the following error for all other buttons

Cannot implicitly convert type 'Dehub.Ulinks' to 'System.Windows.Window' C:\Development\c#\Dehub\Dehub\MainWindow.xaml.cs 36 26 Dehub


Here is the XMAL of Ulinks

<Page x:Class="Dehub.Ulinks"
      d:DesignHeight="300" d:DesignWidth="300"

Posted 30-Jan-13 3:42am
Updated 30-Jan-13 4:18am
Shubh Agrahari 30-Jan-13 8:53am
what error r you getting...???
Tony Constantinides 30-Jan-13 8:57am
Silly me, added to the question.
Pete O'Hanlon 30-Jan-13 9:14am
Could you post the XAML of Ulinks please?
Richard MacCutchan 30-Jan-13 9:16am
I would suggest you forget WPF until you understand C# and .NET a little better.
Shubh Agrahari 30-Jan-13 9:16am
once try this on every button click event...
Window win = new Window();
MadMellon 30-Jan-13 9:19am
added as requested.
MadMellon 30-Jan-13 9:21am
I was actually starting to think that. Thought it may be good to learn both at same time...but not so sure now. I have been reading lots at work and its quite scary! lol
MadMellon 30-Jan-13 9:23am
This creates a new window on each button now. Obviously not the windows I have created (eg ulinks)
Richard MacCutchan 30-Jan-13 9:43am
C# and .NET are reasonably easy on their own, as there are not too many concepts to grasp. If you want a really simple starter document then try .NET Book Zero. Once you understand all that clearly you can move on to Winforms or WPF.
MadMellon 30-Jan-13 10:22am
Thanks, I can see I was using the wrong type now. Thanks for the various snippets of advise and the book recommendation.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Whatever your Ulinks is not of type Window therefore the cast Window win = new Ulinks(); is invalid.


Now that you have posted your XAML it confirms this. Page is not the same as Window. You need to use the correct type.

Seems you are meshing Silverlight/Windows store apps with WPF. WPF uses UserControl and Window. Not Page.. You can use page in WPF but that is not standard practice. As you said you are learning it, so stick with the basics.
Pete O'Hanlon 30-Jan-13 9:28am
You can use Page in WPF. It's not that common, but it is useful occasionally.
Collin Jasnoch 30-Jan-13 9:50am
Guess I have never seen a need for it. Seems odd IMO. I mean technically we can inject a Winform into a WPF using the host control...

I guess I can edit my last comment of "Not Page" :)
Pete O'Hanlon 30-Jan-13 10:03am
Oops - my 5 hadn't registered. Voted again.
Collin Jasnoch 30-Jan-13 10:05am
Thanks Pete :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You're attempting to instantiate a Page here. This is not a Window - it's behaviour is different in WPF and it does not derive from Window. If you want to spawn a new Window, change your type to Window in the definition of ULink. You'll need to do this in the XAML, and the code-behind the XAML.

If you want to create a Page, however, then you need to look into a completely different layout method altogether. Let me know if you need more details on Page layout (and related things like Frames).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100