This source file includes following definitions.
- MainPage_SizeChanged
- InvalidateSize
- InvalidateViewState
- PopulateScenarios
- LoadScenario
- Scenarios_SelectionChanged
- NotifyUser
- Footer_Click
- LoadState
- SaveState
#include "pch.h"
#include "MainPage.xaml.h"
#include "App.xaml.h"
#include <collection.h>
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Platform;
using namespace SDKSample;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::Graphics::Display;
using namespace Windows::UI::ViewManagement;
MainPage^ MainPage::Current = nullptr;
MainPage::MainPage()
{
InitializeComponent();
HiddenFrame = ref new Windows::UI::Xaml::Controls::Frame();
HiddenFrame->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
ContentRoot->Children->Append(HiddenFrame);
FeatureName->Text = FEATURE_NAME;
this->SizeChanged += ref new SizeChangedEventHandler(this, &MainPage::MainPage_SizeChanged);
Scenarios->SelectionChanged += ref new SelectionChangedEventHandler(this, &MainPage::Scenarios_SelectionChanged);
MainPage::Current = this;
autoSizeInputSectionWhenSnapped = true;
}
void MainPage::MainPage_SizeChanged(Object^ sender, SizeChangedEventArgs^ e)
{
InvalidateSize();
MainPageSizeChangedEventArgs^ args = ref new MainPageSizeChangedEventArgs();
args->ViewState = ApplicationView::Value;
MainPageResized(this, args);
}
void MainPage::InvalidateSize()
{
double windowWidth = this->ActualWidth;
if (windowWidth != 0.0)
{
double listBoxWidth = Scenarios->ActualWidth;
double listBoxMarginLeft = Scenarios->Margin.Left;
double listBoxMarginRight = Scenarios->Margin.Right;
double availableWidth = windowWidth - listBoxWidth;
double layoutRootMarginLeft = ContentRoot->Margin.Left;
double layoutRootMarginRight = ContentRoot->Margin.Right;
if (ApplicationView::Value != ApplicationViewState::Snapped)
{
InputSection->Width = ((availableWidth) -
(layoutRootMarginLeft + layoutRootMarginRight + listBoxMarginLeft + listBoxMarginRight));
}
else
{
if (autoSizeInputSectionWhenSnapped)
{
InputSection->Width = (windowWidth - (layoutRootMarginLeft + layoutRootMarginRight));
}
}
}
InvalidateViewState();
}
void MainPage::InvalidateViewState()
{
if (ApplicationView::Value == ApplicationViewState::Snapped)
{
Grid::SetRow(DescriptionText, 3);
Grid::SetColumn(DescriptionText, 0);
Grid::SetRow(InputSection, 4);
Grid::SetColumn(InputSection, 0);
Grid::SetRow(FooterPanel, 2);
Grid::SetColumn(FooterPanel, 0);
}
else
{
Grid::SetRow(DescriptionText, 1);
Grid::SetColumn(DescriptionText, 1);
Grid::SetRow(InputSection, 2);
Grid::SetColumn(InputSection, 1);
Grid::SetRow(FooterPanel, 1);
Grid::SetColumn(FooterPanel, 1);
}
VisualStateManager::GoToState(InputSection, "Input" + LayoutAwarePage::DetermineVisualState(ApplicationView::Value), false);
VisualStateManager::GoToState(OutputSection, "Output" + LayoutAwarePage::DetermineVisualState(ApplicationView::Value), false);
}
void MainPage::PopulateScenarios()
{
ScenarioList = ref new Platform::Collections::Vector<Object^>();
for (unsigned int i = 0; i < scenarios->Length; ++i)
{
Scenario s = scenarios[i];
ListBoxItem^ item = ref new ListBoxItem();
item->Name = s.ClassName;
item->Content = (i + 1).ToString() + ") " + s.Title;
ScenarioList->Append(item);
}
Scenarios->ItemsSource = ScenarioList;
Scenarios->ScrollIntoView(Scenarios->SelectedItem);
}
void MainPage::LoadScenario(String^ scenarioName)
{
autoSizeInputSectionWhenSnapped = true;
TypeName scenarioType = {scenarioName, TypeKind::Custom};
HiddenFrame->Navigate(scenarioType, this);
Page^ hiddenPage = safe_cast<Page^>(HiddenFrame->Content);
UIElement^ input = safe_cast<UIElement^>(hiddenPage->FindName("Input"));
UIElement^ output = safe_cast<UIElement^>(hiddenPage->FindName("Output"));
if (input == nullptr)
{
NotifyUser("Cannot load scenario input section for " + scenarioName +
" Make sure root of input section markup has x:Name of 'Input'", NotifyType::ErrorMessage);
return;
}
if (output == nullptr)
{
NotifyUser("Cannot load scenario output section for " + scenarioName +
" Make sure root of output section markup has x:Name of 'Output'", NotifyType::ErrorMessage);
return;
}
Panel^ panel = safe_cast<Panel^>(hiddenPage->FindName("LayoutRoot"));
if (panel != nullptr)
{
unsigned int index = 0;
UIElementCollection^ collection = panel->Children;
collection->IndexOf(input, &index);
collection->RemoveAt(index);
collection->IndexOf(output, &index);
collection->RemoveAt(index);
InputSection->Content = input;
OutputSection->Content = output;
ScenarioLoaded(this, nullptr);
}
else
{
NotifyUser("Cannot load scenario: " + scenarioName + ". Make sure root tag in the '" +
scenarioName + "' file has an x:Name of 'LayoutRoot'", NotifyType::ErrorMessage);
}
}
void MainPage::Scenarios_SelectionChanged(Object^ sender, SelectionChangedEventArgs^ e)
{
if (Scenarios->SelectedItem != nullptr)
{
NotifyUser("", NotifyType::StatusMessage);
LoadScenario((safe_cast<ListBoxItem^>(Scenarios->SelectedItem))->Name);
InvalidateSize();
}
}
void MainPage::NotifyUser(String^ strMessage, NotifyType type)
{
switch (type)
{
case NotifyType::StatusMessage:
StatusBlock->Style = safe_cast<Windows::UI::Xaml::Style^>(this->Resources->Lookup("StatusStyle"));
break;
case NotifyType::ErrorMessage:
StatusBlock->Style = safe_cast<Windows::UI::Xaml::Style^>(this->Resources->Lookup("ErrorStyle"));
break;
default:
break;
}
StatusBlock->Text = strMessage;
if (StatusBlock->Text != "")
{
StatusBlock->Visibility = Windows::UI::Xaml::Visibility::Visible;
}
else
{
StatusBlock->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}
}
void MainPage::Footer_Click(Object^ sender, RoutedEventArgs^ e)
{
auto uri = ref new Uri((String^)((HyperlinkButton^)sender)->Tag);
Windows::System::Launcher::LaunchUriAsync(uri);
}
void MainPage::LoadState(Object^ navigationParameter, IMap<String^, Object^>^ pageState)
{
(void) navigationParameter;
PopulateScenarios();
ListBoxItem^ startingScenario = nullptr;
int startingScenarioIndex = -1;
if (pageState != nullptr && pageState->HasKey("SelectedScenarioIndex"))
{
startingScenarioIndex = safe_cast<int>(pageState->Lookup("SelectedScenarioIndex"));
}
Scenarios->SelectedIndex = startingScenarioIndex != -1 ? startingScenarioIndex : 0;
InvalidateViewState();
}
void MainPage::SaveState(IMap<String^, Object^>^ pageState)
{
int selectedListBoxItemIndex = Scenarios->SelectedIndex;
pageState->Insert("SelectedScenarioIndex", selectedListBoxItemIndex);
}