我的第一个 WPF 应用程序中的 WPF window_loaded 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17397905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:06:42  来源:igfitidea点击:

WPF window_loaded exception in my first WPF application

c#.netwpfmvvmwpf-controls

提问by Learner

I am learning WPF and its my first MVVM application. I am seeing a video where the MVVM is demonstrated using Silverlight app. However, I am mimicking it for WPF. in Silverlight, the video presenter creates an event in UserControl XAML tag namely UserControl_Loaded which is successful.

我正在学习 WPF 及其第一个 MVVM 应用程序。我正在观看使用 Silverlight 应用程序演示 MVVM 的视频。但是,我正在为 WPF 模仿它。在 Silverlight 中,视频演示者在 UserControl XAML 标记中创建一个成功的事件,即 UserControl_Loaded。

However, I am creating a Window_Loaded in Window root element of xaml file of MainWindow.xaml

但是,我在 MainWindow.xaml 的 xaml 文件的 Window 根元素中创建了一个 Window_Loaded

I am getting this error :( Please help me. This event "Window_Loaded" is in this 5th line only.

我收到此错误 :( 请帮助我。此事件“Window_Loaded”仅在第 5 行中。

Exception:

例外:

'Add value to collection of type 'System.Windows.Controls.UIElementCollection' threw an exception.' Line number '5' and line position '28'.

Inner Exception:

{"Window must be the root of the tree. Cannot add Window as a child of Visual."}

'向'System.Windows.Controls.UIElementCollection'类型的集合添加值引发异常。行号“5”和行位置“28”。

内部异常:

{"Window 必须是树的根。不能将 Window 添加为 Visual 的子项。"}

Please also explain me why it is happening along with the solution. I am just trying to learn MVVM and using observable collection, INotifyPropertyChanged and a model class and I am generating data of this object in hardcoded way.

还请向我解释为什么会发生这种情况以及解决方案。我只是想学习 MVVM 并使用可观察集合、INotifyPropertyChanged 和模型类,我正在以硬编码方式生成此对象的数据。

Note:

笔记:

I suspect it is due to clr-namespace.

我怀疑这是由于 clr-namespace。

First, I am wondering, why if I type clr, intellisense is not taking me to clr ? Rather it just takes me to first http in the sequence of list of namespaces. Second, the Silverlight Video used the namespace of the project, however, I used the namespace of the View. Because, when I use the namespace of the project, it gives me error saying "view" is not found. Anywhere I am going wrong :(

首先,我想知道,为什么如果我输入 clr,intellisense 不会带我去 clr ?相反,它只是让我按照命名空间列表的顺序访问第一个 http。其次,Silverlight Video使用了项目的命名空间,而我使用的是View的命名空间。因为,当我使用项目的命名空间时,它给我错误说找不到“视图”。我哪里出错了:(

(OR):

(或者):

Is it due to a new Window.xaml page that I am trying to insert into another window.xaml page ? Unlike the Silverlight video where it does insert a UserControl into a Page.

是因为我试图插入另一个 window.xaml 页面的新 Window.xaml 页面吗?与 Silverlight 视频不同,它确实将 UserControl 插入到页面中。

MainWindow into MainWindow is a problem here ? If I use UserControl into MainWindow will it be resolved ? Meaning tha, we cnn't have window into window ?

MainWindow 到 MainWindow 是这里的问题吗?如果我在 MainWindow 中使用 UserControl 会解决它吗?意思是,我们没有窗口到窗口?

回答by Vale

You can not have a Window inside another Window! Window should be the root element of your xaml, and you can add other elements inside it. For example Grid. My suggestion is to find some WPF tutorials and books (not silverlight), for starting, because WPF is much more powerfull, and in many ways different.

你不能在另一个窗口中有一个窗口!Window 应该是您的 xaml 的根元素,您可以在其中添加其他元素。例如网格。我的建议是找一些 WPF 教程和书籍(不是 Silverlight)作为开始,因为 WPF 更强大,而且在很多方面都不同。

回答by Krish

Window.xaml:

窗口.xaml:

<Window  x:Class="WpfMvvmApplication1.Views.Login"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:View="clr-namespace:WpfMvvmApplication1.Views">
<Grid>
   <View:UserControl/>
</Grid>
</Window>

This will import your usercontrol into window./

这会将您的用户控件导入窗口。/

Hope this will help you.

希望这会帮助你。