简体   繁体   English

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

[英]Can't set XAML text to string value in C++

so for context I'm relatively new to creating C++ code with GUI.所以就上下文而言,我对使用 GUI 创建 C++ 代码比较陌生。 I read that I needed to create a Universal Windows Project and went ahead.我读到我需要创建一个通用 Windows 项目并继续进行。 I programmed all the XAML and moved on to making functionality but ran into an issue when I tried to set the value of a std::string variable to a XAML TextBlock.我对所有 XAML 进行了编程并继续制作功能,但是当我尝试将 std::string 变量的值设置为 XAML TextBlock 时遇到了问题。

Here's the error:这是错误:

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 ^

Here's the code for the button/place where the error is occurring (MainPage.xaml.cpp):这是发生错误的按钮/位置的代码 (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

}

Finally I have the XAML (MainPage.xaml):最后我有 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!

When using C++, the member returns a method , not a property:使用 C++ 时,该成员返回一个方法,而不是一个属性:

PrimaryX->Text("your text");

Also, you're passing a vector, that is unlikely to work.此外,您正在传递一个不太可能起作用的向量。

  • get the element you want from the vector从向量中获取你想要的元素
  • convert that element to to a String^将该元素转换为String^
  • pass that String^ instance to Text将该String^实例传递给Text

Tip: it might be easier for you to use IVector<T> instead of std::string .提示:使用IVector<T>代替std::string可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM