2015-05-19 9 views
9

एक साधारण उत्तर होना चाहिए, लेकिन मैं इसे नहीं देख रहा हूं।एमवीवीएम प्रकाश - नेविगेशन सेवा/संवाद सेवा कक्षाएं नहीं मिली

एमवीवीएम लाइट वी 5 ने नेविगेशन सेवा और संवाद सेवा पेश की। मैं उनके साथ खेलने के लिए नमूना आवेदन करना चाहता था। सलाह होने के लिए सभी मुझे क्या जरूरत है उन्हें इस तरह के रूप ViewModelLocator में रजिस्टर है कि लगता है:

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

IDialogServiceGalasoft.MvvmLight.Views नाम स्थान है, जो स्वचालित रूप से हल हो जाता है की जरूरत है, लेकिन DialogService वर्ग नहीं पाया जा सकता है, और वी.एस. आयात करने के लिए नामस्थान की सिफारिश नहीं कर सकते हैं।

के लिए NavigationService

उत्तर

5

इसी प्रकार मैं तुम्हें WPF का उपयोग कर रहे है जिस स्थिति में वहाँ IDialogService और INavigationService की डिफ़ॉल्ट कार्यान्वयन नहीं है संभालने हूँ,। इस प्रकार आपको अपने स्वयं के कार्यान्वयन की आवश्यकता होगी।

+2

जो मुझे आश्चर्य है, मैं सोचा है होगा कि WPF डिफ़ॉल्ट कार्यान्वयन करने वाले पहले व्यक्ति होंगे। क्या आप कहीं भी जानते हैं कि इनमें से दोनों के लिए मूल सेवा के लिए कोड नमूना है (इसलिए मैं इसे कार्यान्वित करने की आवश्यकता वाले कार्यक्षमता को देख सकता हूं) – SeeMoreGain

+0

ने मेरी अपनी टिप्पणी का उत्तर दिया। [यह आलेख] (http://www.c-sharpcorner.com/UploadFile/3789b7/modern-ui-for-wpf-plplication-by-example-navigationservice/) मुझे काम करने के लिए पर्याप्त देता है। – SeeMoreGain

6

यहां उनके कुछ नमूना अनुप्रयोगों के आधार पर कोड है ... धन्यवाद लॉरेंट यू रॉक! यह मुझे कुछ समय लिया खोजने के लिए इस ... उम्मीद है कि इस मदद करता है :)

DialogService कार्यान्वयन

/// <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 कार्यान्वयन

/// <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; 
    } 
} 
संबंधित मुद्दे