wpf mvvm light - NavigationService / DialogService 类未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30317863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:08:30  来源:igfitidea点击:

mvvm light - NavigationService / DialogService classes not found

c#wpfmvvm-light

提问by SeeMoreGain

Should be a simple answer, but I'm not seeing it.

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

MVVM Light v5 introduced a NavigationService and DialogService. I wanted to make a sample application to play around with them. The advice seems to be that all i need do is register them in the ViewModelLocatoras such:

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

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

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

The IDialogServiceneeds the Galasoft.MvvmLight.Viewsnamespace, which gets automatically resolved, but the DialogServiceclass cannot be found, and VS cannot recommend a namespace to import.

IDialogService需要Galasoft.MvvmLight.Views空间,它就会自动解决,但DialogService类不能被发现,并且VS不能推荐一个命名空间的进口。

Similar for NavigationService

类似的 NavigationService

采纳答案by Bracher

I'm assuming you are using WPF, in which case there isn't a default implementation of IDialogService and INavigationService. Thus you will need to create your own implementations.

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

回答by Egli Becerra

Here is the code based on some of his sample applications... thanks Laurent u rock! it took me a while to find this... hope this helps :)

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

DialogService Implementation

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 Implementation

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