如何控制控件的加载顺序?
How can I control the loading sequence of controls?
我的英语水平很差,因为我的母语不是英语。
我的应用程序有 MainWindowView 和 MainWindowBehavior,MainWindowView 也有 control(Editor),如下代码所示。
<MainWindowView>
<Grid>
<TabControl>
<TabItem>
<Grid>
<Editor x:Name="editor"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
<i:Interaction.Behaviors>
<behaviors:MainWindowBehavior/>
</i:Interaction.Behaviors>
</MainWindowView>
MainWindowBehavior 在MainWindowView 的LoadedEventHandler 中使用Editor 的属性。
下面的代码展示了上面的逻辑。
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;
}
但是编译器显示运行时间错误,因为编辑器的解析器属性的值为空。
我尝试在 Constructer、OnApplyTemplate 函数、Loaded EventHandler 处初始化编辑器的解析器 属性,但 3 个案例都比 MainWindow 的 Loaded EventHandler 调用晚。
因此,产生 运行 时间错误。
我觉得编辑器的LoadedEventHandler一定要早点调用MainWindowBehavior的LoadedEventHandler。但实际上,顺序颠倒了。
不知道为什么顺序反了
怎样才能像我想的那样改变加载顺序?
感谢您的阅读。
也许你无法改变事件的顺序,但你肯定可以改变你聆听这些事件的方式。我建议您连接到 mainWindow
中的一个事件,该事件将在设置 editor
属性 时向您指示。
您的代码将变为:
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;
}
}
并且在您的 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
}
}
我按照您的建议找到了问题的原因。
我认为Editor的OnApplyTemplate函数调用早于MainWindowView的Loaded事件。但是,实际上并没有调用Editor的OnApplyTemplate函数
我一直对我的问题理解错误。对不起...
现在我会解决我的问题。
编辑器是自定义控件。我错过了在 Generic.xaml 文件中添加以下代码的代码。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfControls;component/Themes/Editor.xaml"/>
</ResourceDictionary.MergedDictionaries>
现在我在Generic.xaml中添加了上面的代码,Editor的OnApplyTemplate函数调用正常,而且比MainWindowView的Loaded事件调用早
感谢您对我的问题的帮助和关注。
我的英语水平很差,因为我的母语不是英语。
我的应用程序有 MainWindowView 和 MainWindowBehavior,MainWindowView 也有 control(Editor),如下代码所示。
<MainWindowView>
<Grid>
<TabControl>
<TabItem>
<Grid>
<Editor x:Name="editor"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
<i:Interaction.Behaviors>
<behaviors:MainWindowBehavior/>
</i:Interaction.Behaviors>
</MainWindowView>
MainWindowBehavior 在MainWindowView 的LoadedEventHandler 中使用Editor 的属性。 下面的代码展示了上面的逻辑。
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;
}
但是编译器显示运行时间错误,因为编辑器的解析器属性的值为空。 我尝试在 Constructer、OnApplyTemplate 函数、Loaded EventHandler 处初始化编辑器的解析器 属性,但 3 个案例都比 MainWindow 的 Loaded EventHandler 调用晚。
因此,产生 运行 时间错误。
我觉得编辑器的LoadedEventHandler一定要早点调用MainWindowBehavior的LoadedEventHandler。但实际上,顺序颠倒了。
不知道为什么顺序反了
怎样才能像我想的那样改变加载顺序?
感谢您的阅读。
也许你无法改变事件的顺序,但你肯定可以改变你聆听这些事件的方式。我建议您连接到 mainWindow
中的一个事件,该事件将在设置 editor
属性 时向您指示。
您的代码将变为:
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;
}
}
并且在您的 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
}
}
我按照您的建议找到了问题的原因。
我认为Editor的OnApplyTemplate函数调用早于MainWindowView的Loaded事件。但是,实际上并没有调用Editor的OnApplyTemplate函数
我一直对我的问题理解错误。对不起... 现在我会解决我的问题。
编辑器是自定义控件。我错过了在 Generic.xaml 文件中添加以下代码的代码。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfControls;component/Themes/Editor.xaml"/>
</ResourceDictionary.MergedDictionaries>
现在我在Generic.xaml中添加了上面的代码,Editor的OnApplyTemplate函数调用正常,而且比MainWindowView的Loaded事件调用早
感谢您对我的问题的帮助和关注。