You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中无法将std::string赋值给XAML TextBlock的Text属性

UWP C++开发问题:std::string赋值给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;

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

}

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>

解决方法

1. 类型不匹配问题

UWP的TextBlock.Text属性要求**Platform::String^类型**,标准C++的std::string无法直接赋值,需要转换为UWP兼容的字符串类型:

PrimaryX->Text = Platform::String::CreateFromUTF8(primaries[rand1].c_str());

其他TextBlock的赋值也需要做同样转换:

SecondaryX->Text = Platform::String::CreateFromUTF8(secondaries[rand2].c_str());
MeleeX->Text = Platform::String::CreateFromUTF8(melee[rand3].c_str());
OtherX->Text = Platform::String::CreateFromUTF8(other[rand4].c_str());

2. 数组越界问题

随机索引计算错误:rand() % (size + 1)会生成超出vector有效范围的索引(vector有效索引是0到size-1),修正为:

int rand1 = rand() % primaries.size();
int rand2 = rand() % secondaries.size();
int rand3 = rand() % melee.size();
int rand4 = rand() % other.size();

完整修改后的Roll_Click函数

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();
    int rand2 = rand() % secondaries.size();
    int rand3 = rand() % melee.size();
    int rand4 = rand() % other.size();
    
    PrimaryX->Text = Platform::String::CreateFromUTF8(primaries[rand1].c_str());
    SecondaryX->Text = Platform::String::CreateFromUTF8(secondaries[rand2].c_str());
    MeleeX->Text = Platform::String::CreateFromUTF8(melee[rand3].c_str());
    OtherX->Text = Platform::String::CreateFromUTF8(other[rand4].c_str());
}

内容的提问来源于stack exchange,提问作者StrangeJmaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:25:17