Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向用户界面发送文本消息的有效技术_C#_.net_Wpf - Fatal编程技术网

C# 向用户界面发送文本消息的有效技术

C# 向用户界面发送文本消息的有效技术,c#,.net,wpf,C#,.net,Wpf,我有一个WPF Windows应用程序。viewmodel调用Model.TrySomething()格式的方法,如果TrySomething中的任何内容逻辑上失败,该方法将返回布尔值。如果返回false,UI可以向用户发回消息 从模型中提取此信息的最佳方法是什么?这是我们在项目中的做法。工作正常: // your event args might include more properties public class ShowMessageBoxEventArgs : System.Even

我有一个WPF Windows应用程序。viewmodel调用Model.TrySomething()格式的方法,如果TrySomething中的任何内容逻辑上失败,该方法将返回布尔值。如果返回false,UI可以向用户发回消息


从模型中提取此信息的最佳方法是什么?

这是我们在项目中的做法。工作正常:

// your event args might include more properties
public class ShowMessageBoxEventArgs : System.EventArgs
{
    public string Title { get; set; }
    public string Text { get; set; }
}

// example of your model base
public class MyModelBase
{
    public event EventHandler<ShowMessageBoxEventArgs> ShowMessageBox;
    protected void RaiseShowMessageBox(string title, string text)
    {
        if (ShowMessageBox == null)
            return;
        var _Args = new ShowMessageBoxEventArgs
        {
            Text = text,
            Title = title
        };
        ShowMessageBox(this, _Args);
    }
}

// for this sample, this is your view model
public class MyModel : MyModelBase
{
    public void DoSomething()
    {
        // TODO: Do Something
        base.RaiseShowMessageBox("DoSomething", "Complete!");
    }
}

// this is your window or in app.xaml.cs (where we do it)
public partial class MainWindow : Window
{
    MyModel m_MyModel = new MyModel();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = m_MyModel;
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    bool m_Loaded = false; // only once
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if (m_Loaded)
            return;
        m_Loaded = true;

        // allow model to show messagebox
        m_MyModel.ShowMessageBox += (s, arg) =>
        {
            MessageBox.Show(arg.Text, arg.Title);
        };
    }
}
//事件参数可能包含更多属性
公共类ShowMessageBoxEventArgs:System.EventArgs
{
公共字符串标题{get;set;}
公共字符串文本{get;set;}
}
//模型库的示例
公共类MyModelBase
{
公共事件事件处理程序ShowMessageBox;
受保护的void RaiseShowMessageBox(字符串标题、字符串文本)
{
if(ShowMessageBox==null)
返回;
var\u Args=新的ShowMessageBoxEventArgs
{
Text=Text,
头衔
};
ShowMessageBox(此,_Args);
}
}
//对于此示例,这是您的视图模型
公共类MyModel:MyModelBase
{
公共无效剂量测定法()
{
//托多:做点什么
base.RaiseShowMessageBox(“DoSomething”,“Complete!”);
}
}
//这是您的窗口或app.xaml.cs(我们在其中执行此操作)
公共部分类主窗口:窗口
{
MyModel m_MyModel=新的MyModel();
公共主窗口()
{
初始化组件();
this.DataContext=m_MyModel;
已加载+=新路由EventHandler(主窗口已加载);
}
bool m_Loaded=false;//仅一次
已加载无效主窗口(对象发送器、路由目标)
{
如果(m_加载)
返回;
m_Loaded=真;
//允许模型显示messagebox
m_MyModel.ShowMessageBox+=(s,arg)=>
{
MessageBox.Show(arg.Text,arg.Title);
};
}
}

祝你好运

如果要显示的消息是一个模式对话框,则可以编写一个服务(将其命名为MessageDialogService),将其注入到viewmodel中,然后调用MessageDialogService.Show()方法。此方法创建一个新的WPF窗口并显示消息


然后,可以在任何ViewModels中使用此服务来显示消息。

变量“Loaded”是什么?This.Loaded是标准的WPF窗口事件。每次在可视树中添加或刷新窗口时,都会发生此事件。因此,m_Loaded可以简单地防止多次处理。这对于UserControls中加载的处理程序尤其重要。