Is it possible to programatically create a xaml page?












3















I might write no sense on the next lines:



I tried to derive a struct named mainpage from winrt::Windows::UI::Xaml::Controls::Page and pass it to winrt::xaml_typename<>(), in "<>" is mainpage.



It will be understandable when you look at the code:
Point number 1 "(1)" is the mainpage.h file, is very short.
Point number 2 "(2)" is the App.cpp file, it only has the necessary.



//(1) mainpage.h

#pragma once

#include "pch.h"

struct mainpage : winrt::Windows::UI::Xaml::Controls::Page
{
mainpage() {

winrt::Windows::UI::Xaml::Controls::Button thebutton = winrt::Windows::UI::Xaml::Controls::Button();
thebutton.Click([&](const IInspectable& sender, const winrt::Windows::UI::Xaml::RoutedEventArgs& event_arg)
{
thebutton.Content(winrt::box_value(L"Clicked"));
});
}
};

//(2) App.cpp

#include "pch.h"
#include "mainpage.h"

struct App : winrt::Windows::UI::Xaml::ApplicationT<App>
{
void OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs& event_arg)
{
winrt::Windows::UI::Xaml::Controls::Frame root_frame{ nullptr };
winrt::Windows::UI::Xaml::UIElement content = winrt::Windows::UI::Xaml::Window::Current().Content();
if (content)
{
root_frame = content.try_as<winrt::Windows::UI::Xaml::Controls::Frame>();
}

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (root_frame == nullptr)
{
// Create a Frame to act as the navigation context and associate it with
// a SuspensionManager key
root_frame = winrt::Windows::UI::Xaml::Controls::Frame();

root_frame.NavigationFailed({ this, &App::OnNavigationFailed });

if (event_arg.PreviousExecutionState() == winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState::Terminated)
{
// Restore the saved session state only when appropriate, scheduling the
// final launch steps after the restore is complete
}

if (event_arg.PrelaunchActivated() == false)
{
if (root_frame.Content() == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
}
// Place the frame in the current Window
winrt::Windows::UI::Xaml::Window::Current().Content(root_frame);
// Ensure the current window is active
winrt::Windows::UI::Xaml::Window::Current().Activate();
}
}
else
{
if (event_arg.PrelaunchActivated() == false)
{
if (root_frame.Content() == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
}
// Ensure the current window is active
winrt::Windows::UI::Xaml::Window::Current().Activate();
}
}
}
void App::OnNavigationFailed(const IInspectable&, const winrt::Windows::UI::Xaml::Navigation::NavigationFailedEventArgs& event_arg)
{
throw winrt::hresult_error(E_FAIL, winrt::hstring(L"Failed to load Page ") + event_arg.SourcePageType().Name);
}
};

int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
winrt::Windows::UI::Xaml::Application::Start((auto &&) { winrt::make<App>(); });
}


If it is not possible, how can I properly create a page?










share|improve this question





























    3















    I might write no sense on the next lines:



    I tried to derive a struct named mainpage from winrt::Windows::UI::Xaml::Controls::Page and pass it to winrt::xaml_typename<>(), in "<>" is mainpage.



    It will be understandable when you look at the code:
    Point number 1 "(1)" is the mainpage.h file, is very short.
    Point number 2 "(2)" is the App.cpp file, it only has the necessary.



    //(1) mainpage.h

    #pragma once

    #include "pch.h"

    struct mainpage : winrt::Windows::UI::Xaml::Controls::Page
    {
    mainpage() {

    winrt::Windows::UI::Xaml::Controls::Button thebutton = winrt::Windows::UI::Xaml::Controls::Button();
    thebutton.Click([&](const IInspectable& sender, const winrt::Windows::UI::Xaml::RoutedEventArgs& event_arg)
    {
    thebutton.Content(winrt::box_value(L"Clicked"));
    });
    }
    };

    //(2) App.cpp

    #include "pch.h"
    #include "mainpage.h"

    struct App : winrt::Windows::UI::Xaml::ApplicationT<App>
    {
    void OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs& event_arg)
    {
    winrt::Windows::UI::Xaml::Controls::Frame root_frame{ nullptr };
    winrt::Windows::UI::Xaml::UIElement content = winrt::Windows::UI::Xaml::Window::Current().Content();
    if (content)
    {
    root_frame = content.try_as<winrt::Windows::UI::Xaml::Controls::Frame>();
    }

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (root_frame == nullptr)
    {
    // Create a Frame to act as the navigation context and associate it with
    // a SuspensionManager key
    root_frame = winrt::Windows::UI::Xaml::Controls::Frame();

    root_frame.NavigationFailed({ this, &App::OnNavigationFailed });

    if (event_arg.PreviousExecutionState() == winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState::Terminated)
    {
    // Restore the saved session state only when appropriate, scheduling the
    // final launch steps after the restore is complete
    }

    if (event_arg.PrelaunchActivated() == false)
    {
    if (root_frame.Content() == nullptr)
    {
    // When the navigation stack isn't restored navigate to the first page,
    // configuring the new page by passing required information as a navigation
    // parameter
    root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
    }
    // Place the frame in the current Window
    winrt::Windows::UI::Xaml::Window::Current().Content(root_frame);
    // Ensure the current window is active
    winrt::Windows::UI::Xaml::Window::Current().Activate();
    }
    }
    else
    {
    if (event_arg.PrelaunchActivated() == false)
    {
    if (root_frame.Content() == nullptr)
    {
    // When the navigation stack isn't restored navigate to the first page,
    // configuring the new page by passing required information as a navigation
    // parameter
    root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
    }
    // Ensure the current window is active
    winrt::Windows::UI::Xaml::Window::Current().Activate();
    }
    }
    }
    void App::OnNavigationFailed(const IInspectable&, const winrt::Windows::UI::Xaml::Navigation::NavigationFailedEventArgs& event_arg)
    {
    throw winrt::hresult_error(E_FAIL, winrt::hstring(L"Failed to load Page ") + event_arg.SourcePageType().Name);
    }
    };

    int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
    winrt::Windows::UI::Xaml::Application::Start((auto &&) { winrt::make<App>(); });
    }


    If it is not possible, how can I properly create a page?










    share|improve this question



























      3












      3








      3








      I might write no sense on the next lines:



      I tried to derive a struct named mainpage from winrt::Windows::UI::Xaml::Controls::Page and pass it to winrt::xaml_typename<>(), in "<>" is mainpage.



      It will be understandable when you look at the code:
      Point number 1 "(1)" is the mainpage.h file, is very short.
      Point number 2 "(2)" is the App.cpp file, it only has the necessary.



      //(1) mainpage.h

      #pragma once

      #include "pch.h"

      struct mainpage : winrt::Windows::UI::Xaml::Controls::Page
      {
      mainpage() {

      winrt::Windows::UI::Xaml::Controls::Button thebutton = winrt::Windows::UI::Xaml::Controls::Button();
      thebutton.Click([&](const IInspectable& sender, const winrt::Windows::UI::Xaml::RoutedEventArgs& event_arg)
      {
      thebutton.Content(winrt::box_value(L"Clicked"));
      });
      }
      };

      //(2) App.cpp

      #include "pch.h"
      #include "mainpage.h"

      struct App : winrt::Windows::UI::Xaml::ApplicationT<App>
      {
      void OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs& event_arg)
      {
      winrt::Windows::UI::Xaml::Controls::Frame root_frame{ nullptr };
      winrt::Windows::UI::Xaml::UIElement content = winrt::Windows::UI::Xaml::Window::Current().Content();
      if (content)
      {
      root_frame = content.try_as<winrt::Windows::UI::Xaml::Controls::Frame>();
      }

      // Do not repeat app initialization when the Window already has content,
      // just ensure that the window is active
      if (root_frame == nullptr)
      {
      // Create a Frame to act as the navigation context and associate it with
      // a SuspensionManager key
      root_frame = winrt::Windows::UI::Xaml::Controls::Frame();

      root_frame.NavigationFailed({ this, &App::OnNavigationFailed });

      if (event_arg.PreviousExecutionState() == winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState::Terminated)
      {
      // Restore the saved session state only when appropriate, scheduling the
      // final launch steps after the restore is complete
      }

      if (event_arg.PrelaunchActivated() == false)
      {
      if (root_frame.Content() == nullptr)
      {
      // When the navigation stack isn't restored navigate to the first page,
      // configuring the new page by passing required information as a navigation
      // parameter
      root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
      }
      // Place the frame in the current Window
      winrt::Windows::UI::Xaml::Window::Current().Content(root_frame);
      // Ensure the current window is active
      winrt::Windows::UI::Xaml::Window::Current().Activate();
      }
      }
      else
      {
      if (event_arg.PrelaunchActivated() == false)
      {
      if (root_frame.Content() == nullptr)
      {
      // When the navigation stack isn't restored navigate to the first page,
      // configuring the new page by passing required information as a navigation
      // parameter
      root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
      }
      // Ensure the current window is active
      winrt::Windows::UI::Xaml::Window::Current().Activate();
      }
      }
      }
      void App::OnNavigationFailed(const IInspectable&, const winrt::Windows::UI::Xaml::Navigation::NavigationFailedEventArgs& event_arg)
      {
      throw winrt::hresult_error(E_FAIL, winrt::hstring(L"Failed to load Page ") + event_arg.SourcePageType().Name);
      }
      };

      int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
      winrt::Windows::UI::Xaml::Application::Start((auto &&) { winrt::make<App>(); });
      }


      If it is not possible, how can I properly create a page?










      share|improve this question
















      I might write no sense on the next lines:



      I tried to derive a struct named mainpage from winrt::Windows::UI::Xaml::Controls::Page and pass it to winrt::xaml_typename<>(), in "<>" is mainpage.



      It will be understandable when you look at the code:
      Point number 1 "(1)" is the mainpage.h file, is very short.
      Point number 2 "(2)" is the App.cpp file, it only has the necessary.



      //(1) mainpage.h

      #pragma once

      #include "pch.h"

      struct mainpage : winrt::Windows::UI::Xaml::Controls::Page
      {
      mainpage() {

      winrt::Windows::UI::Xaml::Controls::Button thebutton = winrt::Windows::UI::Xaml::Controls::Button();
      thebutton.Click([&](const IInspectable& sender, const winrt::Windows::UI::Xaml::RoutedEventArgs& event_arg)
      {
      thebutton.Content(winrt::box_value(L"Clicked"));
      });
      }
      };

      //(2) App.cpp

      #include "pch.h"
      #include "mainpage.h"

      struct App : winrt::Windows::UI::Xaml::ApplicationT<App>
      {
      void OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs& event_arg)
      {
      winrt::Windows::UI::Xaml::Controls::Frame root_frame{ nullptr };
      winrt::Windows::UI::Xaml::UIElement content = winrt::Windows::UI::Xaml::Window::Current().Content();
      if (content)
      {
      root_frame = content.try_as<winrt::Windows::UI::Xaml::Controls::Frame>();
      }

      // Do not repeat app initialization when the Window already has content,
      // just ensure that the window is active
      if (root_frame == nullptr)
      {
      // Create a Frame to act as the navigation context and associate it with
      // a SuspensionManager key
      root_frame = winrt::Windows::UI::Xaml::Controls::Frame();

      root_frame.NavigationFailed({ this, &App::OnNavigationFailed });

      if (event_arg.PreviousExecutionState() == winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState::Terminated)
      {
      // Restore the saved session state only when appropriate, scheduling the
      // final launch steps after the restore is complete
      }

      if (event_arg.PrelaunchActivated() == false)
      {
      if (root_frame.Content() == nullptr)
      {
      // When the navigation stack isn't restored navigate to the first page,
      // configuring the new page by passing required information as a navigation
      // parameter
      root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
      }
      // Place the frame in the current Window
      winrt::Windows::UI::Xaml::Window::Current().Content(root_frame);
      // Ensure the current window is active
      winrt::Windows::UI::Xaml::Window::Current().Activate();
      }
      }
      else
      {
      if (event_arg.PrelaunchActivated() == false)
      {
      if (root_frame.Content() == nullptr)
      {
      // When the navigation stack isn't restored navigate to the first page,
      // configuring the new page by passing required information as a navigation
      // parameter
      root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
      }
      // Ensure the current window is active
      winrt::Windows::UI::Xaml::Window::Current().Activate();
      }
      }
      }
      void App::OnNavigationFailed(const IInspectable&, const winrt::Windows::UI::Xaml::Navigation::NavigationFailedEventArgs& event_arg)
      {
      throw winrt::hresult_error(E_FAIL, winrt::hstring(L"Failed to load Page ") + event_arg.SourcePageType().Name);
      }
      };

      int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
      winrt::Windows::UI::Xaml::Application::Start((auto &&) { winrt::make<App>(); });
      }


      If it is not possible, how can I properly create a page?







      c++-winrt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 7:40







      Ketelhohn

















      asked Dec 29 '18 at 2:31









      KetelhohnKetelhohn

      306




      306
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It is simple if you have installed the CppWinRT VSIX. Then create a "View model" and edit the files. You shouldn't derive the struct from the winrt::Windows::UI::Xaml::Controls::Page class because winrt::xaml_typename<> needs a WinRT class in <>.






          share|improve this answer
























          • Ok!, thank you.

            – Ketelhohn
            Jan 14 at 4:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966212%2fis-it-possible-to-programatically-create-a-xaml-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It is simple if you have installed the CppWinRT VSIX. Then create a "View model" and edit the files. You shouldn't derive the struct from the winrt::Windows::UI::Xaml::Controls::Page class because winrt::xaml_typename<> needs a WinRT class in <>.






          share|improve this answer
























          • Ok!, thank you.

            – Ketelhohn
            Jan 14 at 4:36
















          0














          It is simple if you have installed the CppWinRT VSIX. Then create a "View model" and edit the files. You shouldn't derive the struct from the winrt::Windows::UI::Xaml::Controls::Page class because winrt::xaml_typename<> needs a WinRT class in <>.






          share|improve this answer
























          • Ok!, thank you.

            – Ketelhohn
            Jan 14 at 4:36














          0












          0








          0







          It is simple if you have installed the CppWinRT VSIX. Then create a "View model" and edit the files. You shouldn't derive the struct from the winrt::Windows::UI::Xaml::Controls::Page class because winrt::xaml_typename<> needs a WinRT class in <>.






          share|improve this answer













          It is simple if you have installed the CppWinRT VSIX. Then create a "View model" and edit the files. You shouldn't derive the struct from the winrt::Windows::UI::Xaml::Controls::Page class because winrt::xaml_typename<> needs a WinRT class in <>.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 10 at 3:34









          BerrysoftBerrysoft

          813




          813













          • Ok!, thank you.

            – Ketelhohn
            Jan 14 at 4:36



















          • Ok!, thank you.

            – Ketelhohn
            Jan 14 at 4:36

















          Ok!, thank you.

          – Ketelhohn
          Jan 14 at 4:36





          Ok!, thank you.

          – Ketelhohn
          Jan 14 at 4:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966212%2fis-it-possible-to-programatically-create-a-xaml-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas