【问题标题】:Freezing main window after adding some items from childwindow从子窗口添加一些项目后冻结主窗口
【发布时间】:2023-03-09 11:30:01
【问题描述】:

我正在使用 mvvm 和 WCF 服务开发 Silverlight 4。

每当我将项目从子窗口添加到主窗口时。但同时主窗口自动进入禁用模式。我认为主窗口会自动冻结。

子窗口视图模型

    public class AddFormFieldInformationViewModel : ViewModelBase
{


 private FieldInformationViewModel _FieldInformationViewModel;
    public FieldInformationViewModel FieldInformationViewModel
    {
        get { return _FieldInformationViewModel; }
        set
        {
            _FieldInformationViewModel = value;
            RaisePropertyChanged("FieldInformationViewModel");
        }
    }
 public void MoveSave(object obj)
    { this.FieldInformationViewModel.SelectedFormFields = FieldInformationModel;
                            ResultHandler(true);
 }

公共操作结果处理程序 { 获取;放; } }

ChildWindow .xaml.cs 文件

public partial class AddExistingFormFieldCategoryView : ChildWindow
{
    private AddFormFieldInformationViewModel vm;
    public AddExistingFormFieldCategoryView()
    {
        InitializeComponent();
         vm = new AddFormFieldInformationViewModel();
        this.DataContext = vm;
        vm.ResultHandler = result => { if (result) { Close(); } };

    }        
}

主窗口视图模型

public class FieldInformationViewModel : ViewModelBase
{   private void executeOpenChildWindow(object parameter)
    {
        AddExistingFormFieldCategoryView cw = new AddExistingFormFieldCategoryView();
        ((AddFormFieldInformationViewModel)cw.DataContext).FieldInformationViewModel = this;
        cw.Show();
    }
}

将子窗口中的项目添加到主窗口后,有时我的主窗口会自动冻结。

【问题讨论】:

    标签: c# .net silverlight entity-framework mvvm


    【解决方案1】:

    您好,这是您遇到的错误,请参阅下面的示例。我也是同样的问题,你必须手动启用主页的显示:

            private static void ShowError(string message, string details)
            {
                ErrorWindow error = new ErrorWindow(message, details);
                error.Closed += new EventHandler(error_Closed);
                error.Show();
            }
    
            static void error_Closed(object sender, EventArgs e)
            {
                Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
            }
    

    【讨论】:

      【解决方案2】:

      您可以子类化 ChildWindow 并将 RootVisual 显式设置为启用。 Source

      using System.Windows.Ink;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Animation;
      using System.Windows.Shapes;
      
      namespace DST_Common_Silverlight_Controls
      {
          /// <summary>
          /// Bug in ChildWindow sometimes leaves app disabled. 
          /// </summary>
          public class ChildWindowEx : ChildWindow
          {
              protected override void OnClosed(EventArgs e)
              {
                  base.OnClosed(e);
                  Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
              }
          }
      }
      

      然后像这样在 xaml 中使用新类型而不是 ChildWindow:

      <slcommon:ChildWindowEx 
          x:Class="DST.AvSyncMonitor.Silverlight.Gui.ErrorWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:slcommon="clr-namespace:DST_Common_Silverlight_Controls;assembly=DST.Common.Silverlight.Controls"
          xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"    
          Title="Error">
      
          <Grid x:Name="LayoutRoot" Width="540">
              <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition/>
                  <RowDefinition Height="Auto"/>
              </Grid.RowDefinitions>
      
              <TextBlock x:Name="IntroductoryText" Grid.Row="0" Margin="0" 
                   Text="An unknown error was encountered. Please contact your administrator for more information."/>
      
              <StackPanel x:Name="ContentStackPanel" Grid.Row="2" Margin="0,6,0,0">
      
                  <TextBlock x:Name="LabelText" TextWrapping="Wrap" Margin="0,0,0,2" 
                           Text="Error details"/>
                  <TextBox x:Name="ErrorTextBox" Height="90" TextWrapping="Wrap" IsReadOnly="True"
                           VerticalScrollBarVisibility="Auto"/>
      
              </StackPanel>
      
              <Button x:Name="OKButton" Grid.Row="3" Click="OKButton_Click" 
                  Width="75" Height="23" HorizontalAlignment="Right" Margin="0,10,0,0" 
                  TabIndex="0" Content="OK"/>
      
          </Grid>
      
      </slcommon:ChildWindowEx>
      

      【讨论】:

        猜你喜欢
        • 2013-06-11
        • 2021-04-06
        • 1970-01-01
        • 2020-03-26
        • 2020-05-25
        • 2013-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多