4 月 9 日更新:发布了 2019 年的新版本,再次恢复了 UWP 模板
从VS2019中删除UWP模板是正确的,我不知道它背后的原因,但我们必须处理它。 但是有一个页面可以让您逐步完成整个过程。在 Visual Studio for Mac 中创建的项目已经存在了一段时间,这些项目也不包括 UWP 模板。完整的描述可以在这里找到:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/windows/installation/来自这个页面:
首先,右击解决方案并选择Add > New Project...并添加一个Blank App (Universal Windows)项目:
然后,选择最低和目标 UWP 版本。
右键单击 UWP 项目并选择 Manage NuGet Packages... 并添加 Xamarin.Forms 包。确保解决方案中的其他项目也更新到相同版本的 Xamarin.Forms 包。
另外,确保将在 Build > Configuration 管理器窗口中构建新的 UWP 项目(默认情况下这可能不会发生)。勾选 Universal 项目的 Build 和 Deploy 框:
右键单击 UWP 项目并选择 添加 > 引用,然后创建对 Xamarin.Forms 应用程序项目(.NET Standard 或共享项目)的引用。
在 UWP 项目中,编辑 App.xaml.cs 以在第 52 行附近的 OnLaunched 方法中包含 Init 方法调用:
// under this line
rootFrame.NavigationFailed += OnNavigationFailed;
// add this line
Xamarin.Forms.Forms.Init (e); // requires the `e` parameter
在 UWP 项目中,通过删除包含在 Page 元素中的 Grid 来编辑 MainPage.xaml。然后在MainPage.xaml 中,为Xamarin.Forms.Platform.UWP 添加一个新的xmlns 条目,如下所示:xmlns:forms="using:Xamarin.Forms.Platform.UWP"。
在MainPage.xaml 中,更改根<Page element to <forms:WindowsPage,如下所示:
<forms:WindowsPage
...
xmlns:forms="using:Xamarin.Forms.Platform.UWP"
...
</forms:WindowsPage>
在 UWP 项目中,编辑 MainPage.xaml.cs 以删除:Page 类名的继承说明符(因为由于上一步中所做的更改,它现在将从 WindowsPage 继承)
public sealed partial class MainPage // REMOVE ": Page"
在MainPage.xaml.cs 中,在MainPage 构造函数中添加LoadApplication 调用以启动Xamarin.Forms 应用:
// below this existing line
this.InitializeComponent();
// add this line
LoadApplication(new YOUR_NAMESPACE.App());
从现有平台项目中添加所需的任何本地资源(例如图像文件)。
现在它应该像以前一样工作了。我知道,对于以前刚刚起作用的东西来说,经历它是一件麻烦事。很抱歉,但这也应该能让你到达那里。