使用 WPF 窗口作为自定义模式导入对话框在 DependencyProperties 上失败

发布于 2024-12-12 03:20:51 字数 1793 浏览 0 评论 0原文

我想创建一个自定义的导入对话框,因此我创建了一个包含一些内容的窗口。为了使此导入对话框模式化,我使用 ShowDialog() 方法。到目前为止,一切都按预期进行。我的代码如下所示:

var dialogresult = new MyImportDialog().ShowDialog();
if(dialogresult.HasValue && dialogresult.Value)
{
   Console.WriteLine("Import");

}

但是当我尝试使用此对话框两次时,出现 ArgumentException,因为我的静态 DependencyProperties 已第二次注册。所以我尝试不删除导入对话框并再次使用它。

private MyImportDialog _myImportDialog;

private void OnImportClick(object sender, RoutedEventArgs e)
{
     if (_myImportDialog== null)
          _myImportDialog= new MyImportDialog ();

     var dialogresult = _myImportDialog.ShowDialog();
     if(dialogresult.HasValue && dialogresult.Value)
     {
          Console.WriteLine("Import");              
     }
}

现在我收到 InvalidOperationException(在窗口关闭后无法设置 Visibility 或调用 Show、ShowDialog 或 WindowInteropHelper.EnsureHandle。)。但ShowDialog方法有一个备注:“打开一个窗口,只有当新打开的窗口关闭时才返回”。

所以我的下一个想法是在导入对话框中注册关闭事件,然后取消注册我的 DependecyProperties。不幸的是,没有官方方法可以做到这一点。我唯一发现的是: 任何取消注册的方法WPF 依赖属性?

但该解决方案(在我看来)有点脏,作者警告不要在生产环境中使用此代码。

那么,是否还有另一种更干净的解决方案来使用模态窗口两次?

在期待中感谢您。

编辑: 此代码显示了我正在使用的一个依赖属性:

public DependencyProperty ClearProperty = 
     DependencyProperty.Register("Clear", typeof (bool),
                                 typeof (MyImportDialog), 
                                 new PropertyMetadata(true));
    /// <summary>
    /// Indicates whether view should be cleard before importing new image stack.
    /// </summary>
    public bool Clear { 
         get { return (bool) GetValue(ClearProperty); } 
         set { SetValue(ClearProperty, value);} 
    }

I want to create a customized import dialog and therefore I created a window with some stuff. In order to make this import dialog modal, I'm using the ShowDialog() method. Up to now everything works as expected. My code looks like:

var dialogresult = new MyImportDialog().ShowDialog();
if(dialogresult.HasValue && dialogresult.Value)
{
   Console.WriteLine("Import");

}

But when I try to use this dialog twice, got a ArgumentException because my static DependencyProperties got registered a second time. So I tried not to delete my import dialog and use it a second time.

private MyImportDialog _myImportDialog;

private void OnImportClick(object sender, RoutedEventArgs e)
{
     if (_myImportDialog== null)
          _myImportDialog= new MyImportDialog ();

     var dialogresult = _myImportDialog.ShowDialog();
     if(dialogresult.HasValue && dialogresult.Value)
     {
          Console.WriteLine("Import");              
     }
}

Now I got an InvalidOperationException(Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.). But the ShowDialog method has an remark: "Opens a window and returns only when the newly opened window is closed."

So my next idea was to register for the closing event in my import dialog and then unregister my DependecyProperties. Unfortunately there is no official way doing this. The only thing I found was this: Any way to un-register a WPF dependency property?.

But the solution is (in my opinion) a little bit dirty and the author warned not to use this code in productive environment.

So, is there another more cleaner solution to use a modal window twice?

Thanking you in anticipation.

Edit:
This code shows one Dependency Property I'm using:

public DependencyProperty ClearProperty = 
     DependencyProperty.Register("Clear", typeof (bool),
                                 typeof (MyImportDialog), 
                                 new PropertyMetadata(true));
    /// <summary>
    /// Indicates whether view should be cleard before importing new image stack.
    /// </summary>
    public bool Clear { 
         get { return (bool) GetValue(ClearProperty); } 
         set { SetValue(ClearProperty, value);} 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2024-12-19 03:20:51

听起来您的静态依赖属性未定义为静态成员。否则,它们只会在执行静态初始化程序时(即第一次引用该类)时被初始化。您可以粘贴 DependencyProperty.Register 代码吗? DependencyProperty 字段应该静态声明。

public static DependencyProperty ClearProperty =  
 DependencyProperty.Register("Clear", typeof (bool), 
                             typeof (MyImportDialog),  
                             new PropertyMetadata(true));

It sounds like your static dependency properties are not defined as static members. Otherwise they would only be initialized when the static initializer was executed (i.e. the first time the class was referenced). Can you paste the DependencyProperty.Register code? DependencyProperty fields are supposed to be declared statically.

public static DependencyProperty ClearProperty =  
 DependencyProperty.Register("Clear", typeof (bool), 
                             typeof (MyImportDialog),  
                             new PropertyMetadata(true));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文