1

I am trying to run through the very basic example on the page below, following the instructions for WinForms

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

I keep getting errors when trying to drag a WindowsXamlHost control onto the designer

I have been communicating on MSDN forums and this process describes to problem and what I have tried, please see the following link Problem with WinForms

Can anyone help please with my problem. I really need to try and get some modern UPC controls into my WinForms program.

Kind regards

Steve

SWiggins
  • 13
  • 2

2 Answers2

0

Thanks for your feed back, I can reproduce this problem, and it looks Visual Studio strange behavior, and please feel free post this in Microsoft.Toolkit.Win32 github issue box. Currently, there is a workaround that initials WindowsXamlHost in the code behind, then call below in the InitializeComponent method.

For 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);
}

Please note: if you encounter Catastrophic failure" exception please add app.manifest file and write below content, then switch WinForm app's default manifest to app.manifest.(right click your project -> application -> Manifest) For the detail please refer Matteo Pagani blog.

<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>
Vizel Leonid
  • 456
  • 7
  • 15
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Thanks for the answer. I have raised an issue on GitHub so let's see if that helps. The example for creating at runtime appears to be for a wpf application not a winforms version. So I can't implement that. Thanks again Steve – SWiggins May 01 '20 at 12:35
  • @SWiggins, I have edited default code, It could works in winform app, but you need add app.manifest manually – Nico Zhu May 01 '20 at 14:32
  • Hi Nico Was just about to comment on that, I did not notice you had edited the example. Your version works...thanks – SWiggins May 01 '20 at 15:15
  • Not sure whether to post a new question or add to this one. I will mark this as answer as it has solved my initial problem. I have managed to add three controls to my form, Button, ToggleSwitch & Slider. The first two look great, but the slider has a white box surrounding it and I can't seem to change this. I have tried all the various colour properties but cannot set it to transparent or the system colour. Any advice? Cheers – SWiggins May 01 '20 at 15:23
  • @SWiggins, the better way is create new thread for the question and add the screenshot code. I will check it on Monday. – Nico Zhu May 02 '20 at 13:04
  • 1
    Ok Nico, I have just posted a new question. Cheers. – SWiggins May 04 '20 at 14:50
0

I had the same problem when trying to implement XAML Islands for WinForms using Visual Studio 2017, and here is what I did to work around the problem.

I had originally installed version 6.0 of Microsoft.Toolkit.Forms.UI.XamlHost. I uninstalled that version, and installed version 5.1 instead.

I then upgraded my project to use .Net 4.6.2 or later, and dragging the XAML Host control onto the designer worked for me.

Note that this site https://github.com/dotnet/wpf/issues/1290 seemed to indicate that the problem was fixed in later versions, but I could not get those to work.

Greg Thatcher
  • 1,303
  • 20
  • 29