WPF 视图在更改 contentControl 绑定属性时不呈现

发布于 2024-11-18 13:31:59 字数 1496 浏览 5 评论 0原文

我正在使用内容控件上的绑定属性更改主窗口上的用户控件。

XMAL:

<ContentControl Grid.Row="0" Content="{Binding MainContent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></ContentControl>

属性:

private UserControl _mainContent;
public UserControl MainContent
{
  get
  {
    return _mainContent;
  }
  set
  {
    _mainContent = value;
    OnPropertyChanged();
  }
}

代码隐藏:

MainContent = new TestUserControl();

属性已更改:

    public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
  if (this.PropertyChanged != null)
  {
    this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  }
}

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

我的问题是,在一台特定的测试机器上(与其他机器类似,没有什么独特或奇怪的。Windows 7、4GB RAM 等),当使用此机制更改用户控件时,应用程序挂起。

查看我的日志,收到更改命令,实例化新用户控件,运行构造函数。主内容属性已设置,OnPropertyChanged 事件触发

,然后什么也没有发生。应用程序挂起,Windows 表示它没有响应并关闭应用程序。

用户控件的 OnLoaded 事件永远不会被触发。

在特定机器上以这种方式加载任何用户控件时会发生这种情况。

欢迎提出想法、评论。欢迎提出如何调试这个的想法。

更新: 由于这是一台测试机,因此不会经常重新启动。

一旦我们重新启动机器,问题就消失了。 我仍然想知道为什么以及如何阻止这种情况再次发生。

PS。目标平台是x86,问题机器是x64,但在另一个window 7 x64上没有问题。我们正在使用.net框架4.0

I am changing user controls on my main window using a bound property on a content control.

XMAL:

<ContentControl Grid.Row="0" Content="{Binding MainContent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></ContentControl>

PROPERTY:

private UserControl _mainContent;
public UserControl MainContent
{
  get
  {
    return _mainContent;
  }
  set
  {
    _mainContent = value;
    OnPropertyChanged();
  }
}

CODE BEHIND:

MainContent = new TestUserControl();

ON PROPERTY CHANGED:

    public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
  if (this.PropertyChanged != null)
  {
    this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  }
}

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

My Problem is that on one specific test machine (similar to others, nothing unique or strange about it. Windows 7, 4GB Ram etc), when changing the usercontrols using this mechanism, the application hangs.

Looking at my logs, the Change command is received, the new user control is instantiated, the constructor runs. The Main Content Property is set, the OnPropertyChanged event fires

and then nothing. The application hangs and windows says its not responding and closes the app.

The OnLoaded event of the user control never gets fired.

This happens on loading any user control this way on the specific machine.

Ideas, comments are all welcome. Idea how to debug this one are welcome.

UPDATE:
As this is a test machine, its not rebooted very often.

Once we rebooted the machine, the problem went away. I would still like to know why and how to stop this happening again.

PS. The Target platform is x86 and the problem machine is x64, but on the other window 7 x64 there was no issue. We are using .net framework 4.0

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

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

发布评论

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

评论(1

千里故人稀 2024-11-25 13:31:59

我以前遇到过这个问题,这是由我使用引起的:

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

您的属性在发布模式下内联(也许在编译期间),因此堆栈搜索无法正确识别属性名称...

I ran into this once before, it was caused by me using:

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

Your properties get inlined in release mode (and perhaps during your compile) so the property name was not correctly identified by the stack search...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文