See the question and my original answer on StackOverflow

You forgot to detach the geometry from the path, and the constructor just creates a new grid, it doesn't gets the one from current content.

The strict equivalent would be this (note it's better to use std::wstring to benefit from natural hstring conversions - Windows is all Unicode for a long time now):

  using namespace winrt;
  using namespace Windows::Foundation;
  using namespace Microsoft::UI;
  using namespace Microsoft::UI::Xaml;
  using namespace Microsoft::UI::Xaml::Controls;
  using namespace Microsoft::UI::Xaml::Media;
  using namespace Microsoft::UI::Xaml::Markup;
  using namespace Microsoft::UI::Xaml::Shapes;

  ...

    MainWindow::MainWindow()
    {
        InitializeComponent();

        std::wstring str;
        str = L"M 0 0 L 0 100 M 0 50 L 50 50 M 50 0 L 50 100 ";
        str += L"M 125 0 C 60 -10, 60 60, 125 50, 60 40, 60 110, 125 100 ";
        str += L"M 150 0 L 150 100, 200 100 ";
        str += L"M 225 0 L 225 100, 275 100 ";
        str += L"M 300 50 A 25 50 0 1 0 300 49.9";
        Path path;
        path.Stroke(SolidColorBrush(Colors::Red()));
        path.StrokeThickness(12);
        path.StrokeLineJoin(PenLineJoin::Round);
        path.HorizontalAlignment(HorizontalAlignment::Center);
        path.VerticalAlignment(VerticalAlignment::Center);
        path.Data(PathMarkupToGeometry(str));

        Content().try_as<Grid>().Children().Append(path);
    }

    Geometry MainWindow::PathMarkupToGeometry(const std::wstring& pathMarkup)
    {
        std::wstring xaml = L"<Path ";
        xaml += L"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>";
        xaml += L"<Path.Data>";
        xaml += pathMarkup;
        xaml += L"</Path.Data></Path>";

        auto path = XamlReader::Load(xaml).try_as<Path>();

        Geometry geometry = path.Data();
        path.Data(nullptr);
        return geometry;
    }

It's generally better to use XAML, bindings, templates, controls, custom controls, etc. than to load and mangle raw XAML "manually", but I've kept your original code spirit. Also Petzold's book might lag a bit with respect to the all new WinUI3. And all this is so much easier in C# but that's another subject :-)