1

リスト内のプロパティの 1 つが読み込みに時間がかかります (その場でサムネイルを作成します)。残りのプロパティをリストに表示し、処理に時間がかかるプロパティをバックグラウンドでロードするにはどうすればよいですか。

次の例は、状況を示しています。ショート ネームはすぐに表示できるようになり、ロング ネームは利用可能になったときに表示できるようにしたいと考えています。

public partial class MainWindow 
{
    public MainWindow()
    {
        InitializeComponent();

        var list = new List<Example> {
            new Example {ShortName = "A", LongName = "Z"}, 
            new Example {ShortName = "B", LongName = "ZZ"}, 
            new Example {ShortName = "C", LongName = "ZZZ"}};
        DataContext = list;
    }
}

public class Example : INotifyPropertyChanged
{
    private String _shortName;
    public String ShortName
    {
        get { return _shortName; }
        set
        {
            if (_shortName == value) return;
            _shortName = value;
            NotifyPropertyChanged("ShortName");
        }
    }

    private String _longName;
    public String LongName
    {
        get
        {
            System.Threading.Thread.Sleep(1000);
            return _longName;
        }
        set
        {
            if (_longName == value) return;
            _longName = value;
            NotifyPropertyChanged("LongName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label>ShortName: </Label>
                        <Label Content="{Binding ShortName}" />
                        <Label> LongName:</Label>
                        <Label Content="{Binding LongName}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
    </Grid>
</Window>
4

1 に答える 1

6

IsAsyncバインディング プロパティを使用して、プロパティを非同期的に読み込むことができます。

<Label Content="{Binding Path=LongName,IsAsync=true}" />

Fallbackプロパティを使用して、実際の値が設定されるまでLoadingなどのメッセージを表示することもできます。

于 2012-08-20T21:35:59.423 に答える