diff --git a/src/Calculator/Views/HistoryList.xaml b/src/Calculator/Views/HistoryList.xaml index 60e2eb1d..1132c3ec 100644 --- a/src/Calculator/Views/HistoryList.xaml +++ b/src/Calculator/Views/HistoryList.xaml @@ -51,7 +51,7 @@ Invoked="OnDeleteSwipeInvoked"/> - + @@ -59,9 +59,7 @@ - + + diff --git a/src/Calculator/Views/HistoryList.xaml.cpp b/src/Calculator/Views/HistoryList.xaml.cpp index de4a96c4..70c3e18a 100644 --- a/src/Calculator/Views/HistoryList.xaml.cpp +++ b/src/Calculator/Views/HistoryList.xaml.cpp @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // @@ -67,9 +67,13 @@ void HistoryList::ListView_ItemClick(_In_ Object^ sender, _In_ ItemClickEventArg void HistoryList::OnDeleteMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e) { - auto clickedItem = safe_cast(safe_cast(sender)->DataContext); + auto listViewItem = HistoryContextMenu->Target; + auto itemViewModel = safe_cast(HistoryListView->ItemFromContainer(listViewItem)); - Model->DeleteItem(clickedItem); + if (itemViewModel != nullptr) + { + Model->DeleteItem(itemViewModel); + } } void HistoryList::OnDeleteSwipeInvoked(_In_ MUXC::SwipeItem^ sender, _In_ MUXC::SwipeItemInvokedEventArgs^ e) diff --git a/src/Calculator/Views/Memory.xaml b/src/Calculator/Views/Memory.xaml index 50db4050..7beeb43a 100644 --- a/src/Calculator/Views/Memory.xaml +++ b/src/Calculator/Views/Memory.xaml @@ -19,7 +19,7 @@ - + @@ -44,6 +44,7 @@ BasedOn="{StaticResource HistoryMemoryItemContainerStyle}" TargetType="ListViewItem"> + @@ -107,8 +108,6 @@ (Resources->Lookup("MemoryContextMenu")); MemoryPaneEmpty->FlowDirection = LocalizationService::GetInstance()->GetFlowDirection(); } @@ -56,53 +55,31 @@ void Memory::MemoryListItemClick(_In_ Object^ sender, _In_ ItemClickEventArgs^ e } } -void Memory::OnContextRequested(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::Input::ContextRequestedEventArgs^ e) -{ - // Walk up the tree to find the ListViewItem. - // There may not be one if the click wasn't on an item. - auto requestedElement = safe_cast(e->OriginalSource); - while ((requestedElement != sender) && !dynamic_cast(requestedElement)) - { - requestedElement = safe_cast(VisualTreeHelper::GetParent(requestedElement)); - } - - if (requestedElement != sender) - { - // The context menu request was for a ListViewItem. - auto memorySlot = safe_cast(MemoryListView->ItemFromContainer(requestedElement)); - Point point; - if (e->TryGetPosition(requestedElement, &point)) - { - m_memoryItemFlyout->ShowAt(requestedElement, point); - } - else - { - // Not invoked via pointer, so let XAML choose a default location. - m_memoryItemFlyout->ShowAt(requestedElement); - } - - e->Handled = true; - } -} - -void Memory::OnContextCanceled(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) -{ - m_memoryItemFlyout->Hide(); -} - void Memory::OnClearMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e) { - GetMemoryItemForCurrentFlyout()->Clear(); + auto memoryItem = GetMemoryItemForCurrentFlyout(); + if (memoryItem != nullptr) + { + memoryItem->Clear(); + } } void Memory::OnMemoryAddMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e) { - GetMemoryItemForCurrentFlyout()->MemoryAdd(); + auto memoryItem = GetMemoryItemForCurrentFlyout(); + if (memoryItem != nullptr) + { + memoryItem->MemoryAdd(); + } } void Memory::OnMemorySubtractMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e) { - GetMemoryItemForCurrentFlyout()->MemorySubtract(); + auto memoryItem = GetMemoryItemForCurrentFlyout(); + if (memoryItem != nullptr) + { + memoryItem->MemorySubtract(); + } } bool Memory::IsErrorVisualState::get() @@ -135,7 +112,6 @@ void Memory::MemoryList_Unloaded(_In_ Object^ sender, _In_ RoutedEventArgs^ e) MemoryItemViewModel^ Memory::GetMemoryItemForCurrentFlyout() { - auto listViewItem = m_memoryItemFlyout->Target; - + auto listViewItem = MemoryContextMenu->Target; return safe_cast(MemoryListView->ItemFromContainer(listViewItem)); } diff --git a/src/Calculator/Views/Memory.xaml.h b/src/Calculator/Views/Memory.xaml.h index 7a3e2da5..ee99b958 100644 --- a/src/Calculator/Views/Memory.xaml.h +++ b/src/Calculator/Views/Memory.xaml.h @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // @@ -37,14 +37,11 @@ namespace CalculatorApp } private: - Windows::UI::Xaml::Controls::MenuFlyout^ m_memoryItemFlyout; Windows::Foundation::Rect m_visibleBounds; Windows::Foundation::Rect m_coreBounds; bool m_isErrorVisualState; void MemoryListItemClick(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::Controls::ItemClickEventArgs^ e); - void OnContextRequested(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::Input::ContextRequestedEventArgs^ e); - void OnContextCanceled(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void OnClearMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e); void OnMemoryAddMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e); void OnMemorySubtractMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e);