-1

我正在尝试在 Windows 8 应用程序中开始使用 Win2D。我只是试图将库安装到我的新项目并使用其中一个函数画一个圆圈,但由于我不明白的错误而无法继续。

下面显示的是在 Mainpage.xaml.cpp 文件中自动创建的事件处理程序。我的错误是函数内部单行代码开头的'args'部分带有红色下划线。将鼠标悬停在“args”下的红线上会显示消息“表达式必须具有类类型”。有人能帮忙吗?

void AdditiveSignals::MainPage::CanvasAnimatedControl_Draw(Microsoft::Graphics::Canvas::UI::Xaml::ICanvasAnimatedControl^ sender, Microsoft::Graphics::Canvas::UI::Xaml::CanvasAnimatedDrawEventArgs^ args)
{
    args.DrawingSession.DrawCircle(640, 360, 30.0f, Colors->Blue);
}

如果需要,这是完整的代码

在 MainPage.xaml.h

//
// MainPage.xaml.h
// Declaration of the MainPage class.
//

#pragma once

#include "MainPage.g.h"

namespace AdditiveSignals
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public ref class MainPage sealed
    {
    public:
        MainPage();

    protected:
        virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
    private:
        void CanvasAnimatedControl_Draw(Microsoft::Graphics::Canvas::UI::Xaml::ICanvasAnimatedControl^ sender, Microsoft::Graphics::Canvas::UI::Xaml::CanvasAnimatedDrawEventArgs^ args);
    };
}

在 MainPage.xaml

<Page
    x:Class="AdditiveSignals.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AdditiveSignals"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <canvas:CanvasAnimatedControl Draw="CanvasAnimatedControl_Draw" />
    </Grid>
</Page>

在 MainPage.xaml.cpp(完整代码)

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

#include "pch.h"
#include "MainPage.xaml.h"

using namespace AdditiveSignals;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI;
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 http://go.microsoft.com/fwlink/?LinkId=234238

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

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
    (void) e;   // Unused parameter

    // TODO: Prepare page for display here.

    // TODO: If your application contains multiple pages, ensure that you are
    // handling the hardware Back button by registering for the
    // Windows::Phone::UI::Input::HardwareButtons.BackPressed event.
    // If you are using the NavigationHelper provided by some templates,
    // this event is handled for you.
}

//This event handler will be called 60 times per second
void AdditiveSignals::MainPage::CanvasAnimatedControl_Draw(Microsoft::Graphics::Canvas::UI::Xaml::ICanvasAnimatedControl^ sender, Microsoft::Graphics::Canvas::UI::Xaml::CanvasAnimatedDrawEventArgs^ args)
{


    args.DrawingSession.DrawCircle(640, 360, 30.0f, Colors->Blue);
}
4

1 回答 1

2

关于语言的标准说明:C++/CLI 旨在作为允许 C# 或其他 .Net 代码调用 C++ 代码的桥梁。它不打算作为主要的开发语言。如果您想学习 C++,我会使用带有 MFC 的 C++ 作为您的 GUI 工具包。如果您想学习托管代码,我会使用 WinForms 或 WPF 学习 C#。

那说:

void CanvasAnimatedControl_Draw(ICanvasAnimatedControl^ sender, CanvasAnimatedDrawEventArgs^ args)
{
    args.DrawingSession.DrawCircle(640, 360, 30.0f, Colors->Blue);
}

args是引用类型,而不是值类型。(它是通过引用 ( ^) 传递给您的,这大致类似于通过指针 ( *) 传递 C++ 对象。)由于它是引用类型,因此它的成员是使用->而不是访问.的,就像指向对象的 C++ 指针一样。

DrawingSession也是一个引用类型,所以它的成员也可以使用->.

Colors是一个类的名称;静态成员使用::.

args->DrawingSession->DrawCircle(640, 360, 30.0f, Colors::Blue);
于 2017-01-25T20:28:45.090 回答