简体   繁体   English

如何控制控件的加载顺序?

[英]How can I control the loading sequence of controls?

My English skill is poor because I'm not a native English speaker.我的英语水平很差,因为我不是以英语为母语的人。

My application has a MainWindowView and MainWindowBehavior also MainWindowView has control(Editor) as the following code.我的应用程序有一个 MainWindowView 和 MainWindowBehavior 也 MainWindowView 有控制(编辑器)如下代码。

<MainWindowView>
  <Grid>
    <TabControl>
      <TabItem>
        <Grid>
          <Editor x:Name="editor"/>
        </Grid>
      </TabItem>
    </TabControl>
  </Grid>

  <i:Interaction.Behaviors>
    <behaviors:MainWindowBehavior/>
  </i:Interaction.Behaviors>
</MainWindowView>

MainWindowBehavior uses the property of Editor in the LoadedEventHandler of MainWindowView. MainWindowBehavior 使用 MainWindowView 的 LoadedEventHandler 中 Editor 的属性。 The following code shows the above logic.以下代码显示了上述逻辑。

protected override void OnDetaching()
{
    this.AssociatedObject.Loaded -= AssociatedObject_Loaded;

    base.OnDetaching();
}

protected override void OnAttached()
{
    base.OnAttached();

    this.AssociatedObject.Loaded += AssociatedObject_Loaded;
}

private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
    this.mainWindow = sender as MainWindow;

    // run time error
    this.mainWindow.editor.Parser.ParsingFailed += Parser_ParsingFailed;
}

But compiler shows run time error because of the value of the Parser property of the editor is null.但是编译器显示运行时错误,因为编辑器的 Parser 属性的值为 null。 I tried to initialize the parser property of the editor at the Constructer, OnApplyTemplate function, Loaded EventHandler but 3 cases all called late than Loaded EventHandler of MainWindow.我试图在构造器上初始化编辑器的解析器属性,OnApplyTemplate function,加载的事件处理程序,但 3 个案例都比 MainWindow 的加载事件处理程序调用晚。

And as a result, generate run time error.结果,产生运行时错误。

I think that the Loaded EventHandler of the editor must be called early more Loaded EventHandler of the MainWindowBehavior.我认为编辑器的Loaded EventHandler必须更早调用MainWindowBehavior的Loaded EventHandler。 But in fact, the sequence reverse.但实际上,顺序相反。

I don't know why the sequence reverse.我不知道为什么顺序相反。

How can I change the loading sequence as I thought?如何按照我的想法更改加载顺序?

Thank you for reading.感谢您的阅读。

Maybe you cannot change the sequence of events, but you can sure change the way you listen to these events.也许你不能改变事件的顺序,但你可以肯定改变你收听这些事件的方式。 I suggest you to hook-up to an event in your mainWindow that will indicate you when its editor property is set.我建议您连接到mainWindow中的一个事件,该事件将在设置其editor属性时指示您。

Your code would become:您的代码将变为:

private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
    this.mainWindow = sender as MainWindow;
    // Here we don't access mainWindow.editor anymore, we hook-up to an event instead
    this.mainWindow.OnEditorPropertyChanged += MainWindow_EditorPropertyChanged;
}

private void MainWindow_EditorPropertyChanged(object sender){
{
    var mainWindow = sender as MainWindow;
    if (mainWindow.editor != null) {
        mainWindow.editor.Parser.ParsingFailed += Parser_ParsingFailed;
    }
}

And in your MainWindow , make sure to raise an event when its editor property is set, for example:在您的MainWindow中,确保在设置其editor属性时引发事件,例如:

public delegate void OnEditorPropertyChangedEventHandler(object sender);

public event OnEditorPropertyChangedEventHandler OnEditorPropertyChanged;

// Backing field
private Editor _editor;

public Editor editor {
    get => _editor;
    set => {
        _editor = value;
        OnEditorPropertyChanged?.Invoke(this); // Raise the event
    }
}

I found the cause of my problem while following your advice.在听从您的建议时,我找到了问题的原因。

I think the OnApplyTemplate function of the Editor is called early than the Loaded event of the MainWindowView.我认为编辑器的 OnApplyTemplate function 比 MainWindowView 的 Loaded 事件更早被调用。 But, in fact, the OnApplyTemplate function of the Editor is not called.但是,实际上是没有调用Editor的OnApplyTemplate function。

I have been apprehending wrong about my problem.我一直对我的问题理解错误。 I'm sorry... Now I would make my problem right.对不起...现在我会解决我的问题。

The Editor is Custom-Control.编辑器是自定义控件。 I missed adding code in the Generic.xaml file the following code.我错过了在 Generic.xaml 文件中添加以下代码的代码。

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/WpfControls;component/Themes/Editor.xaml"/>
</ResourceDictionary.MergedDictionaries>

Now I added the above code in the Generic.xaml and the OnApplyTemplate function of Editor is called normally also it is called early than the Loaded event of the MainWindowView.现在我在 Generic.xaml 和 OnApplyTemplate function 中添加了上面的代码,通常调用它也比 MainWindowView 的 Loaded 事件更早调用。

Thank you for your help and interest in my problem.感谢您对我的问题的帮助和关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Binding中加载控件而不阻止UI? - How i can make loading controls in Binding without blocking UI? 如何更好地一次控制多个控件的行为? - How can I better control behavior on multiple controls at once? 如何将ListView控件置于WPF中的其他控件之上 - How I can bring ListView control in top of other controls in WPF 如何将ASP Web用户控件中的表单控件与页面的其余表单控件隔离开? - How can I isolate the form controls in a ASP Web User Control from the rest of the page's form controls? 如何在进程等待完成时显示加载控件? - How can I display a loading control while a process is waiting for be finished? 在加载时如何延迟显示WinForm以防止其控件闪烁? - How can I delay showing a WinForm to prevent its controls from flickering when loading up? 如何解决UI控件映射控件丢失的映射问题? - How can I fix UI Control Map controls from losing their mappings? 如何在WPF控件/控件集中获得与具有链接到其他页面内容的HTML相同的行为? - How can I get the same behavior in a WPF control/set of controls as that of HTML with links to other page content? 如何通过上下文菜单控制动态生成的控件的可见性? - How can I control visibility of dynamically generated controls via context menu? 如何使内部控件不包含主控件的事件? - How can I make the inner controls don't cover the main control's events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM