c# - 如何在 MVVM 中启动非模态对话框?

标签 c# wpf mvvm

我有一个使用 MVVM 设计的 C# 应用程序。该结构是常规的。

我有一个 View 模型,我想在其中启动一个自定义的非模态对话框。

此对话框负责收集数据绑定(bind)序列号。

这是我想象我的非模态对话框的方式:

enter image description here

这是 Xaml 代码:

<Window x:Class="RailComm.EmbeddedApplications.EacManagementTool.Views.RestoreDeviceDialogView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RailComm.EmbeddedApplications.EacManagementTool.Views"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        Title="Device Restore" Height="150" Width="375">
        <Window.Resources>
            <Style TargetType="{x:Type TextBox}" x:Key="SelectableLabel">
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="IsReadOnly" Value="True" />
                <Setter Property="HorizontalAlignment" Value="Center" />
            </Style>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="35"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0">
                <Label HorizontalAlignment="Left" Grid.Row="1">This device has no identity, would you like to restore it?</Label>
            </StackPanel>
            <StackPanel Grid.Row="1">
                <UniformGrid Rows="1" Columns="2" >
                    <Label HorizontalAlignment="Right">Device IP:</Label>
                    <TextBox Text="{Binding IpAddress, Mode=OneWay}" Style="{StaticResource SelectableLabel}"></TextBox>
                </UniformGrid>
            </StackPanel>
            <StackPanel Grid.Row="2">
                <UniformGrid Rows="1" Columns="2" >
                    <Label HorizontalAlignment="Right">Serial Number:</Label>
                    <TextBox Text="{Binding SerialNumber, Mode=TwoWay}"></TextBox>
                </UniformGrid>
            </StackPanel>
            <DockPanel Grid.Row="3">
                <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Margin="5">
                    <Button Content="No" Command="{Binding CancelCommand }" Width="45"/>
                    <Separator Width="5"/>
                    <Button Content="Identify" Command="{Binding IdentifyCommand }"/>
                    <Separator/>
                </StackPanel>
            </DockPanel>
        </Grid>
    </Window>

现在我想将它挂接到我现有的 View 模型中,它还包含其他 View 模型作为属性。

我也想添加的 View 模型有一个名为 GetUpdate() 的方法.在方法的某个时刻,我想向用户显示非模态对话框。如果序列号通过检查,我将排除结果并关闭对话框。如果序列号失败,我想在同一个对话框中显示错误。

我计划拥有这些文件:
ExistingViewModel.cs
DialogViewModel.cs
DialogView.xaml

我只是不知道如何将它们连接起来。异步显示这个模型对我来说非常重要。我可能会在 Task.Rush() 中运行对话。 ..

此对话框不会更新父 View 。此对话框只是获取要传递给另一个方法的值。

我该如何实现?我见过其他人的方法,但它们对我的实现没有意义。他们不显示/解释 View 和 View 模型是如何连接的。他们经常遗漏一些没有归属感的东西或代码块,例如这段代码应该进入这个类......

非常感谢任何能让我走上正轨的人!

最佳答案

这是一种方法,例如。
使用 MVVM 模式时显示对话框
http://www.codeproject.com/Articles/36745/Showing-Dialogs-When-Using-the-MVVM-Pattern

重要的是保留您的 ViewModel清理与 View 相关的东西。使用 IDialogService with 将 View 内容抽象出来,可以注入(inject) ViewModel并且易于模拟以进行测试。

DialogService 也将是一个很好的关键词,结合 MVVM 有很多例子。
这使您还可以完全控制对话的行为。

高温高压

关于c# - 如何在 MVVM 中启动非模态对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297345/

相关文章:

c# - 仅在第一次打印时出现 Hardmargin 的奇怪打印问题 (WinForms)

c# - 弯曲并遵循某些路径几何形状的 WPF 动画

c# - 在由多个 View 模型绑定(bind)的 View 中绑定(bind)图像不起作用

c# - 在C#中从ViewModel中的View访问按钮

.net - View 或 View 模型哪个先出现?

c# - MVVM WPF 教程

c# - 在 asp.net 中使用文本框自动回发

c# - 如何使用 C# 将 Excel 工作表中特定行或单元格的字体加粗?

c# - 如何在WPF/C#中将RichTextBox与字符串进行比较

.net - WPF 中的应用程序级快捷键