我最近遇到了为我的wpf应用程序创建添加和编辑对话框的问题.
我想在代码中做的就是这样.(我主要使用viewmodel第一种方法与mvvm)
调用对话框窗口的ViewModel:
var result = this.uiDialogService.ShowDialog("Dialogwindow Title", dialogwindowVM);
// Do anything with the dialog result
Run Code Online (Sandbox Code Playgroud)
它是如何工作的?
首先,我创建了一个对话服务:
public interface IUIWindowDialogService
{
bool? ShowDialog(string title, object datacontext);
}
public class WpfUIWindowDialogService : IUIWindowDialogService
{
public bool? ShowDialog(string title, object datacontext)
{
var win = new WindowDialog();
win.Title = title;
win.DataContext = datacontext;
return win.ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
WindowDialog是一个特殊而简单的窗口.我需要它来保留我的内容:
<Window x:Class="WindowDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="WindowDialog"
WindowStyle="SingleBorderWindow"
WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight">
<ContentPresenter x:Name="DialogPresenter" Content="{Binding .}">
</ContentPresenter>
</Window>
Run Code Online (Sandbox Code Playgroud)
wpf中对话框的问题是dialogresult = true只能在代码中实现.这就是为什么我为我dialogviewmodel实现它的界面.
public class …Run Code Online (Sandbox Code Playgroud) 我有一个运行后台任务的WPF应用程序使用async/await.任务是在进展时更新应用程序的状态UI.在此过程中,如果满足某个条件,我需要显示一个模态窗口,让用户知道此类事件,然后继续处理,现在还更新该模态窗口的状态UI.
这是我想要实现的草图版本:
async Task AsyncWork(int n, CancellationToken token)
{
// prepare the modal UI window
var modalUI = new Window();
modalUI.Width = 300; modalUI.Height = 200;
modalUI.Content = new TextBox();
using (var client = new HttpClient())
{
// main loop
for (var i = 0; i < n; i++)
{
token.ThrowIfCancellationRequested();
// do the next step of async process
var data = await client.GetStringAsync("http://www.bing.com/search?q=item" + i);
// update the main window status
var info = "#" + i …Run Code Online (Sandbox Code Playgroud)