我正在从使用Blank模板开始的Visual Studio 2017示例通用Windows程序 . 我的目标是编写一个简单的UWP应用程序,它将显示一个模拟时钟面,显示当前时间,更新时间和指针移动 . 一旦我在台式机上使用x86,我就会将构建更改为ARM并部署到运行Windows 10 IoT的Raspberry Pi 3 Model B.
我发现这篇博文,Using the Composition API in UWP apps,但它似乎是使用C#而不是C . 它有以下说明,表明有一种方法使用一种组合界面:
在本文中,我们将探索Windows.UI.Composition API . Composition API是位于Windows 10 XAML框架和DirectX之间的可视层 . 它使通用Windows平台应用程序可以轻松访问较低级别的Windows绘图堆栈 . API专注于绘制矩形和图像 - 有或没有XAML表面 - 并对这些应用动画和效果 .
我也发现了这篇博客文章Introduction to Composition,但它似乎也是C#而不是C . 我找到了这篇文章,Interop between XAML and the Visual Layer .
我对这些文章的问题是C#和C之间的各种XAML对象和类不一样,而且我对此不熟悉这一事实并没有帮助 .
这篇文章Using the Visual Layer with XAML似乎有一些C#源代码的C版本,但我不确定这是否是我实际需要的 .
想法是使用Ellipse()函数绘制一个圆,然后为手绘制两条线,一条(时针)更短更厚,第二条(在时针之后绘制的分针使其打开)顶部)比第一个更长更薄,以便在双手重叠时能够看到它 .
在我的MainPage.xaml文件中,我有以下测试代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="60,4,1348,712" RenderTransformOrigin="0.5,0.5">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="300" Margin="61,27,0,0" VerticalAlignment="Top" Width="424"/>
</Grid>
在MainPage.xaml.cpp中,我有以下代码 . 此代码在画布上绘制一个椭圆,然后绘制一条线,然后启动一个周期性计时器,每2秒钟更改一次椭圆的颜色 .
MainPage::MainPage()
{
InitializeComponent();
// See https://xamlbrewer.wordpress.com/2016/01/04/using-the-composition-api-in-uwp-apps/
// m_root = MyCanvas->GetVisual();
// m_compositor = m_root->Compositor;
m_BrushList[0] = ref new SolidColorBrush(Windows::UI::Colors::Red);
m_BrushList[1] = ref new SolidColorBrush(Windows::UI::Colors::Purple);
m_BrushList[2] = ref new SolidColorBrush(Windows::UI::Colors::Blue);
m_BrushList[3] = ref new SolidColorBrush(Windows::UI::Colors::Green);
m_BrushList[4] = ref new SolidColorBrush(Windows::UI::Colors::Yellow);
m_BrushList[5] = ref new SolidColorBrush(Windows::UI::Colors::Orange);
m_icount = 0;
m_r = ref new Windows::UI::Xaml::Shapes::Ellipse();
m_r->Width = 200;
m_r->Height = 200;
m_r->Stroke = m_BrushList[m_icount];
// r->Fill = ref new SolidColorBrush(Windows::UI::Colors::Blue);
m_r->StrokeThickness = 4;
m_r->Margin = Thickness(20, 20, 0, 0);
MyCanvas->Children->Append(m_r);
m_line1 = ref new Windows::UI::Xaml::Shapes::Line();
m_line1->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Red);
m_line1->StrokeThickness = 6;
m_line1->Y1 = 30;
m_line1->X1 = 100;
m_line1->X2 = 400;
MyCanvas->Children->Append(m_line1);
StartTimerAndRegisterHandler();
}
void App2_ArmTest::MainPage::StartTimerAndRegisterHandler()
{
// create our time task so that we can change the clock periodically.
auto timer = ref new Windows::UI::Xaml::DispatcherTimer();
TimeSpan ts;
// right now we are using a 2 second timer as part of prototyping this out.
// this allows us to check that the timer is in fact working.
// this needs to be changed from every two seconds to every minute once we
// have the hands of the clock displaying properly.
ts.Duration = 2 * 10000000; // 10,000,000 ticks per second as value units is 100 nanoseconds
timer->Interval = ts;
timer->Start();
auto registrationtoken = timer->Tick += ref new EventHandler<Object^>(this, &MainPage::OnTick);
}
void App2_ArmTest::MainPage::OnTick(Object^ sender, Object^ e)
{
// change the color of our clock.
m_icount = (m_icount + 1) % 6;
m_r->Stroke = m_BrushList[m_icount];
// get the current local time which will be used for positioning the
// clock hands once we have that figured out.
std::time_t result = std::time(nullptr);
std::tm localTime;
localtime_s (&localTime, &result);
}
几秒钟后,我当前显示的窗口看起来像这个图像:
如何在椭圆形顶部绘制线条,这样的方式使得每次定时器触发功能 App2_ArmTest::MainPage::OnTick()
时,我可以将时钟指针或旋转到钟面上的正确位置?
1 回答
经过一些工作和深入而肮脏的深入研究各种不足的Microsoft文档,这些文档集中在C#上,我有一个工作初始应用程序,它显示一个模拟时钟表面,两只手显示小时和分钟并自行更新 .
另请参阅本文末尾,以获取有关将控件添加到
Canvas
并在其中显示YouTube视频的简要概述 . 见下面的附录I.我花了很多时间阅读,然后使用Visual Studio 2017 IDE来探索各种组件 . 在某些情况下,C#源代码使用与C不同的对象(例如,C#使用
Vector2
和Vector3
类,而C使用float2
和float3
来自Windowsnumerics.h
) . 在某些情况下,需要使用指针更改涉及对C语法的引用的C#语法 .XAML文件已更改,在
Grid
中的Canvas
中添加了Ellipse
.height
和width
与制作圆圈相同,我们在开始运行后以编程方式更改height
和width
.MainPage.xaml.h文件具有类成员更改 .
MainPage.xaml.cpp文件具有最彻底的更改 .
此应用程序生成一个窗口,其时钟如下所示:
Appendix I (Oct-25-2017): Adding WebView control
在查看XAML设计器的Visual Studio 2017的“工具箱”窗格中,在"All XAML Controls"部分中有一个控件
WebView
,可以将其插入XAML页面 .添加它并重新调整大小后,我得到了以下XAML代码:
通过添加
WebView
,我们现在可以在C代码中访问控件,以设置URI并在Web上显示网页或资源 .在我的初始化代码中添加了以下两行,将YouTube视频嵌入到
WebView
控件中: