2015-05-19 14 views
9

Phải là một câu trả lời đơn giản, nhưng tôi không nhìn thấy nó.ánh sáng mvvm - Không tìm thấy các hộp NavigationService/DialogService

MVVM Light v5 đã giới thiệu một NavigationService và DialogService. Tôi muốn làm một ứng dụng mẫu để chơi với chúng. Lời khuyên dường như là tất cả tôi cần làm là đăng ký chúng trong ViewModelLocator như vậy:

SimpleIoc.Default.Register<IDialogService, DialogService>();

các IDialogService cần không gian tên Galasoft.MvvmLight.Views, mà được tự động giải quyết, nhưng lớp DialogService không thể được tìm thấy, và VS không thể đề xuất một không gian tên để nhập.

tương tự cho NavigationService

Trả lời

5

tôi giả sử bạn đang sử dụng WPF, trong trường hợp không có một thực hiện mặc định của IDialogService và INavigationService. Vì vậy, bạn sẽ cần phải tạo ra các triển khai của riêng bạn.

+2

Đó làm tôi ngạc nhiên, tôi sẽ suy nghĩ đã WPF sẽ là người đầu tiên có triển khai mặc định. Bạn có biết bất cứ nơi nào có một mẫu mã cho một dịch vụ cơ bản cho cả hai (vì vậy tôi có thể thấy chức năng nó sẽ cần phải thực hiện) – SeeMoreGain

+0

Đã trả lời nhận xét của riêng tôi. [Bài viết này] (http://www.c-sharpcorner.com/UploadFile/3789b7/modern-ui-for-wpf-application-by-example-navigationservice/) cung cấp cho tôi đủ để làm việc. – SeeMoreGain

6

Đây là mã dựa trên một số ứng dụng mẫu của anh ấy ... cảm ơn Laurent u rock! nó đã cho tôi một thời gian để tìm thấy điều này ... hy vọng điều này sẽ giúp :)

DialogService Thực hiện

/// <summary> 
/// Example from Laurent Bugnions Flowers.Forms mvvm Examples 
/// </summary> 
public class DialogService : IDialogService 
{ 
    private Page _dialogPage; 

    public void Initialize(Page dialogPage) 
    { 
     _dialogPage = dialogPage; 
    } 

    public async Task ShowError(string message, 
     string title, 
     string buttonText, 
     Action afterHideCallback) 
    { 
     await _dialogPage.DisplayAlert(
      title, 
      message, 
      buttonText); 

     if (afterHideCallback != null) 
     { 
      afterHideCallback(); 
     } 
    } 

    public async Task ShowError(
     Exception error, 
     string title, 
     string buttonText, 
     Action afterHideCallback) 
    { 
     await _dialogPage.DisplayAlert(
      title, 
      error.Message, 
      buttonText); 

     if (afterHideCallback != null) 
     { 
      afterHideCallback(); 
     } 
    } 

    public async Task ShowMessage(
     string message, 
     string title) 
    { 
     await _dialogPage.DisplayAlert(
      title, 
      message, 
      "OK"); 
    } 

    public async Task ShowMessage(
     string message, 
     string title, 
     string buttonText, 
     Action afterHideCallback) 
    { 
     await _dialogPage.DisplayAlert(
      title, 
      message, 
      buttonText); 

     if (afterHideCallback != null) 
     { 
      afterHideCallback(); 
     } 
    } 

    public async Task<bool> ShowMessage(
     string message, 
     string title, 
     string buttonConfirmText, 
     string buttonCancelText, 
     Action<bool> afterHideCallback) 
    { 
     var result = await _dialogPage.DisplayAlert(
      title, 
      message, 
      buttonConfirmText, 
      buttonCancelText); 

     if (afterHideCallback != null) 
     { 
      afterHideCallback(result); 
     } 

     return result; 
    } 

    public async Task ShowMessageBox(
     string message, 
     string title) 
    { 
     await _dialogPage.DisplayAlert(
      title, 
      message, 
      "OK"); 
    } 
} 

NavigationService Thực hiện

/// <summary> 
/// Example from Laurent Bugnions Flowers.Forms mvvm Examples 
/// </summary> 
public class NavigationService : INavigationService 
{ 
    private readonly Dictionary<string, Type> _pagesByKey = new Dictionary<string, Type>(); 
    private NavigationPage _navigation; 

    public string CurrentPageKey 
    { 
     get 
     { 
      lock (_pagesByKey) 
      { 
       if (_navigation.CurrentPage == null) 
       { 
        return null; 
       } 

       var pageType = _navigation.CurrentPage.GetType(); 

       return _pagesByKey.ContainsValue(pageType) 
        ? _pagesByKey.First(p => p.Value == pageType).Key 
        : null; 
      } 
     } 
    } 

    public void GoBack() 
    { 
     _navigation.PopAsync(); 
    } 

    public void NavigateTo(string pageKey) 
    { 
     NavigateTo(pageKey, null); 
    } 

    public void NavigateTo(string pageKey, object parameter) 
    { 
     lock (_pagesByKey) 
     { 
      if (_pagesByKey.ContainsKey(pageKey)) 
      { 
       var type = _pagesByKey[pageKey]; 
       ConstructorInfo constructor = null; 
       object[] parameters = null; 

       if (parameter == null) 
       { 
        constructor = type.GetTypeInfo() 
         .DeclaredConstructors 
         .FirstOrDefault(c => !c.GetParameters().Any()); 

        parameters = new object[] 
        { 
        }; 
       } 
       else 
       { 
        constructor = type.GetTypeInfo() 
         .DeclaredConstructors 
         .FirstOrDefault(
          c => 
          { 
           var p = c.GetParameters(); 
           return p.Count() == 1 
             && p[0].ParameterType == parameter.GetType(); 
          }); 

        parameters = new[] 
        { 
         parameter 
        }; 
       } 

       if (constructor == null) 
       { 
        throw new InvalidOperationException(
         "No suitable constructor found for page " + pageKey); 
       } 

       var page = constructor.Invoke(parameters) as Page; 
       _navigation.PushAsync(page); 
      } 
      else 
      { 
       throw new ArgumentException(
        string.Format(
         "No such page: {0}. Did you forget to call NavigationService.Configure?", 
         pageKey), 
        "pageKey"); 
      } 
     } 
    } 

    public void Configure(string pageKey, Type pageType) 
    { 
     lock (_pagesByKey) 
     { 
      if (_pagesByKey.ContainsKey(pageKey)) 
      { 
       _pagesByKey[pageKey] = pageType; 
      } 
      else 
      { 
       _pagesByKey.Add(pageKey, pageType); 
      } 
     } 
    } 

    public void Initialize(NavigationPage navigation) 
    { 
     _navigation = navigation; 
    } 
} 
Các vấn đề liên quan