GetService<IMessageBoxService>() returned null.

 GetService<IMessageBoxService>() returned null. 

 

Dear Support Team,

I readed online documentation of Data Editors (Lesson 1 ~ Lession 5 of [How To: Create a Registration Form]) and I also add the sample code as E4575 in my application project.
I tried to show validation fail error message dialogue, but application returned exception. It's because GetService<IMessageBoxService>() returned null. Could you tell me what's wrong and how to fix it?

The attach file is my sample code.

Thanks.

 

Hello Rinkin,

Thank you for providing the sample project. It is necessary to add the DXMessageBoxService service to the XAML code of the user controls to avoid this exception. Please try this and let me know your results.

XAML
</UserControl.Resources>  
<dxmvvm:Interaction.Behaviors>  
    <dx:DXMessageBoxService/>  
</dxmvvm:Interaction.Behaviors>  
<Grid>  

需要添加上面的dxmvvm

 

<dxmvvm:Interaction.Behaviors>
                                <dxmvvm:EventToCommand EventName="SuggestionChosen"
                                                       Command="{Binding SuggestionChosenCommand}"
                                                       PassEventArgsToCommand="True" />
                            </dxmvvm:Interaction.Behaviors>

然后user model里面指定   

public DelegateCommand<AutoSuggestEditSuggestionChosenEventArgs> SuggestionChosenCommand { get; set; }

 

UWP Binding to AutoSuggestBox in MVVM

InvokeCommandAction has a parameter named InputConverter which you can use to convert the event args to some other parameter that can be passed to your ViewModel.

First create a IValueConverter class to extract what you need from your event args like this:-

public class AutoSuggestQueryParameterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
       {
          // cast value to whatever EventArgs class you are expecting here
          var args = (AutoSuggestBoxQuerySubmittedEventArgs)value;
          // return what you need from the args
          return (string)args.ChosenSuggestion;
       }
}

Then use that converter in your XAML like this:

<Page.Resources>
     <converters:AutoSuggestQueryParameterConverter x:Key="ArgsConverter" />
</Page.Resources>

<AutoSuggestBox Name="SearchAutoSuggestBox"
            PlaceholderText="Search by keywords"
            QueryIcon="Find">
    <interactivity:Interaction.Behaviors>
      <core:EventTriggerBehavior EventName="QuerySubmitted">
        <core:InvokeCommandAction 
              Command="{x:Bind ViewModel.SearchCommand}"
              InputConverter="{StaticResource ArgsConverter}" />
      </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</AutoSuggestBox>

Finally in your viewmodel change your command to accept a string as parameter. So you would have the following in your vm:

public DelegateCommand<string> SearchCommand { get; }

public MainPageViewModel()
{
    SearchCommand = new DelegateCommand<string>(ExecuteMethod);
}

private void ExecuteMethod(string o)
{
    // CODE HERE
}

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-07-18 Life Cycle Stages of IIS 7.0
2019-07-18 widget jquery 理解
2017-07-18 sqlcmd
2017-07-18 无法往SQL Server Management Studio拖脚本
2016-07-18 Task<TResult>的使用
2014-07-18 C#创建继承的窗体
2014-07-18 git的软件安装
点击右上角即可分享
微信分享提示