c# - 带有 Prism 的 MVVM 编码是否与 MVVM 本身不同?

标签 c# wpf mvvm

我最近使用 MVVM 创建了一个接口(interface),以确保没有后面的代码。我们在印度的开发团队通过一些修改重用了该接口(interface)。当我开始使用新的 UI 时,我发现这些修改似乎破坏了我在 View 和 View 模型之间所做的一些绑定(bind)。然后我在 View 下面找到了这个新代码。我只包含了部分代码。您可以看到 View 模型在 View 中被多次引用。我虽然很气馁。我知道在 View 中为 View 模型设置一个 setter 是他们在 Prism 实现中一直在做的事情,但我之前从未见过从 View 中对 View 模型的实际调用。除非我确定我在良好编码实践方面的立场,否则我不想向他们的主管提出这个问题。

    [Import]
    public ProfileLimitsViewModel ViewModel
    {
        get
        {
            return DataContext as ProfileLimitsViewModel;
        }
        set
        {
            DataContext = value;
            _performSelection = true;
            ViewModel.OnSelectedProfilesChanged -= setSelection;
            ViewModel.OnSelectedProfilesChanged += setSelection;
        }
    }

    /// <summary>
    /// On Profile selection changed in UI
    /// </summary>
    private void ProfileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            int count = 0;
            ViewModel.SelectedProfiles.Clear();

            //if multiple profile is selected
            if (ProfileList.SelectedItems.Count > 0)
            {
                foreach (var profile in ProfileList.SelectedItems)
                {
                    ViewModel.SelectedProfiles.Add((profile as ProfileNS).ProfileID);

                    //the profile that is selected
                    if (count++ == 0)
                        ViewModel.SelectedProfile = profile as ProfileNS;
                }
            }

            if (_performSelection)
            {
                ViewModel.SelectedFormationId = ViewModel.SelectedProfile.Layers[0].BedId;
                ViewModel.ProfileSelectionChanged();
            }
        }
        catch(Exception ex)
        {
            Diagnostics.LogHandledException(ex);
        }
    }

    /// <summary>
    /// on closed
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ProfileEditor_Closed(object sender, RoutedEventArgs e)
    {
        //Event to update ProfileEditor checkbox in ContextualTab
        ServiceLocator.Current.GetInstance<IEventAggregatorService>().Publish
            <ProfileEditorSelectedEvent, MCWDEditorMode>(MCWDEditorMode.Close);

        ViewModel.ModuleService.UpdateMcwdEditorViewModelsHash(ViewModel.WellLandingId, null);
    }

    /// <summary>
    /// On formation selection changed in UI
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ProfileLayersDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0 && _performSelection)
                ViewModel.FormationSelectionChanged(e.AddedItems[0]);
        }
        catch (Exception ex)
        {
            Diagnostics.LogHandledException(ex);
        }
    }

最佳答案

C# 中的事件处理程序肯定表明您没有正确执行 MVVM。我在看着你,ProfileList_SelectionChange .您/您的老板的引用:https://msdn.microsoft.com/en-us/library/hh848246.aspx Prism 只为您提供正确执行 MVVM 的工具;它不会阻止您以 WinForms 风格做事。

关于c# - 带有 Prism 的 MVVM 编码是否与 MVVM 本身不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442253/

相关文章:

c# - 如何使用selenium webdriver在c#中以私有(private)模式启动IE

c# - 如何从 CompletedEventArgs 获取抛出异常的类型?

c# - 在编译后将属性注入(inject) .NET 类

c# - 在基于 View 的WPF应用程序中从ViewModel更改 View

c# - 将 out 修饰符的值返回到 C# 中的集合

c# - 在 RichTextBox 中设置插入符/光标位置 - WPF

wpf - 您可以在 Style 或 ControlTemplate 中包含的 Storyboard 中使用 DynamicResource

WPF VisualStateManager 动画绑定(bind)

c# - 如何将 WPF 控件文本绑定(bind)到 bool 值,并在 xaml 中决定文本

c# - 如何使用本地主机测试 HttpListener()?