繁体   English   中英

mvvm light - 找不到NavigationService / DialogService类

[英]mvvm light - NavigationService / DialogService classes not found

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

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

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

IDialogService需要Galasoft.MvvmLight.Views命名空间,该命名空间会自动解析,但无法找到DialogService类,并且VS无法建议导入命名空间。

类似于NavigationService

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

以下是基于他的一些示例应用程序的代码...感谢Laurent u rock! 我花了一段时间才找到这个......希望这有助于:)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM