Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
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?

C#
 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
XML
<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
Updated 30-Jan-13 3:18am
v4
Comments
Shubh Agrahari 30-Jan-13 8:53am    
what error r you getting...???
MadMellon 30-Jan-13 8:57am    
Silly me, added to the question.
Thanks
Pete O'Hanlon 30-Jan-13 9:14am    
Could you post the XAML of Ulinks please?
MadMellon 30-Jan-13 9:19am    
added as requested.
Richard MacCutchan 30-Jan-13 9:16am    
I would suggest you forget WPF until you understand C# and .NET a little better.

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).
 
Share this answer
 
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.
 
Share this answer
 
v3
Comments
Pete O'Hanlon 30-Jan-13 9:28am    
You can use Page in WPF. It's not that common, but it is useful occasionally.
[no name] 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.
[no name] 30-Jan-13 10:05am    
Thanks Pete :-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900