简体   繁体   English

如何在Visual Studio中的C ++ / WinRT中添加新的XAML“页面”及其相关的代码隐藏文件?

[英]How to add a new XAML “Page”, and its associated code-behind files, in C++/WinRT in Visual Studio?

There are various related questions that give a hint how to add a new XAML Page properly to a WinRT/C++ project in Visual Studio , but none are specific to C++/WinRT. 有各种相关问题可以提示如何在Visual Studio中为WinRT / C ++项目正确添加新的XAML Page ,但没有一个是C ++ / WinRT特有的。 (Ie, here , here , and here .) (即, 这里这里这里 。)

Further, though WinRT/C++ is a great improvement over C++/CX (in my opinion), almost all Microsoft documentation uses either C# or C++/CX as examples. 此外,尽管WinRT / C ++是对C ++ / CX的重大改进(在我看来),但几乎所有的Microsoft文档都使用C#或C ++ / CX作为示例。

To make matters worse, as of the date of this posting, when you use the Add Item feature in Visual Studio to add a XAML Page , it creates the page in C++/CX even for WinRT/C++ projects. 更糟糕的是,截至发布之日, 当您使用Visual Studio中的“添加项目”功能添加XAML Page即使对于WinRT / C ++项目,它也会在C ++ / CX中创建页面

Finally, there is (as of this date) no documentation provided by Microsoft for how to add a new XAML Page to a WinRT/C++ project, at least that I can find (see first paragraph). 最后,(截至此日期)Microsoft没有提供有关如何将新的XAML Page添加到WinRT / C ++项目的文档,至少我能找到(参见第一段)。

Ugh. 啊。

To save others potentially hours, I have pieced together the hints and succeeded in properly adding a new XAML Page to a Visual Studio WinRT/C++ project. 为了节省其他时间,我将这些提示拼凑在一起,并成功地将新的XAML Page正确添加到Visual Studio WinRT / C ++项目中。

I am asking and answering the question: How do you add a new XAML Page to a WinRT/C++ project in Visual Studio? 我问并回答这个问题: 如何在Visual Studio中为WinRT / C ++项目添加新的XAML Page


Addendum 附录

I was missing the obvious! 我错过了显而易见的事! It turns out you can use Visual Studio to trivally add a WinRT/C++ XAML Page . 事实证明,您可以使用Visual Studio来简单地添加WinRT / C ++ XAML Page Despite looking as carefully as I (thought) I could. 尽管看起来像我想的那样仔细(我想)。

The (easy) correct answer is given in answer #2. 答案#2给出了(简单)正确的答案。

But I'll keep this question and answer up - in case anyone else blunders down the same path and can't find the option to add a XAML Page in WinRT/C++ in Visual Studio. 但我会保留这个问题并回答 - 如果其他人在同一条路径上犯了错误,并且无法在Visual Studio中找到在WinRT / C ++中添加XAML Page的选项。

I was missing the obvious! 我错过了显而易见的事!

It turns out you can use Visual Studio to trivally add a WinRT/C++ XAML Page. 事实证明,您可以使用Visual Studio来简单地添加WinRT / C ++ XAML页面。

Despite looking as carefully as I (thought) I could through the options in Visual Studio's editor, I missed the location of the XAML template for WinRT/C++ projects. 尽管看起来像我想的那样仔细,我可以通过Visual Studio编辑器中的选项,但我错过了WinRT / C ++项目的XAML模板的位置。

So, for completeness, I'll paste screenshots. 所以,为了完整起见,我会粘贴截图。


Here is how you add a new XAML Page to an existing WinRT/C++ project, spelled out in painful detail. 以下是如何向现有的WinRT / C ++项目添加新的XAML Page ,详细说明。

Step 1 : Right-click on the solution, and navigate through the menu/submenus to Add New Item... 步骤1 :右键单击解决方案,然后在菜单/子菜单中导航到Add New Item...

在此输入图像描述

Don't select the XAML node. 不要选择XAML节点。

...Keep the ROOT NODE selected: ...选择ROOT NODE:

在此输入图像描述

Here's what it looks like if you select the 'XAML' node in the Add New Item... entry: 如果您在Add New Item...条目中选择'XAML'节点,它就是这样的:

在此输入图像描述


Bottom line 底线

I lost a few hours because I assumed that it was a good idea to click on the 'XAML' node in the Add New Item... dialog to filter down to just XAML-related templates. 我失去了几个小时,因为我认为单击Add New Item...对话框中的'XAML'节点是一个好主意,以过滤到仅与XAML相关的模板。

Unfortunately, the XAML template for the WinRT/C++ version of the component is not present in that list (as of the day I write this). 不幸的是,组件的WinRT / C ++版本的XAML模板不存在于该列表中(截至我写这篇文章的那一天)。 You have to click on the root node to see it. 您必须单击根节点才能看到它。

I will leave my other answer up, in case it's useful to anybody. 我会留下我的另一个答案,以防它对任何人都有用。

But this answer describes the (trivial) correct way to add a new XAML Page to a WinRT/C++ project - use the Visual Studio editor to Add New Item... , as expected. 但是这个答案描述了将新的XAML Page添加到WinRT / C ++项目的(平凡的)正确方法 - 使用Visual Studio编辑器Add New Item... ,如预期的那样。

To save others potentially hours, I have pieced together the hints and succeeded in properly adding a new XAML Page to a Visual Studio WinRT/C++ project. 为了节省其他时间,我将这些提示拼凑在一起,并成功地将新的XAML Page正确添加到Visual Studio WinRT / C ++项目中。

Here is the answer. 这是答案。

Pictures are worth thousands of words, so here are the steps. 图片价值数千字,所以这里是步骤。

  • Save and close your project in Visual Studio 在Visual Studio中保存并关闭项目
  • Manually open the .vcxproj and .vcxproj.filters files in a text editor 在文本编辑器中手动打开.vcxproj.vcxproj.filters文件

在此输入图像描述

  • Assume we already have the default page, MainPage.xaml and its associated .idl and code-behind files. 假设我们已经有了默认页面MainPage.xaml及其相关的.idl和代码隐藏文件。 In the following steps, we will create a new XAML page and its associated files called LoginPage . 在以下步骤中,我们将创建一个名为LoginPage的新XAML页面及其关联文件。
  • No need to actually create the files for LoginPage yet - we'll do that in the final steps. 无需为LoginPage实际创建文件 - 我们将在最后的步骤中执行此操作。 Just edit the .vcxproj and .vcxproj.filters files for now. 现在就编辑.vcxproj.vcxproj.filters文件。
  • In both of these two files, we will copy every block that contains a reference to MainPage and modify it to read LoginPage . 在这两个文件中,我们将复制包含对MainPage的引用的每个块 ,并将其修改为读取LoginPage
  • Start by editing the .vcxproj file 首先编辑.vcxproj文件

在此输入图像描述

Block 1 : code-behind dependency for the LoginPage.cpp - LoginPage.xaml relationship 块1LoginPage.cpp代码隐藏依赖关系 - LoginPage.xaml关系

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

Block 2 : code-behind dependency for the LoginPage.h - LoginPage.xaml relationship 块2LoginPage.h代码隐藏依赖关系 - LoginPage.xaml关系

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

Block 3 : Let Visual Studio know to use the Designer for LoginPage.xaml (I think!) 第3块 :让Visual Studio知道将Designer用于LoginPage.xaml (我想!)

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

Block 2 : code-behind dependency for the LoginPage.idl - LoginPage.xaml relationship 块2LoginPage.idl代码隐藏依赖关系 - LoginPage.xaml关系

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

  • Proceed to edit the .vcxproj.filters file 继续编辑.vcxproj.filters文件

在此输入图像描述

Block 1 : Tell Visual Studio to include LoginPage.xaml in Solution Explorer (I think!) 第1块 :告诉Visual Studio在解决方案资源管理器中包含LoginPage.xaml (我想!)

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

Block 2 : Tell Visual Studio to include LoginPage.idl in Solution Explorer (I think!) 第2块 :告诉Visual Studio在解决方案资源管理器中包含LoginPage.idl (我想!)

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

Block 3 : Tell Visual Studio to compile LoginPage.cpp as part of this project (I think!) 第3块 :告诉Visual Studio将LoginPage.cpp编译为该项目的一部分(我想!)

We start with this: 我们从这开始:

在此输入图像描述

...Modify the block to add a section for LoginPage , resulting in this: ...修改块以为LoginPage添加一个部分,结果如下:

在此输入图像描述

  • You're done editing the .vcxproj and .vcxproj.filters files. 您已完成编辑.vcxproj.vcxproj.filters文件。 Save and close them. 保存并关闭它们。
  • The rest is simple. 其余的很简单。 Create all four LoginPage files by simply copying the MainPage files. 只需复制MainPage文件即可创建所有四个LoginPage文件。

We start with these files in the project: 我们从项目中的这些文件开始:

在此输入图像描述

...after copying the 4 (highlighted) MainPage files, and renaming them to LoginPage , we end up with these files: ...复制4(突出显示的) MainPage文件,并将它们重命名为LoginPage ,我们最终得到这些文件:

在此输入图像描述

  • For the final step, you must actually open up each of the four new LoginPage files in a text editor (not Visual Studio) and do a search-and-replace for the text MainPage and replace with LoginPage . 对于最后一步,您必须在文本编辑器 (而不是Visual Studio)中实际打开四个新的LoginPage文件中的每一个 ,并对文本MainPage进行搜索和替换 ,并使用LoginPage替换。

    In the search, DO match on capitalization, but DO NOT match on 'whole-word only' (ie, there are some data types that have MainPage as part of the name, such as MainPageT , and these must also be searched-and-replaced). 在搜索中, DO匹配大小写,但是不要匹配'仅全字'(即,有一些数据类型将MainPage作为名称的一部分,例如MainPageT ,这些也必须被搜索 - 和 -代替)。

    I won't bore you by pasting screenshots of the actual source code files. 通过粘贴实际源代码文件的屏幕截图,我不会厌烦你。

  • Once you have replaced all occurrences of MainPage with LoginPage in the four files noted, save and close the file. 在所记录的四个文件中使用LoginPage替换所有出现的MainPage ,保存并关闭该文件。

  • You're done! 你完成了!

Open the solution in Visual Studio, and notice the beauty: 在Visual Studio中打开解决方案,注意美观:

Solution Explorer now shows our LoginPage.xaml file along with its code-behind files, properly nested: 解决方案资源管理器现在显示我们的LoginPage.xaml文件及其代码隐藏文件,正确嵌套:

在此输入图像描述

...and pressing F7 to build the project results in a successful build: ...并按F7构建项目会导致成功构建:

在此输入图像描述

...and it successfully runs, as well - unchanged from what it did before. ......它也成功运行 - 与之前的情况完全不同。

This is because, of course, you haven't DONE anything with LoginPage yet - it doesn't appear in your user interface - but it's available for use in code and I'll let you take it from here! 这是因为,当然,你还没有使用LoginPage完成任何事情 - 它没有出现在你的用户界面中 - 但它可以在代码中使用,我会让你从这里拿走它!

As grateful as I am that Microsoft has released the excellent WinRT/C++ functionality - it is rather ... frustrating ... to say the least - that as of this date there is no way to add a new XAML Page to a WinRT/C++ project using Visual Studio standard tooling (such as the Add Item option), so far as I can see. 感谢我微软已经发布了优秀的WinRT / C ++功能 - 至少......令人沮丧...至少 - 截至此日期, 没有办法将新的XAML Page添加到WinRT / C ++项目使用Visual Studio标准工具(例如Add Item选项),据我所知。 It is further... frustrating... that there is also no documentation whatsoever that explains how to do it (that I can find). 更令人沮丧的是......也没有任何文件可以解释如何做到(我能找到)。

I sure hope that my question and answer become irrelevant very soon and that Microsoft adds both tooling and documentation re. 我确信希望我的问题和答案很快就会变得无关紧要,并且微软会同时添加工具和文档。 adding a new XAML Page to an existing WinRT/C++ project in Visual studio. 将新的XAML Page添加到Visual Studio中的现有WinRT / C ++项目中。

Or maybe I'm mistaken? 或许我错了? Maybe it's already possible to do this easily, and I just couldn't find it? 也许已经可以轻松地做到这一点,而我却找不到它?

I hope this helps someone, though, just in case! 我希望这可以帮助某人,以防万一!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Visual Studio“合并”XAML文件及其代码隐藏 - How to 'merge' a XAML file and its code-behind with Visual Studio Visual Studio 2005停止添加代码隐藏文件 - Visual Studio 2005 stopped adding code-behind files 从XAML切换到Xamarin.Forms中的代码隐藏的Visual Studio热键 - Visual Studio hotkey to switch from XAML to code-behind in Xamarin.Forms 使用 Visual Studio 制作非 aspx 代码隐藏页面 - Using Visual Studio to make non aspx code-behind pages XAML UI更新在代码隐藏中不可见 - XAML UI updates not visible in code-behind Visual Studio C++ 如何在前缀文件夹中添加 header 文件 - Visual Studio C++ how to add header files in a prefix folder 如何在C ++ Visual Studio中从Xaml动态删除按钮? - How to dynamically remove a button from Xaml in C++ Visual Studio? 如何使用 Visual Studio 中的设计器在 WinForms 中的 DataGridView 中为列 header 创建多行工具提示(非代码隐藏) - How to create multi-line tooltips for a column header in a DataGridView in WinForms using the designer in Visual Studio (NOT code-behind) Visual Studio 中从代码隐藏切换到 Xaml 代码的捷径是什么? - What is the shorcut in visual studio to switch from code behind to Xaml Code? C++ 项目找不到它的头文件(visual studio) - C++ project is not finding its header files (visual studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM