Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get CsWinRT going #1453

Closed
MichalStrehovsky opened this issue Aug 24, 2021 · 8 comments
Closed

Get CsWinRT going #1453

MichalStrehovsky opened this issue Aug 24, 2021 · 8 comments
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation help wanted Extra attention is needed

Comments

@MichalStrehovsky
Copy link
Member

MichalStrehovsky commented Aug 24, 2021

CsWinRT is a necessary prerequisite for WinUI support.

We're close to getting some of it going but it doesn't work yet. The below code should pop a notification. Nothing happens instead. I haven't tried to troubleshoot where the problem is (is there a problem in the XmlDocument? Maybe try saving it to a StorageFile first? Etc.)

Eventually we'll also need to make CsWinRT AOT and trimming friendly. It's neither of those right now.

CSPROJ:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
    <TrimMode>link</TrimMode>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-*" />
    <RdXmlFile Include="rd.xml" />
  </ItemGroup>
</Project>

RD.XML:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="Microsoft.Windows.SDK.NET">
      <Type Name="ABI.Windows.UI.Notifications.IToastNotificationManagerStatics" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlDocument" Dynamic="Required All" />
      <Type Name="Windows.Data.Xml.Dom.XmlNodeList" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlNodeList" Dynamic="Required All" />

      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlNode" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlNodeSerializer" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlNodeSelector" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlDocumentIO" Dynamic="Required All" />
      <Type Name="ABI.Windows.Data.Xml.Dom.IXmlDocumentIO2" Dynamic="Required All" />
      <Type Name="ABI.Windows.UI.Notifications.IToastNotificationFactory" Dynamic="Required All" />
      <Type Name="ABI.Windows.UI.Notifications.IToastNotification" Dynamic="Required All" />
      <Type Name="Windows.UI.Notifications.ToastNotifier" Dynamic="Required All" />
      <Type Name="ABI.Windows.UI.Notifications.IToastNotifier" Dynamic="Required All" />
    </Assembly>
    <Assembly Name="WinRT.Runtime">
      <Type Name="ABI.System.Collections.Generic.IReadOnlyList`1" Dynamic="Required All" />
      <Type Name="WinRT.IObjectReference">
        <Method Name="As" Dynamic="Required">
          <GenericArgument Name="ABI.System.Collections.Generic.IReadOnlyList`1+Vftbl[[Windows.Data.Xml.Dom.IXmlNode, Microsoft.Windows.SDK.NET]]" />
        </Method>
      </Type>
      <Type Name="ABI.System.Collections.Generic.IReadOnlyList`1+Vftbl" Dynamic="Required All" />
    </Assembly>
  </Application>
</Directives>

C#

using System;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

class Program
{
    static void Main(string[] args)
    {
        // This hits AOT hostile code in CsWinRT, so working around by building XML manually below:
        /*XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

        // Fill in the text elements
        XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
        for (int i = 0; i < stringElements.Length; i++)
        {
            stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
        }*/

        string title = "featured picture of the day";
        string content = "beautiful scenery";

        string xmlString =
        $"<toast><visual><binding template=\"ToastText02\"><text>{title}</text><text>{content}</text></binding></visual></toast>";
        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(xmlString);

        ToastNotification toast = new ToastNotification(toastXml);

        ToastNotificationManager.CreateToastNotifier("MyAppIddd").Show(toast);
    }
}
@MichalStrehovsky MichalStrehovsky added help wanted Extra attention is needed area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation labels Aug 24, 2021
@MichalStrehovsky
Copy link
Member Author

For whoever is looking into this - if you start hitting null dereferences in generic [DynamicInterfaceCastableImplementation] interfaces, that's dotnet/runtime#72909. It's fixable, but it's a day or two of work for someone knowledgeable.

@MichalStrehovsky
Copy link
Member Author

Oh, also the CsWinRT sample above depends on #1443 and #1454 getting merged.

@kant2002 FYI - this is in your general area of interest. We have enough of IDynamicInterfaceCastable implemented that this might be feasible.

@jkoritzinsky
Copy link
Member

cc: @AaronRobinsonMSFT @elinor-fung for interop team visibility

@kant2002
Copy link
Contributor

@MichalStrehovsky can you change package version to 7.0 in the original issue ? 😉

@kant2002
Copy link
Contributor

kant2002 commented Sep 9, 2021

@MichalStrehovsky add Thread.Sleep(1000) at the end of main. Then with #1544 you code will simply works!

@MichalStrehovsky
Copy link
Member Author

@kant2002 got it going and there's no action items left at this point. We'll need to make it more AOT friendly but that will be work in the CsWinRT repo.

@kant2002
Copy link
Contributor

In case somebody interesting, how I plan move that forward, place to start would be microsoft/CsWinRT#989

@MichalStrehovsky
Copy link
Member Author

In case somebody interesting, how I plan move that forward, place to start would be microsoft/CsWinRT#989

Ah, nice, thanks! Clicked "Subscribe" on that one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants