繁体   English   中英

ObservableCollection.CollectionChange在设计时

[英]ObservableCollection.CollectionChanged at design time

public Router() // Router is a ContentControl with [ContentProperty(nameof(Routes))]
{
    Routes.CollectionChanged += (sender, args) =>
    {
        // designer will crash here because NewItems is null...
        if (args.NewItems.Count == 0) return;
        var route = (Route)args.NewItems[0];

        _routesCache[route.Id] = route;
    };
}

public ObservableCollection<Route> Routes { get; } = new ObservableCollection<Route>();

CollectionChanged调用,但args.NewItemsnull并且collection始终为空。 有什么方法可以在设计时使它可用吗?

<local:Router x:Name="AppRouter">
    <local:Route Id="Home" Type="local:Home" />
    <local:Route Id="Login" Type="local:Login" />
</local:Router>

我需要在设计时访问这两个Route对象。

我通过不使用ObservableCollection<T>来解决它,因为即使在发生Loaded事件后,项目在设计时始终为空。

我现在只使用List<T>并访问Loaded事件处理程序中的路由。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM