将函数更改为依赖属性

发布于 2024-10-22 06:48:12 字数 629 浏览 1 评论 0原文

我是 XAML 和 WPF 的新手,正在学习 DependencyProperty 和 Path。例如,我有一个这样的函数

public byte[] DownloadPicture()
{
    WebClient webClient = new WebClient();
    byte[] data;
    data = webClient.DownloadData("https://graph.facebook.com/4/picture&type=large");       
    return data;
}

,并且我有这样的 dependencyproperty

public static DependencyProperty DownloadPicProperty = 
DependencyProperty.Register("DownloadPic", typeof(byte), 
    typeof(ImageControl), new PropertyMetadata(false));        

如何将 DependencyProperty 与我编写的 DownloadPicture 函数连接起来?有什么建议吗?我应该在 CLR 包装器中写什么?

I am new to XAML and WPF and I am learning about DependencyProperty and Path. For example, I have a function like this

public byte[] DownloadPicture()
{
    WebClient webClient = new WebClient();
    byte[] data;
    data = webClient.DownloadData("https://graph.facebook.com/4/picture&type=large");       
    return data;
}

and I have dependencyproperty like this

public static DependencyProperty DownloadPicProperty = 
DependencyProperty.Register("DownloadPic", typeof(byte), 
    typeof(ImageControl), new PropertyMetadata(false));        

How can I connect the DependencyProperty with the DownloadPicture function I wrote? Any suggestions? What should I write in the CLR wrapper?

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

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

发布评论

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

评论(1

梦情居士 2024-10-29 06:48:12

您还可以通过向控件添加标准属性来获取和设置依赖项属性的值。

public static DependencyProperty DownloadPicProperty =
    DependencyProperty.Register("DownloadPic", typeof(byte[]), typeof(ImageControl));

public byte[] DownloadPic
{
    get { return (byte[])GetValue(DownloadPicProperty); }
    set { SetValue(DownloadPicProperty, value); }
}

...
ImageControl imageControl = ...;
imageControl.DownloadPic = DownloadPicture();

You can get and set the value of a dependency property by adding a standard property to the control as well.

public static DependencyProperty DownloadPicProperty =
    DependencyProperty.Register("DownloadPic", typeof(byte[]), typeof(ImageControl));

public byte[] DownloadPic
{
    get { return (byte[])GetValue(DownloadPicProperty); }
    set { SetValue(DownloadPicProperty, value); }
}

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