[英]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. 图片价值数千字,所以这里是步骤。
.vcxproj
and .vcxproj.filters
files in a text editor .vcxproj
和.vcxproj.filters
文件 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页面及其关联文件。 LoginPage
yet - we'll do that in the final steps. LoginPage
实际创建文件 - 我们将在最后的步骤中执行此操作。 Just edit the .vcxproj
and .vcxproj.filters
files for now. .vcxproj
和.vcxproj.filters
文件。 MainPage
and modify it to read LoginPage
. MainPage
的引用的每个块 ,并将其修改为读取LoginPage
。 .vcxproj
file .vcxproj
文件 Block 1 : code-behind dependency for the LoginPage.cpp
- LoginPage.xaml
relationship 块1 :
LoginPage.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 块2 :
LoginPage.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 块2 :
LoginPage.idl
代码隐藏依赖关系 - LoginPage.xaml
关系
We start with this: 我们从这开始:
...Modify the block to add a section for LoginPage
, resulting in this: ...修改块以为
LoginPage
添加一个部分,结果如下:
.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
添加一个部分,结果如下:
.vcxproj
and .vcxproj.filters
files. .vcxproj
和.vcxproj.filters
文件。 Save and close them. 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
,保存并关闭该文件。
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.