无法在C++中将XAML文本设置为字符串值

wn9m85ua  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(117)

因此,对于上下文,我对使用GUI创建C++代码相对较新。我了解到需要创建一个通用Windows项目,并继续进行。我编写了所有XAML,并继续创建功能,但在尝试将std::string变量的值设置为XAML TextBlock时遇到了问题。
下面是错误:

Error (active)  E1767   function "Windows::UI::Xaml::Controls::TextBlock::Text::set" cannot 
be called with the given argument list App1 C:\Users\stran\source\repos\App1\App1\MainPage.xaml.cpp line 56 argument types are:
(std::vector<std::string, std::allocator<std::string>>)
            object type is: Windows::UI::Xaml::Controls::TextBlock ^

下面是发生错误的按钮/位置的代码(MainPage.xaml.cpp):

//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

#include "pch.h"
#include "MainPage.xaml.h"
#include <fstream>
#include <string>
#include <vector>
using namespace App1;

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;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

MainPage::MainPage()
{
    InitializeComponent();
}

void App1::MainPage::Roll_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){
    std::vector<std::string> primaries{ "SPAS 12", "ITHACA 37", "FAMAS", "AWP", "M16", "AK47", "DB-SHOTGUN", "THOMPSON SMG", "FLAMETHROWER", "DRAGUNOV", "SHIELD", "SNAKESNIPER", "FISH16", "FAMOS", "SR-25" };
    std::vector<std::string> secondaries{ "MP5K", "AKIMBO UZIS", "GLOCK 17", "MAGNUM", "DEAGLE", "MAGNOM", "FUEL TANK", "PM9 EVIL GUN" };
    std::vector<std::string> melee{ "AXE", "BAT", "FORK", "KNIFE", "SHOVEL", "KATANA", "ATTACK FISH" };
    std::vector<std::string> other{ "GRENADES", "CUFFS", "MINE", "MEDKIT", "GASOLINE", "SECRET SERVICE PIGEON", "CRABNADE", "MOLOTOV", "void" };
    
    if (NoneChance->IsChecked == true) {
        primaries.push_back("NONE");
        secondaries.push_back("NONE");
        melee.push_back("NONE");
        other.push_back("NONE");
    }
    srand((int)time(0));

    int rand1 = rand() % (primaries.size() + 1);
    int rand2 = rand() % (secondaries.size() + 1);
    int rand3 = rand() % (melee.size() + 1);
    int rand4 = rand() % (other.size() + 1);
    PrimaryX->Text = primaries[rand1]; //Error on this line

}

最后我有了XAML(MainPage.xaml):

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <StackPanel x:Name="MainStack" Orientation="Horizontal">
            <Image x:Name="TitleSplash" Width="550" Height="550" Source="/Assets/glowing.png" VerticalAlignment="Top" HorizontalAlignment="Center"/>
            <StackPanel x:Name="Controls" Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Left">
                <StackPanel x:Name="Overview" Orientation="Horizontal" VerticalAlignment="Center">
                    <TextBlock x:Name="text1" Margin="15,15" Text="Primary" />
                    <TextBlock x:Name="text2" Margin="15,15" Text="Secondary" />
                    <TextBlock x:Name="text3" Margin="15,15" Text="Melee" />
                    <TextBlock x:Name="text4" Margin="15,15" Text="Other" />
                </StackPanel>
                <StackPanel x:Name="Labels" Orientation="Horizontal" VerticalAlignment="Bottom">
                    <TextBlock x:Name="PrimaryX" Margin="15,15" Text="-" />
                    <TextBlock x:Name="SecondaryX" Margin="15,15" Text="-" />
                    <TextBlock x:Name="MeleeX" Margin="15,15" Text="-" />
                    <TextBlock x:Name="OtherX" Margin="15,15" Text="-" />
                </StackPanel>
                <StackPanel x:Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button x:Name="Roll" Margin="15,15" Content="Roll" IsEnabled="True" Click="Roll_Click"/>
                    <CheckBox x:Name="NoneChance" Margin="5,15" IsEnabled="True" Content="Use None" IsChecked="False" />
                    <TextBlock x:Name="ErrorLogs" Margin="20" Text=""/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </Grid>
</Page>

Any help would be greatly appreciated, please just comment if you need any more information!
xpcnnkqh

xpcnnkqh1#

使用C++时,成员returns a method,而不是属性:
PrimaryX->Text("your text");
而且,你传递了一个向量,这不太可能起作用。

  • 从向量中得到你想要的元素
  • 将该元素转换为String^
  • 将该String^示例传递给Text
    **提示:**使用IVector可能比使用std::string更容易。

相关问题