如何添加后面带有代码的新XAML视图 [英] How to add a new XAML View with code behind

查看:40
本文介绍了如何添加后面带有代码的新XAML视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2015,创建了一个Univerasl应用程序.我想创建一个新视图(XAML).我可以右键单击添加">"XAML">"XAML视图",然后使用所需的名称和位置创建XAML.

I am using VS 2015, creating a Univerasl App. I want to create a new view (XAML). I can right click, Add > XAML > XAML View, and the XAML gets created with the name and location that I want.

但是,如何在此处创建代码,例如MyNewView.xaml.cs,并在解决方案资源管理器中将其链接"为子节点?

But, how can I create a code behind here, e.g. MyNewView.xaml.cs, and "link it up" as a child node in my solution explorer?

推荐答案

RavingDev 所述:

请勿使用"XAML视图",而应使用空白页"或用户控件".

Do not use "XAML View", instead use "Blank Page" or "User Control".


在旁注中,如果要手动创建代码文件并将其与其他任何文件链接(即,Visual Studio在创建时会自动链接 .cs .xaml ),则必须编辑项目的XML代码.


On a side note, if you want to manually create a code file and link it with anything else (i.e. Visual Studio automatically links .cs and .xaml on creation), you'll have to edit the project's XML code.

假设您创建了一个名为 MyView.xaml 的XAML视图/页面/控件和一个名为 MyView.xaml.cs 的单独的C#文件,并且它们是未链接的(此如果您直接将文件添加到解决方案资源管理器中,也会发生这种情况.要链接它们,您将必须编辑项目的内部代码.首先,保存并退出Visual Studio.其次,找到您的项目文件(<项目名称> .csproj ).使用文本编辑器(例如Notepad ++,VS Code或Atom(非Visual Studio ))将其打开.向下移动文件,直到看到 ItemGroup 元素.其中有一些,但是包含 Compile 元素的是正确的元素.在该元素内的某处添加以下代码:

Assume you created a XAML view/page/control named MyView.xaml and a separate C# file named MyView.xaml.cs, and they're unlinked (this can also happen if you add files directly into the Solution Explorer). To link them, you will have to edit your project's internal code. First, save and quit Visual Studio. Second, find your project file (<project name>.csproj). Open it with a text editor, such as Notepad++, VS Code, or Atom (not Visual Studio). Move down the file until you see ItemGroup elements. There are a few of them, but the one that contains Compile elements is the right one. Add the following code somewhere inside that element:

<Compile Include="MyView.xaml.cs">
  <DependentUpon>MyView.xaml</DepenedentUpon>
</Compile>

对要链接的每个文件执行此操作.如果一切都正确完成,则可以保存文件,然后在Visual Studio中将其打开备份.您的文件现在应该在解决方案资源管理器中链接了.

Do this for every file you want to link. If everything was done correctly, you can save the file and open it back up in Visual Studio. Your files should now be linked in the Solution Explorer.

这篇关于如何添加后面带有代码的新XAML视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆