如何在 C++ 中以编程方式设置堆栈面板的边框颜色在WinUI3中?

发布于 2025-01-10 14:22:49 字数 780 浏览 0 评论 0原文

我正在使用 C++ 中的 WinUI 3 开发一个项目,我想根据某些条件更改 XAML 控件(例如 stackpanel)的边框颜色。我尝试过在线搜索,但大多数答案都是用 C# 编写的,还有一些用 C++ 编写的,我尝试过但没有成功。

例如:(“StackPanel”在 xaml 中定义)

StackPanel().BorderBrush(SolidColorBrush(ColorHelper::FromArgb(255, 255, 255, 255)));

然后会出现错误:

没有重载函数的实例与参数列表匹配
参数类型为:(winrt::Windows::UI::Xaml::Media::SolidColorBrush)
对象类型为:winrt::Microsoft::UI::Xaml::Controls::StackPanel

我在 .cpp 文件中尝试过另一个对象类型:

StackPanel().BorderBrushProperty(SolidColorBrush(Colors::Black()));

错误是:

函数调用中的参数过多。

为什么会发生这些错误?

有人能帮我解决这个问题吗?或者有什么建议吗?
示例代码会很棒!

PS:我对 WinUI 3 还是很陌生,尤其是在 C++ 方面(没有太多 C++ 的学习材料),

如果有任何帮助,我将不胜感激。

I am working on a project using WinUI 3 in C++, and I want to change the border color of a XAML control(e.g. stackpanel) according to some condition. I have tried search it online, but most of answers are in c#, and some in C++ I have tried but got no luck.

For example: ("StackPanel" is defined in the xaml )

StackPanel().BorderBrush(SolidColorBrush(ColorHelper::FromArgb(255, 255, 255, 255)));

Then the error would come up:

no instance of overloaded function matches the argument list
argument types are: (winrt::Windows::UI::Xaml::Media::SolidColorBrush)
object type is: winrt::Microsoft::UI::Xaml::Controls::StackPanel

And another one I tried in .cpp file:

StackPanel().BorderBrushProperty(SolidColorBrush(Colors::Black()));

the error is:

too many arguments in function call.

Why are these errors happening?

Could anyone help me on this? Or any suggestions?
Sample code would be great!

PS : I am still very new to WinUI 3 especially in C++ (there are not so much study material for C++)

I would be grateful for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-01-17 14:22:49

从我所看到的实际错误(尽可能复制您正在做的事情)是

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'void Windows::UI::Xaml::Controls::StackPanel::BorderBrush::set(Windows::UI::Xaml::Media::Brush ^)': cannot convert argument 1 from 'Windows::UI::Xaml::Media::SolidColorBrush' to 'Windows::UI::Xaml::Media::Brush ^'  App1    c:\repos\App1\MainPage.xaml.cpp 29  

如果您像这样创建画笔 auto Brush = ref new SolidColorBrush(...);< br>
并将其传递给 stackpanel,如下所示 stackpanel->BorderBrush = Brush;
你的错误应该消失。

显然,您获取堆栈面板的方式可能与我的方式不同,但重点是,只要您拥有正确形式的值,您就应该能够将其设置为值 ref new object 在这种情况下,看起来。

From what I'm seeing the actual error (as best as i can replicate what you're doing) is

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'void Windows::UI::Xaml::Controls::StackPanel::BorderBrush::set(Windows::UI::Xaml::Media::Brush ^)': cannot convert argument 1 from 'Windows::UI::Xaml::Media::SolidColorBrush' to 'Windows::UI::Xaml::Media::Brush ^'  App1    c:\repos\App1\MainPage.xaml.cpp 29  

If you create your brush like this auto brush = ref new SolidColorBrush(...);
And pass it to the stackpanel like this stackpanel->BorderBrush = brush;
Your error should go away.

Obviously how you are getting your stack panel might be different from how i did, but the point is you should be able to just set it to the value as long as you have the value in the correct form a ref new object in this case, it would seem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文