Click here to Skip to main content
11,412,833 members (72,501 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Links
Hi,
I have decided to start learning c# and wpf today and am sure there will be 100 more posts to follow Smile | :)
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()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Window win = new bapp();
            win.Show();
        }
 
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Window win = new Ulinks();
            win.Show();
        }
 
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Window win = new tasks();
            win.Show();
        }
 
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Window win = new settings();
            win.Show();
        }
    }
}


UPDATE 1

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


UPDATE 2

Here is the XMAL of Ulinks
 
<Page x:Class="Dehub.Ulinks"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	Title="Ulinks">
 
    <Grid>



Posted 30-Jan-13 3:42am
Edited 30-Jan-13 4:18am
v4
Comments
Shubh Agrahari at 30-Jan-13 8:53am
   
what error r you getting...???
Tony Constantinides at 30-Jan-13 8:57am
   
Silly me, added to the question.
Thanks
Pete O'Hanlon at 30-Jan-13 9:14am
   
Could you post the XAML of Ulinks please?
MadMellon at 30-Jan-13 9:19am
   
added as requested.
Richard MacCutchan at 30-Jan-13 9:16am
   
I would suggest you forget WPF until you understand C# and .NET a little better.
MadMellon at 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
Richard MacCutchan at 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.
Shubh Agrahari at 30-Jan-13 9:16am
   
once try this on every button click event...
Window win = new Window();
win.Show();
MadMellon at 30-Jan-13 9:23am
   
This creates a new window on each button now. Obviously not the windows I have created (eg ulinks)
MadMellon at 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
good
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.

EDIT:

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.
  Permalink  
v3
Comments
Pete O'Hanlon at 30-Jan-13 9:28am
   
You can use Page in WPF. It's not that common, but it is useful occasionally.
Collin Jasnoch at 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 at 30-Jan-13 10:03am
   
Oops - my 5 hadn't registered. Voted again.
Collin Jasnoch at 30-Jan-13 10:05am
   
Thanks Pete :-)
Rate this: bad
good
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).
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 404
1 Sascha Lefévre 200
2 Maciej Los 150
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,317
2 Maciej Los 3,570
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web04 | 2.8.150427.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2015
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