1

我正在尝试按照 WinForms 的说明运行下面页面上的非常基本的示例

https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost

尝试将 WindowsXamlHost 控件拖到设计器上时,我不断收到错误消息

我一直在 MSDN 论坛上进行交流,此过程描述了问题以及我尝试过的内容,请参阅以下链接WinForms 问题

谁能帮我解决我的问题。我真的需要尝试在我的 WinForms 程序中加入一些现代 UPC 控件。

亲切的问候

史蒂夫

4

2 回答 2

0

在尝试使用 Visual Studio 2017 为 WinForms 实现 XAML 岛时,我遇到了同样的问题,这就是我为解决该问题所做的工作。

我最初安装了 Microsoft.Toolkit.Forms.UI.XamlHost 6.0 版。我卸载了那个版本,而是安装了 5.1 版。

然后我将我的项目升级为使用 .Net 4.6.2 或更高版本,并将 XAML 主机控件拖到为我工作的设计器上。

请注意,此站点https://github.com/dotnet/wpf/issues/1290似乎表明该问题已在以后的版本中得到解决,但我无法使它们正常工作。

于 2020-05-03T18:32:22.747 回答
0

感谢您的反馈,我可以重现此问题,它看起来 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>
于 2020-05-01T06:26:17.817 回答