C ++ / CX WPF,访问代码

时间:2017-03-14 15:44:07

标签: wpf visual-studio xaml c++-cx

  

解决方案:

     

添加

#include "MenuEntryControl.xaml.h"
     在MenuControl.cpp中

     

谢谢安迪!

我在2017年Visual Studio社区的C ++ / CX中编写了一个程序。 我在XAML中构建我的界面。

所以,我做了一个自定义用户控件(它是一个自定义按钮)

MenuEntryControl.xaml

这是MenuEntryControl的xaml:

<UserControl
x:Class="XXXXXX.MenuEntryControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XXXXXX"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="54"
d:DesignWidth="276">

<Grid x:Name="container">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="39*"/>
        <ColumnDefinition Width="63*"/>
        <ColumnDefinition Width="143*"/>
        <ColumnDefinition Width="31*"/>
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="TitleInput" Grid.Column="2" TextAlignment="Center" Margin="0,21,0,15" TextWrapping="Wrap" Text="I N P U T" Foreground="White" FontFamily="Open Sans Light"/>
    <Image x:Name="image" Source="ms-appx:///Assets/settings.png" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="29" Margin="17,14,0,0" Grid.Column="1"/>
    <Button x:Name="ClickableZone" Grid.ColumnSpan="4" Height="54" Width="276" Click="ClickableZone_Click" BorderThickness="0" Background="#00000000"/>
</Grid> 
</UserControl>

MenuEntryControl.xaml.h:

[Windows::Foundation::Metadata::WebHostHidden]
public ref class MenuEntryControl sealed
{
    public:
        MenuEntryControl();
        void SetTitle(Platform::String ^title);
        void SetFocus(bool focused);

        //Titre affiché sur le bouton (Set attribut s_title)

        property Platform::String ^ Title
        {
            Platform::String ^ get() { return s_title; }
            void set(Platform::String ^ value)
            {
                s_title = value;
                Refresh();
            }
        }

        property Windows::UI::Xaml::Media::ImageSource ^ Icon
        {
            Windows::UI::Xaml::Media::ImageSource^ get() { return is_icon; }
            void set(Windows::UI::Xaml::Media::ImageSource ^ value)
            {
                is_icon = value;
                Refresh();
            }
        }


    private :
        Platform::String ^ s_title; //Titre du bouton (Attribut)
        bool b_isFocused;
        Windows::UI::Xaml::Media::ImageSource ^ is_icon;    //Icone affichée sur le bouton (Attribut)
        void ClickableZone_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

        //Methode de rafraichissement de l'UI
        void Refresh();


};

重建后,我在MenuControl.xaml(Main和Configure)中添加了这个控件

MenuControl.xaml

这是MenuControl.xaml的xaml代码

<UserControl
x:Class="XXXXXX.MenuControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:XXXXXX"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="720"
d:DesignWidth="276">

<Grid Background="#FF4B4B4B">
    <Border BorderBrush="#66FFFFFF" BorderThickness="0,0.4,0,0" HorizontalAlignment="Left" Height="10" Margin="0,83,0,0" VerticalAlignment="Top" Width="276"/>
    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextAlignment="Center" Margin="0,30,0,0" TextWrapping="Wrap" Text="X X X X X X" VerticalAlignment="Top" Foreground="White" FontFamily="Open Sans Light" FontSize="24" Width="276"/>
    <local:MenuEntryControl x:Name="Main_button" HorizontalAlignment="Left" Height="55" Margin="0,105,0,0" VerticalAlignment="Top" Width="276" Icon="ms-appx:///Assets/home.png" Title="M A I N"/>
    <local:MenuEntryControl HorizontalAlignment="Left" Height="58" Margin="0,160,0,0" VerticalAlignment="Top" Width="276" Title="C O N F I G U R E" Icon="ms-appx:///Assets/settings.png"/>

</Grid>   </UserControl>

所以,现在问题;)

在MenuControl.cpp中,我无法使用x:name&#34; Main_button&#34;来访问我的用户控件的方法。

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 User Control item template is documented at       https://go.microsoft.com/fwlink/?LinkId=234236

MenuControl::MenuControl()
{
    InitializeComponent();
}

void XXXXXX::MenuControl::Configure()
{
    Main_button->   
}

视觉说在这一行:

Main_button->

&#34;不允许指向不完整的课程&#34;

老实说,我被阻止了。你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在C ++中,您需要为要使用的任何类包含头文件。要修复此编译错误,请包含定义MenuEntryControl的头文件。在MenuControl.cpp顶部或附近添加此内容:

#include "MenuEntryControl.xaml.h"