【问题标题】:mvvm light - NavigationService / DialogService classes not foundmvvm light - 未找到 NavigationService / DialogService 类
【发布时间】:2015-05-19 06:03:46
【问题描述】:

应该是一个简单的答案,但我没有看到它。

MVVM Light v5 引入了 NavigationService 和 DialogService。我想制作一个示例应用程序来玩弄它们。建议似乎是我需要做的就是在ViewModelLocator 中注册它们:

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

IDialogService 需要 Galasoft.MvvmLight.Views 命名空间,它会自动解析,但找不到 DialogService 类,VS 无法推荐要导入的命名空间。

NavigationService 类似

【问题讨论】:

    标签: c# wpf mvvm-light


    【解决方案1】:

    我假设您使用的是 WPF,在这种情况下,没有 IDialogService 和 INavigationService 的默认实现。因此,您需要创建自己的实现。

    【讨论】:

    • 这让我吃惊,我原以为 WPF 会是第一个拥有默认实现的。您是否知道任何地方都有这两种基本服务的代码示例(所以我可以看到它需要实现的功能)
    • 回答了我自己的评论。 This article 给了我足够的工作机会。
    【解决方案2】:

    这是基于他的一些示例应用程序的代码...感谢 Laurent,你真棒!我花了一段时间才找到这个...希望这会有所帮助:)

    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;
        }
    }
    

    【讨论】:

    • @Ergli,首先,感谢您为大家提供代码。我发现 DialogService 和 NavigationService 代码显然仅适用于 Xamarin。 Xamarin 通过 DialogService 中的 Page 的 DisplayAlert() 与“常规”Windows 发生冲突。 NavigationService 冲突涉及 NavigationPage 对象。我必须找到解决方法或“常规”版本。
    • 对于“常规”Windows 和我需要自定义模态对话框,我最终选择了在 github.com/FantasticFiasco/mvvm-dialogs 找到的开源 MVVM 对话框包。 MVVM Light 示例位于:github.com/FantasticFiasco/…
    猜你喜欢
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多