English 中文(简体)
InitializeComponent上的StackOverflow异常
原标题:StackOverflowException on InitializeComponent
  • 时间:2010-10-19 19:08:56
  •  标签:
  • c#
  • wpf
  • xaml

我正在尝试关注这个小教程,但我一直收到这个异常。

相关XAML如下所示:

    <StatusBar Margin="0,288,0,0" Name="statusBar" Height="23" VerticalAlignment="Bottom">
        <StatusBar.DataContext>
            <m:MainWindow />
        </StatusBar.DataContext>
        <TextBlock Name="statusText" Text="{Binding Path=StatusBarText, NotifyOnTargetUpdated=True}" DataContext="{Binding}">
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:4" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
    </StatusBar>

我猜我得到了StackOverflowException,因为我试图使用MainWindow作为DataContext。我想使用MainWindow,因为它似乎是放置我的StatusBarText属性的逻辑位置,

public partial class MainWindow : Window
{
    public string StatusBarText { get; set; }

它使在我的代码中更容易访问事件处理程序。

那我该怎么办?我应该把这处房产放在哪里?或者有没有一种方法可以将DataContext设置为“this”,这样它就不会创建MainWindow的新实例,只引用自己?

最佳回答

我通常在代码背后的构造函数中设置我的DataContext(我通常使用MVVM,但在小型临时项目中使用了一个窗口):

public MainWindow()
{
    statusBar.DataContext = this;
}

请注意,在所示的代码示例中,您将只获得初始StatusBarText值,因为您没有实现INotifyPropertyChanged。

问题回答

理想情况下,您将绑定到的属性应该位于遵循MVVM模式的ViewModel中,将它们从视图中抽象出来。既然这不是你的问题,我们将继续前进。。。DataContext是从其父级继承的。因此,如果StatusBar存在于Window中(我很确定它确实存在),那么它将已经从Window继承DataContext。您实际上是在尝试使用UI组件(Window)作为DataContext的源来绑定UI组件。远非理想。。。以下是MVVM模式。。。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签