如何在 C++ Visual Studio 中从 Xaml 中动态删除按钮?
How to dynamically remove a button from Xaml in C++ Visual Studio?
这就是我在 C++ (Visual Studio C++ 2017) 中动态创建按钮的方式:
Button^ myButton = ref new Button();
myButton->Content = "Button";
myButton->Height = 80;
myButton->Width = 150;
ContentPanel->Children->Append(myButton);
如何从 ContentPanel 中动态删除此按钮?
ContentPanel->Children-> RemoveAt(myButton);
不起作用并产生错误。我在这里做错了什么?
完整示例:
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel x:Name="ContentPanel">
<TextBlock HorizontalAlignment="Left" Text="Hello!" />
</StackPanel>
<Button x:Name="InputButton" Content="Click" Click="InputButton_Click"/>
</Grid>
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace App3;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
MainPage::MainPage()
{
InitializeComponent();
}
void App3::MainPage::InputButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Button^ myButton = ref new Button();
if (myButton != nullptr)
{
myButton->Margin = Thickness(10);
myButton->Content = "myButton";
myButton->Height = 80;
myButton->Width = 150;
myButton->Foreground = ref new SolidColorBrush (Windows::UI::Colors::Lavender);
myButton->Background = ref new SolidColorBrush(Windows::UI::Colors::Olive);
ContentPanel->Children->Append(myButton);
}
else
ContentPanel->Children->RemoveAt(myButton);
}
此外,我猜想 (myButton != nullptr) 不是测试 'myButton' 是否已经动态创建的正确方法(在 'InputButton_Click' 的先前调用中)
RemoveAt
采用要删除的项目的索引。您应该在添加新项目时将其保存在某个地方,稍后使用删除它。
m_button_index = ContentPanel->Children->Size;
ContentPanel->Children->Append(myButton);
}
else
ContentPanel->Children->RemoveAt(m_button_index);
另请注意,检查 myButton != nullptr
毫无意义,因为分配的按钮不为空或引发异常,因此您的代码将始终添加按钮。
这就是我在 C++ (Visual Studio C++ 2017) 中动态创建按钮的方式:
Button^ myButton = ref new Button();
myButton->Content = "Button";
myButton->Height = 80;
myButton->Width = 150;
ContentPanel->Children->Append(myButton);
如何从 ContentPanel 中动态删除此按钮?
ContentPanel->Children-> RemoveAt(myButton);
不起作用并产生错误。我在这里做错了什么?
完整示例:
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel x:Name="ContentPanel">
<TextBlock HorizontalAlignment="Left" Text="Hello!" />
</StackPanel>
<Button x:Name="InputButton" Content="Click" Click="InputButton_Click"/>
</Grid>
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace App3;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
MainPage::MainPage()
{
InitializeComponent();
}
void App3::MainPage::InputButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Button^ myButton = ref new Button();
if (myButton != nullptr)
{
myButton->Margin = Thickness(10);
myButton->Content = "myButton";
myButton->Height = 80;
myButton->Width = 150;
myButton->Foreground = ref new SolidColorBrush (Windows::UI::Colors::Lavender);
myButton->Background = ref new SolidColorBrush(Windows::UI::Colors::Olive);
ContentPanel->Children->Append(myButton);
}
else
ContentPanel->Children->RemoveAt(myButton);
}
此外,我猜想 (myButton != nullptr) 不是测试 'myButton' 是否已经动态创建的正确方法(在 'InputButton_Click' 的先前调用中)
RemoveAt
采用要删除的项目的索引。您应该在添加新项目时将其保存在某个地方,稍后使用删除它。
m_button_index = ContentPanel->Children->Size;
ContentPanel->Children->Append(myButton);
}
else
ContentPanel->Children->RemoveAt(m_button_index);
另请注意,检查 myButton != nullptr
毫无意义,因为分配的按钮不为空或引发异常,因此您的代码将始终添加按钮。