学习WPF& C# - 第一天 [英] Learning WPF & C# - Day one
问题描述
我今天决定开始学习c#和wpf,并确信还会有100个帖子可以关注:)
我打算关注一些教程今晚在这里,但在我工作的时候,我想我会快速玩一下。
我创建了一个带有一些按钮的页面,每个按钮都会产生一个新的形式。
第一个按钮将始终有效,但是一旦我添加了几个按钮,我就会开始出错。
我可以解释为什么会这样吗?
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?
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
我收到所有其他按钮的以下错误
无法隐式转换类型''Dehub.Ulinks'' to''System.Windows.Window''C:\Development\c#\Dehub \Dehub \ MainWindow.xaml.cs 36 26 Dehub
更新2
这是Ulinks的XMAL
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>
推荐答案
您正在尝试在此处实例化一个页面。这不是一个窗口 - 它的行为在WPF中是不同的,它不是从Window派生的。如果要生成新窗口,请在ULink定义中将类型更改为Window。你需要在XAML中执行此操作,以及XAML背后的代码。
如果你想创建一个页面,那么你需要完全看一下完全不同的布局方法。如果您需要有关页面布局(以及框架等相关内容)的更多详细信息,请告诉我。
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).
无论您的Ulinks
是什么类型Window
因此,施法Window win = new Ulinks();
无效。
编辑:
现在您已经发布了XAML,它确认了这一点。Page
与Window
不同。你需要使用正确的类型。
似乎你正在使用WPF与Silverlight / Windows商店应用程序进行网格划分。 WPF使用UserControl
和Window
。不。您可以在WPF中使用页面,但这不是标准做法。正如你所说,你正在学习它,所以坚持基础。Page
。
Whatever yourUlinks
is not of typeWindow
therefore the castWindow win = new Ulinks();
is invalid.
Now that you have posted your XAML it confirms this.Page
is not the same asWindow
. You need to use the correct type.
Seems you are meshing Silverlight/Windows store apps with WPF. WPF usesUserControl
andWindow
.Not. You can use page in WPF but that is not standard practice. As you said you are learning it, so stick with the basics.Page
.
这篇关于学习WPF& C# - 第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!