【问题标题】:Binding doesnot work in WinRT - XAML绑定在 WinRT 中不起作用 - XAML
【发布时间】:2014-06-13 19:33:34
【问题描述】:

我有自己的控制,绑定在其中不起作用。下面是我尝试绑定的代码。

XAML 中的代码

<cc:MyControl Name="myControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemSource="{Binding Path=Document}"></cc:MyControl>

ItemSource 依赖属性的 CodeBehind 中的代码

 public Object ItemSource
    {
        get { return (Object)GetValue(ItemSourceProperty); }
        set { SetValue(ItemSourceProperty, value); }
    }

public static readonly DependencyProperty ItemSourceProperty =
        DependencyProperty.Register("ItemSource", typeof(Object), typeof(MyControl), new PropertyMetadata(null, new PropertyChangedCallback(DocumentLoadCallBack)));

 private static void DocumentLoadCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
}

这甚至没有被触发.....

但是当我为 ItemSource 提供一些数据而没有如下绑定时,会触发 DocumentLoadCallBack。

<cc:MyControl Name="myControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemSource="ImagePath"></cc:MyControl>

“文档”绑定属性代码

public sealed partial class MainPage : Page
{
private StorageFile _doc = null;
    public StorageFile Document
    {
        get
        {
            if (this._doc == null)
            {
                this._doc = GetDoc().Result;
            }
            return this._doc;
        }
        set
        {
            this._doc = value;
        }
    }

    private async Task<StorageFile> GetDoc()
    {
        //return imagedocument location
    }
}

有人帮我解决这个问题吗?

【问题讨论】:

  • 看起来是异步的?如果你调用GetDoc,它不会马上返回结果?

标签: c# xaml binding windows-runtime winrt-xaml


【解决方案1】:

您在属性的 get 访问器中使用异步方法。 因此,当您尝试获取时,它不会返回结果,因为该方法是异步的,您不应该这样做。

您应该先读取文件(例如 Load 事件),然后更新属性,而不是读取属性内的文件。 这不是一个很好的开发实践。

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 2016-09-09
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多