1

Visual Studio を使用して、MVVM ライト ツールキットを使用して WP7 用のノートブック アプリを作成しています。設計時のデータが欲しいのですが、表示されません。実行時に動作し、現時点では実装はまったく同じ (DesignNoteDataService = NoteDataService) です (実際のデータソースはまだありません)。

私は何を間違っていますか?

DesignNoteDataService (& NoteDataService)

public class DesignNoteDataService : INoteDataService
{
    private List<Note> _noteList;

    private void InitNotes()
    {
        _noteList = new List<Note>();
        _noteList.Add(new Note(0, "Zahnarzt", "Zahnarzt morgen", Color.FromArgb(50, 20, 150, 50)));
        _noteList.Add(new Note(0, "Party", "Sarah 7 Jahre", Color.FromArgb(50, 200, 10, 50)));
        _noteList.Add(new Note(0, "Arbeit", "Projekt abgabe", Color.FromArgb(50, 20, 150, 50)));
        _noteList.Add(new Note(0, "Meeting", "Abend Hotel @ Olten", Color.FromArgb(50, 20, 150, 50)));
    }

    void INoteDataService.GetNoteList(Action<List<Note>, Exception> callback)
    {
        if (_noteList == null)
            InitNotes();
        callback(_noteList, null);
    }

    void INoteDataService.GetNoteById(Action<Note, Exception> callback, int id)
    {
        if (_noteList == null)
        {
            InitNotes();
        }
        var item = (from n in _noteList
                    where n.Id.Equals(id)
                    select n).First();
        callback(item, null);
    }
}

MainViewModel

public class MainViewModel : ViewModelBase
{
    private readonly INoteDataService _dataService;

    /// <summary>
    /// The <see cref="WelcomeTitle" /> property's name.
    /// </summary>
    public const string NoteListPropertyName = "NoteList";

    private ObservableCollection<Note> _noteList;

    /// <summary>
    /// Gets the WelcomeTitle property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ObservableCollection<Note> NoteList
    {
        get
        {
            return _noteList;
        }

        set
        {
            if (_noteList == value)
            {
                return;
            }

            _noteList = value;
            RaisePropertyChanged(NoteListPropertyName);
        }
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(INoteDataService dataService)
    {
        _dataService = dataService;
        NoteList = new ObservableCollection<Note>();
        _dataService.GetNoteList(
            (item, error) =>
            {
                if (error != null)
                {
                    return;
                }
                foreach (var note in item)
                {
                    NoteList.Add(note);
                }
            });
    }

MainPage.xaml でのバインディング

 <ListBox ItemsSource="{Binding NoteList}"/>

ViewModelLocator の一部

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<INoteDataService, Design.DesignNoteDataService>();
        }
        else
        {
            SimpleIoc.Default.Register<INoteDataService, NoteDataService>();
        }
4

1 に答える 1

2

ほとんどの場合DataContext、ビューでを設定する必要があります。通常、これはビューロケーターを使用して行います(サンプルについては、MVVM LightのWebサイトまたはここを参照してください)。

または、ビューのコンストラクターでを設定することもできますがDataContext、「ブレンド可能性」は失われます。

そうです、明らかに正しいデータサービスを設定(注入)する必要があります。

于 2011-10-12T13:28:39.150 に答える