感谢您的反馈,我可以重现此问题,它看起来 Visual Studio 的奇怪行为,请随时在Microsoft.Toolkit.Win32 github 问题框中发布此问题。目前,有一种解决方法,即WindowsXamlHost
在后面的代码中缩写,然后在方法中调用下面的InitializeComponent
方法。
对于 WinForms
private void CreateUWPControlsFirst()
{
Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread();
Windows.UI.Xaml.Controls.Button myButton = new Windows.UI.Xaml.Controls.Button();
myButton.Name = "button1";
myButton.Width = 75;
myButton.Height = 40;
myButton.TabIndex = 0;
myButton.Content = "button1";
myButton.Click += MyButton_Click;
Microsoft.Toolkit.Forms.UI.XamlHost.WindowsXamlHost myHostControl =
new Microsoft.Toolkit.Forms.UI.XamlHost.WindowsXamlHost();
myHostControl.Name = "myWindowsXamlHostControl";
myHostControl.Child = myButton;
myHostControl.Height = 500;
myHostControl.Width = 600;
this.Controls.Add(myHostControl);
}
请注意:如果遇到Catastrophic failure" exception
请添加app.manifest
文件并写入以下内容,然后将win从应用程序的默认清单切换到app.manifest。(右键单击您的项目->应用程序->清单)详细信息请参阅Matteo Pagani博客。
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<maxversiontested Id="10.0.18358.0"/>
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>